Serial Port Complete - Latest Microcontroller projects

(lily) #1
Using .NET’s SerialPort Class

The StreamReader class provides several methods for reading text from a Seri-
alPort object.
StreamReader’s Read method can read a single character into an Integer or one
or more characters into a Char array. The method returns when at least one
character has been received:

 ' Read a character.


Dim receivedData1 As Integer

receivedData1 = streamReader1.Read
Console.WriteLine(Chr(receivedData1))

' Read two characters into an array.

Dim receivedData2(1) As Char
Dim count as Integer

count = streamReader1.Read(receivedData2, 0, 2)
Console.WriteLine(count & " characters received: ")
Console.WriteLine(receivedData2)

 // Read a character.


int receivedData1;

receivedData1 = streamReader1.Read();
Console.WriteLine(Strings.ChrW(receivedData1));

// Read two characters into an array.

Char[] receivedData2 = new Char[2];
int count;

count = streamReader1.Read(receivedData2, 0, 2);
Console.WriteLine(count + " characters received: ");
Console.WriteLine(receivedData2);
ReadBlock is a blocking version of the Read method and stores received data in
a Char array. Unlike Read, ReadBlock waits for all of the requested characters to
arrive or a timeout.
Free download pdf