Serial Port Complete - Latest Microcontroller projects

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

 Char[] textToWrite = new Char[2];


textToWrite[0] = 'h' ;
textToWrite[1] = 'i';
myComPort.Write(textToWrite, 0, 2);

5 
The SerialPort class provides five methods for reading text.
The ReadExisting method returns a String containing all of the characters in
the receive buffer. If the buffer is empty, the method returns an empty String.
ReadExisting is the only read method that doesn’t block until receiving data or a
timeout.

 Dim receivedText as String


receivedText = myComPort.ReadExisting

 String receivedText;


receivedText = myComPort.ReadExisting();
The ReadLine method returns all received data up to a NewLine character:

 Dim receivedText as String


receivedText = myComPort.ReadLine

 String receivedText;


receivedText = myComPort.ReadLine();
The returned String contains everything up to the NewLine. The NewLine
character(s) are removed from the receive buffer and discarded. If the buffer
doesn’t contain a NewLine, the method waits up to the ReadTimeout value and
then raises a timeout exception.
The ReadTo method is like ReadLine but enables defining any value as the
delimiter.
The Read method can copy a specified number of received Chars into a Char
array starting at a specified offset.
Free download pdf