Join 244,295 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 874 people online right now. Registration is fast and FREE... Join Now!
Hello! I've writed a program that function as a serial multiplexer, thus I can read from 9 ports simultaneously and forward the messages to the main port. Also, the messages incoming from the main port are distributed to the aforementioned multiplexed ports. I have used the tutorial found on this site and modified it for my needs.
I have bought some pci serial port controllers so I can have at least 6 ports to work, and I have connected an async Analog modem 9600kbps to each port.
The program works just fine even when the incoming messages are not right, but the problems start if the leased line is cut off for some reason (it happens very often). then, the input buffer gets full of garbage data and the program gets stuck, so if I try to close the port that causes the problem, the program ends. I tried to make input/output buffer larger but no result. I tried to introduce the following: If Not comport.Breakstate then comport.ReadTo(xxx) end if but again no result. Take in mind that I have writed a sub for data filtering, so the messages that contain non-readable characters are not forwarded(only ABC... and 123...) The code is very large to paste in its whole, so if somebody can give me an opinion, I can paste some parts here. Please help...
The sub comport_DataReceived is as follows:
CODE
Try Dim msg As String If Not comPort.BreakState Then comPort.ReadTimeout = -1 msg = comPort.ReadTo(Chr(13)) _type = MessageType.Incoming msg = msg.Trim msg = Chr(10) & msg & Chr(13) _msg = msg DisplayData(MessageType.Incoming, msg) Else comPort.Close() comPort.Open() 'reset port End If Exit Select Catch ex As Exception comPort.Close() comPort.Open() 'reset port-these resets I added them yesterday,not yet tested if work properly End Try
msg = Chr(10) & msg & Chr(13)
this cannot change. It is the way modems communicate
Try Dim msg As String If Not comPort.BreakState Then comPort.ReadTimeout = -1 msg = comPort.ReadTo(Chr(13)) _type = MessageType.Incoming msg = msg.Trim msg = Chr(10) & msg & Chr(13) _msg = msg DisplayData(MessageType.Incoming, msg)
Else comPort.Close() comPort.Open() 'reset port End If
Exit Select Catch ex As Exception comPort.Close() comPort.Open() 'reset port-these resets I added them yesterday,not yet tested if work properly End Try
When BreakState is true and the else branch is taken, can you use the comport.BytesToRead property and the comport.Read() method to empty the serialport's input buffer?
When BreakState is true and the else branch is taken, can you use the comport.BytesToRead property and the comport.Read() method to empty the serialport's input buffer?
when breakstate is true the modem is supposed to has lost communication and the program gets stuck. how can I empty the buffer? actually all this is theoretical because I realised that breakstate is always false...
if it is false the modem is not connected. btw - when issuing modem commands, they are acted on when carriage return is received.
SerialPort1.Write("AT" & vbCr) 'returns OK
I have tried to enclose the read command in an if..then where I checked the CDHolding status before reading from the buffer but then the program never read anything, so I gave up with this idea.
As for the commands, I don't want to give commands to the modem itself, but to another system connected to the modem, so I just need these characters, tested and works.
The program works just fine even when the incoming messages are not right, but the problems start if the leased line is cut off for some reason (it happens very often). then, the input buffer gets full of garbage data and the program gets stuck, so if I try to close the port that causes the problem, the program ends.
The program works just fine even when the incoming messages are not right, but the problems start if the leased line is cut off for some reason (it happens very often). then, the input buffer gets full of garbage data and the program gets stuck, so if I try to close the port that causes the problem, the program ends.
Why is the program ending?
if I knew, I would solve that issue... I guess something might be wrong with the input buffer (despite the fact I have set the buffers to 256K (!)) the program doesn't end on its own, it just gets very very slow or even completely stuck and if I click on any button, I get a windows error message, I click on "Don't Send" (!) and then the program terminates. Is there any possibility that the port could go to an undefined status or something like that? Or maybe because of the mass garbage data, the bus that carries serial data from the ports to CPU gets stuck...(?)
I suppose that if it was an input buffer issue, then only the specific port that is connected to the offline modem would get stuck, but the program in its whole malfunction.
break is an indication from the other end that if you are sending data to stop, or that the other end needs attention.
if carrier detect is false the modem is not connected.
so, I may not use breakstate... but (I think) Carrier Detect is false all the time, even if the modem is connected and has Carrier with the modem at the other end. otherwise I would get the messages from the port at that case
If I would filter the garbage data before it would enter the main program, would that solve my problem? I have a data filter sub, that is true when the parameter string is readable (ABC..., 123...). What if I would apply this in the comport_DataReceived sub?
and always in the end there is a message "NO CARRIER" but it is too late...
This is my data filter:
CODE
Private Function Allowed(ByVal msg1 As String) As Boolean Dim permit As Boolean Dim character As Char Dim asciiofchar, i As Integer msg1 = msg1.ToUpper permit = False If msg1.Length > 1 Then For i = 0 To (msg1.Length - 1) Step 1 character = msg1.Substring(i, 1) asciiofchar = Asc(character) If asciiofchar >= 48 And asciiofchar <= 57 Then permit = True ElseIf asciiofchar >= 65 And asciiofchar <= 90 Then permit = True ElseIf asciiofchar = 10 Then permit = True ElseIf asciiofchar = 13 Then permit = True Else permit = False Exit For End If Next i ElseIf msg1.Length = 1 Then character = msg1 asciiofchar = Asc(character) If asciiofchar >= 48 And asciiofchar <= 57 Then permit = True ElseIf asciiofchar >= 65 And asciiofchar <= 90 Then permit = True ElseIf asciiofchar = 10 Then permit = True ElseIf asciiofchar = 13 Then permit = True Else permit = False End If End If Return permit End Function
I upload the .dat logfile with the messages I get at the time of disconnect.
...
QUOTE(dbasnett @ 11 Nov, 2008 - 02:47 PM)
i believe the default is for CD to follow carrier. the command to set this if it isn't the default is
AT&C1
Even then, the CD would become False after about half a second, time in which some messages have entered and the program and the result is the same. even if that would help, what could I do if I get the CDHolding=False? Closing and reopening the port after some seconds would be an idea...
I am thinking to use this expression
CODE
If Allowed(comport.ReadTo(Chr(13))) then msg=comport.ReadTo(Chr(13)) endif
like I foresee if the message is right
what do you think?
but I don't know if in this way, the buffer would empty or the garbae data would still remain in the buffer and I get them on the next ReadTo
the point is that the program shouldn't get stuck. the modem cannot redial if the port is open, because there are forwarded outgoing messages that interrupt the handshake between the modems. that I realised by trial. the program is supposed to work even (especially) if I am not there...
sure it can. if it has dropped carrier then the modem should be in command mode and just do this
atdt5551212
ok it can redial, I tested it today, but there's no point in this, since the program remains stuck. once it gets stuck it can't get into normal function
I want to write a program that doesn't get stuck when the modem is disconnecting. Besides, now, I am the one who causes the disconnection and I can plug the line again back soon, but in real, the line can be down at every time, and the result will be the same-the program crashes and nothing works. Sure I can redial and restart the program but that's not what I want. I want the program not to crash with the modem's disconnection...
where does it crash? are you checking CD? if CD is false you should not bother reading from the serial port[size=3].
why? it crashes when the modem disconnects (CD=off) and it sends garbage data to the serial port. i am not checking the CD before reading from the port..