Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 9


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.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 off-
set 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
Free download pdf