Write(byteBuffer, 0, 3)
byte[] byteBuffer = new byte[3];
byteBuffer[0] = 117;
byteBuffer[1] = 115;
byteBuffer[2] = 98;
myComPort.Write(byteBuffer, 0, 3);
5 &
The SerialPort class provides two methods for reading bytes.
The Read method can copy a specified number of received bytes from the
receive buffer into a byte array beginning at a specified offset.
This example reads up to three received bytes, stores the bytes beginning at offset
1 in the byteBuffer array, and displays the result:
Dim byteBuffer() As Byte = {0, 0, 0, 0}
Dim count As Integer
Dim numberOfReceivedBytes As Integer
myComPort.Read(byteBuffer, 1, 3)
For count = 0 To 3
Console.WriteLine(CStr(byteBuffer(count)))
Next count
Using .NET??™s SerialPort Class
169
byte[] byteBuffer = new byte[4] {0, 0, 0, 0};
Int32 count;
Int32 numberOfReceivedBytes;
myComPort.Read(byteBuffer, 1, 3);
for (count = 0; count <= 3; count++)
{
Console.WriteLine(byteBuffer[count].ToString() );
}
If the remote port sends bytes with values 10, 20, and 30, the output is this:
0
10
20
30
The Read method doesn??™t wait for the specified number of bytes to arrive. The
method returns if there is at least one received byte in the buffer. For example, if
the remote port has sent a single byte with a value of 10, the Read method in
the example above returns on receiving the byte and the output is this:
0
10
0
0
Reading a byte removes the byte from the receive buffer.
Pages:
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205