Receiving strings from Serial Port

Dear reader,

I want to read a string from a serial port. I found the sample code in Help, to

do this, with the following code:

Using com1 as IO.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

The problem is that if the return string does not end with LineFeed, the

com1.ReadLine command does not return to the application, and as a result

doesn not return any value to Incoming, to check it contents.

The application give the impression that it is just hanging, untill I stop the application.

Also how does the Readtimeout property work. How can I get the ReadLine method

to timeout, if there was no message received.

I would appreciate any repoonse on this issue.

[964 byte] By [Secsi] at [2007-12-24]
# 1
The ReadTimeout property will cause the ReadLine() to abort with an exception. Not sure if you'd really want to use that, consider using the DataReceived event instead...
nobugz at 2007-10-8 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

You can try using ReadExisting() instead of ReadLine().

Or, handle the Com1.DataReceived event as previously suggested (this is probably the best method).

rkimble at 2007-10-8 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

Thanks for the reply, I am following your solution.

I searched Help to find more about the DataReceived event, but as usual

the maize of Microsoft documentation is not very helpfull.

According to Help, the DataReceived event must first be declared:

I tried the samples in the documentation, but did not manage.

Please could you help me to declare the DataReceived event so that I can use it.

Thanks

Secsi at 2007-10-8 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

Dear nobugz,

Found another solution !!!.

Trying to implement what you suggested I looked deeper in the Com1.Readbyte

I made a smalll loop that checks: if com1.BytesToRead > 0 then

incoming = com1.ReadByte

And it works.

Thanks very much.

Secsi at 2007-10-8 > top of Msdn Tech,Visual Basic,Visual Basic General...