Serial Port Complete - Latest Microcontroller projects

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

6 5  


6 5- 


The BinaryReader and BinaryWriter classes support reading and writing data in
a variety of formats, including bytes and other numeric types, Boolean data,
Chars, and length-prefixed strings.
An application uses the SerialPort object’s BaseStream property to obtain a
Stream object and then uses the object to create BinaryReader and Binary-
Wr i t e r o b j e c t s :

 Friend binaryReader1 As BinaryReader
Friend binaryWriter1 As BinaryWriter
Dim serialPortStream As Stream


' Get the port's Stream object.

serialPortStream = myComPort.BaseStream

' Use the Stream object to create BinaryReader and BinaryWriter objects.

binaryReader1 = New BinaryReader(serialPortStream)
binaryWriter1 = New BinaryWriter(serialPortStream)

 internal BinaryReader binaryReader1;
internal BinaryWriter binaryWriter1;
Stream serialPortStream = null;


// Get the port's Stream object.

serialPortStream = myComPort.BaseStream;

// Use the Stream object to create BinaryReader and BinaryWriter objects.

binaryReader1 = new BinaryReader( serialPortStream );
binaryWriter1 = new BinaryWriter( serialPortStream );
The Write method can write a byte to a port:

 Dim dataToWrite As Byte


dataToWrite = 65
binaryWriter1.Write(dataToWrite)
Free download pdf