SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 192 | Next

Jan Axelson

"Serial Port Complete: COM Ports, USB Virtual COM Ports, and Ports for Embedded Systems"

The ReadExisting method is an exception that
returns immediately whether data is available or not. The BytesToRead property
provides a way to find out if data is available before attempting a read operation.
Methods that write data also block until the write buffer is empty.
Attempting to read or write to a port that isn??™t open raises an InvalidOperation-
Exception. To prevent the exception, code can check to ensure the port is open
and open the port if needed before reading or writing to the port.
Using .NET??™s SerialPort Class
167
 dim portData as String
If (Not (myComPort Is Nothing)) Then
If (Not myComPort.IsOpen) Then
myComPort.Open()
End If
End If
If myComPort.IsOpen Then
myComPort.ReadLine(portData)
myComPort.WriteLine(portData)
End If
 String portData;
if (!(ComPort.selectedPort == null))
{
if (!myComPort.IsOpen)
{
myComPort.Open();
}
}
if (ComPort.selectedPort.IsOpen )
{
portData = myComPort.ReadLine();
myComPort.WriteLine(portData ) ;
}
13 65
Many applications send and receive data stored in byte arrays and read data into
byte arrays or byte variables.
Chapter 9
168
3 &
The SerialPort object??™s Write method can write all or a portion of a byte array to
a port. This example creates a 3-byte array and writes the array??™s contents to a
port:
 Dim byteBuffer(2) As Byte
byteBuffer(0) = 117
byteBuffer(1) = 115
byteBuffer(2) = 98
myComPort.


Pages:
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204