Chapter 9
This example reads up to three received Chars, stores the bytes beginning at off-
set zero in a byte array, and displays the result:
Dim charBuffer(7) As Char
Dim count As Integer
myComPort.Read(charBuffer, 0, 3)
For count = 0 To 2
Console.WriteLine(CStr(charBuffer(count)))
Next count
char[] charBuffer = new char[8];
int count = 0;
myComPort.Read(charBuffer, 0, 3)
for ( count=0; count <= 2; count++ )
{
Console.WriteLine(System.Convert.ToString(charBuffer[count]));
}
If the buffer contains the characters “a”, “b”, and “c”, the output is this:
Number of received bytes = 3
a
b
c
Characters that have been read are no longer available in the receive buffer.
When reading a byte array, the Read method stores one byte in each array ele-
ment. When reading a Char array, the Read method stores received data as Uni-
code characters. Each element in a Char array represents one character, whether
the character’s encoding uses one or more bytes. The Read method returns
when at least one character is available or on a timeout.
The ReadChar method reads a single Char from the receive buffer. The method
returns an integer that contains an encoded character.
Dim charToRead As Integer
charToRead = myComPort.ReadChar()
Console.WriteLine(ChrW(charToRead))