Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 9


 byte dataToWrite;


dataToWrite = 65;
binaryWriter1.Write( dataToWrite );
The Read method can read a received byte into a byte array:

 Dim receivedData(0) As Byte


binaryReader1.Read(receivedData, 0, 1)
Console.WriteLine(ChrW(receivedData(0)))

 byte[] receivedData = new byte[ 1 ];


binaryReader1.Read( receivedData, 0, 1 );
Console.WriteLine((char)( receivedData[ 0 ] ) );
The Write method can also write just about any data type. For example, a
BinaryWriter object can write an Int32 value:

 Dim valueToWrite As Int32


valueToWrite = 66
binaryWriter1.Write(valueToWrite)

 Int32 dataToWrite;


dataToWrite = 66;
binaryWriter1.Write( dataToWrite );
Each Int32 value written to the port is four bytes. For example, writing an
Int32 with a value of 66 (42h) writes the bytes 42h, 00h, 00h, 00h to the port.
The receiving computer can store the value in an Int32 variable:

 Dim receivedData As Int32


receivedData = binaryReader1.ReadInt32

 Int32 receivedData;


receivedData = binaryReader1.ReadInt32();
The ReadInt32 method returns on receiving four bytes.
Free download pdf