Comm port read hangs

justhininabouti

New member
Joined
Apr 17, 2010
Messages
1
Programming Experience
Beginner
I'm VERY new to VB, and haven't coded since the TRS-80 days. I;m attempting to write code to read several NMEA ports and format the data for archive. When I execute the following code:

Using Commportno As IO.Ports.SerialPort = _

My.Computer.Ports.OpenSerialPort(portName:=port)

Dim Incoming As String = Commportno.ReadLine()



on a port with the device turned off, the readline instruction hangs. can someone point me in the right direction to handle this exception.



Tia
 
It's not an exception. You're telling the SerialPort object to read a line so it's waiting until a line is available to read, which is what it's supposed to do.

The value of the SerialPort's ReadTimeout property is equal to SerialPort.InfiniteTimeout by default. If you want to wait only a specific period before aborting a read operation then you need to set ReadTimeout to the appropriate value. If the timeout period expires before data is successfully read, THEN an exception will occur. The MSDN documentation for the SerialPort.ReadTimeout property has a code example.

In future, I'd suggest consulting the documentation first. A look at the documentation for the SerialPort class would have revealed the ReadTimeout property and, as I said, the documentation for that property provides a code example of its use. Most questions can be answered by the documentation when you already know what type or member you're using because you can just go straight to the appropriate topic for that type or member.
 
Back
Top