OpenSerialPort Method


OpenSerialPort Method

Location

My.Computer.Ports.OpenSerialPort

Syntax

     Dim result As System.IO.Ports.SerialPort = _        My.Computer.Ports.OpenSerialPort(portName[, baudRate _    [, parity [, dataBits [, stopBits]]]]) 


portName (required; String)

The name of the port to open. Typically, this will be a numbered "COM" serial port, as in "COM1."


baudRate (optional; Integer)

The desired baud rate of the port. The value you supply must be supported by the port you are trying to open. By default, a value of 9600 bits per second (bps) is used.


parity (optional; Parity enumeration)

The parity settings of the port. One of the following System.IO.Ports.Parity enumeration values.

Value

Description

Even

Sets the parity bit so that the count of bits set within each unit is always an even number of bits

Mark

Always sets the parity bit to 0

None

Skips all parity checks

Odd

Sets the parity bit so that the count of bits set within each unit is always an odd number of bits

Space

Always sets the parity bit to 1



dataBits (optional; Integer)

Data-bit setting of the port. This value ranges from 5 to 8 and is set to 8 by default.


stopBits (optional; StopBits enumeration)

Stop-bit setting of the port, indicating the number of bits used to separate each unit of data. One of the following System.IO.Ports.StopBits enumeration values.

Value

Description

None

Uses no stop bits

One

Uses one stop bit

OnePointFive

Uses 1.5 stop bits

Two

Uses two stop bits


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 Also

Ports Object, SerialPortNames Property




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net