My.Computer.Ports
to receive strings from the computer's serial ports in Visual Basic.COM1
.My.Computer.Ports.OpenSerialPort
method to obtain a reference to the port. For more information, see OpenSerialPort.Try...Catch...Finally
block allows the application to close the serial port even if it generates an exception. All code that manipulates the serial port should appear within this block.Do
loop for reading lines of text until no more lines are available.If
statement to determine if the ReadLine() method returns Nothing
(which means no more text is available). If it does return Nothing
, exit the Do
loop.Else
block to the If
statement to handle the case if the string is actually read. The block appends the string from the serial port to the return string.COM1
.COM1
. For more flexibility, the code should allow the user to select the desired serial port from a list of available ports. For more information, see How to: Show Available Serial Ports.Try...Catch...Finally
block to make sure that the application closes the port and to catch any timeout exceptions. For more information, see Try...Catch...Finally Statement.