Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 9


For applications that transfer text, numeric data other than bytes, and logical
values, the SerialPort class includes methods that handle the encoding and
decoding of these data types. For example, an application that wants to send the
text “CMD1” followed by a LF character could store the character codes in a
byte array and write the array to a port:

 Dim dataToSend(4) As Byte


dataToSend(0) = Asc("C")
dataToSend(1) = Asc("M")
dataToSend(2) = Asc("D")
dataToSend(3) = Asc("1")
dataToSend(4) = 10
myComPort.Write(dataToSend, 0, 5)

Table 9-1: Applications can use a variety of methods to write data to a SerialPort object.


9:   
+!  7

SerialPort Write Byte array.
Char array or subarray.
String.

yes

WriteLine String + NewLine.
NewLine only.

yes

WriteByte Byte. yes
BinaryWriter Write Byte, Byte array or subarray, SByte.
Char, Char array or subarray.
String.
Boolean, Decimal, Double, Int16, Int32, Int64,
Single, UInt16, UInt32, UInt64.

yes

StreamWriter Write Char, Char array or subarray.
String, formatted String.
Text representation of Boolean, Decimal,
Double, Int32, Int64, Object, Single, UInt32,
UInt64.

yes

WriteLine Same as Write but with NewLine appended.
NewLine only.

yes
Free download pdf