
588
|
Chapter 13: The ‘My’ Reference
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
Description
The OpenSerialPort method opens one of the serial ports on the local computer and
returns an instance of System.IO.Ports.SerialPort for that port.
Usage at a Glance
• This method is not valid in ASP.NET applications.
• This serial port must be closed and disposed of when finished. Normally, this is
done by using the Close method on the object returned from the OpenSerialPort
method. You may also close it automatically by using the
Using keyword. The
example provided in this entry shows how to do this.
• An exception is thrown if any of the parameters passed are invalid or are not
available through the specified port.
Example
The following example opens the computer’s COM1 serial port and sends a single line
of text data. The serial port is closed implicitly through the use of the
Using keyword.
The System.IO.Ports.SerialPort.WriteLine method sends the data to the serial port.
Public Sub SendTextToCOM1(ByVal textToSend As String)
' ----- Open COM1 and send some text.
Dim comPort As System.IO.Ports.SerialPort
Using comPort = My.Computer.Ports.OpenSerialPort("COM1")
comPort.WriteLine(textToSend)
End Using
End Function
Related Framework Entries
• Microsoft.VisualBasic.Devices.Ports.OpenSerialPort Method
• System.IO.Ports.SerialPort.Open Method
See ...