Receving strings from Serial Port
Dear readers,
I am sending an ASCII request via the serial port to a device, which answers back with a serial string. I am using VB2005.
After writing the request, I am using the the following code to read the string from the serial port, as per sample in Visual Basic Help.
Using com1 as IO.Ports.Ports.SerialPort =My.Computer.Ports.OpenSerialPort("COM1")
Do
Dim Incoming as String = com1.ReadLine()
If Incoming Is Nothing then
Exit Do
Else
ReturnStr & = Incoming & vbCrLf
End If
Loop
end using
I can read the string from the serial port, as long as
it ends with a Line Feed character.
The problem is, if the device does not answer back, or does not
end with Line Feed, the ReadLine method, is hanging somewhere waiting for the Line Feed command and not returning Incoming equals to Nothing.
I set the Read Timeout to 3 seconds, but it
does not return to my application.
It seems that the method does not timeout after a while, returning
with Incoming equals to Nothing, as per the sample code.
I also tried to use ReadChar to read the characters. With a loop
I can read each character in the buffer, but after reading the last
character control is not given back to my application.
It gives the impression the code is hanging.
I would appreciate any help on this
Thanks
|