Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 9


If a SerialPort object is using ASCIIEncoding and storing received data in a
String or Char array, each received byte is stored by default as a 16-bit charac-
ter:

 ' Read a String.


Dim receivedText1 As String

receivedText1 = myComPort.ReadExisting
Console.WriteLine(receivedText1)

' Read a Char array.

Dim receivedText2(3) As Char

myComPort.Read(receivedText2, 0, 4)
Console.WriteLine(receivedText2)

 // Read a String.


String receivedText1;

receivedText1=myComPort.ReadExisting();
Console.WriteLine(receivedText1);

// Read a Char array.

Char[] receivedText2 = new Char[4];

myComPort.Read(receivedText2, 0, 4);
Console.WriteLine(receivedText2);
The ReadExisting and Read methods retrieve received characters and store each
as a 16-bit value in a String.
The transmitting and receiving computers must agree on an encoding method
for the text. For example, in the code below, the transmitting computer might
think it’s transmitting a byte equal to 169 (A9h), which is the code point for a
copyright symbol (©):

 Dim charToSend as Char = ChrW(169)


myComPort.Write(charToSend)
Free download pdf