Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 9


1 3  
1


Chapter 2 introduced methods of encoding text. The SerialPort class provides a
variety of methods for reading and writing text data as Strings and Char vari-
ables.

3
 
The Write and WriteLine methods can write text to a port.
The Write method used for writing byte arrays can also write Strings:

 Dim textToWrite As String


textToWrite = "hello, world"
myComPort.Write(textToWrite)

 String textToWrite;


textToWrite = "hello, world";
myComPort.Write(textToWrite);
The WriteLine method is similar to Write but appends a NewLine character or
characters to the String. The default NewLine value is a line feed (LF, or code
point U+000A). Other possible NewLine values are a carriage return (CR, or
U+000D), or a CR followed by a LF. Visual Basic defines constants for these
values (VbLf, VbCr, and VbCrLf ).
The carriage-return and line-feed characters affect how text is printed or dis-
played on the receiving computer. To save bandwidth, software at a receiving
computer can convert each received LF into a CR+LF if needed.
The Write method can also write all or a portion of a Char array to a port. This
example places two characters in an array and writes the characters to a port:

 Dim textToWrite(1) As Char


textToWrite(0) = "h"c
textToWrite(1) = "i"c
myComPort.Write(textToWrite, 0, 2)
Free download pdf