public Read ( byte buffer, int offset, int count ) : int | ||
buffer | byte | |
offset | int | |
count | int | |
return | int |
using System.IO.Ports; SerialPort myPort = new SerialPort("COM1"); // creates a new instance of the SerialPort class myPort.Open(); // opens the port for communication byte[] buffer = new byte[16]; // creates a byte array with a length of 16 int bytesRead = myPort.Read(buffer, 0, buffer.Length); // reads up to 16 bytes from the input buffer and saves them to the byte array myPort.Close(); // closes the port when finished
using System.IO.Ports; SerialPort myPort = new SerialPort("COM1"); // creates a new instance of the SerialPort class myPort.Open(); // opens the port for communication while (myPort.BytesToRead > 0) // while there is data to read in the input buffer { byte[] buffer = new byte[myPort.BytesToRead]; // create a byte array to hold the data myPort.Read(buffer, 0, buffer.Length); // reads the data into the array Console.WriteLine(Encoding.ASCII.GetString(buffer)); // displays the data as ASCII text } myPort.Close(); // closes the port when finishedThis example creates a new SerialPort object, opens the communication port, checks if there is data in the input buffer, creates a byte array to hold the data, reads the data into the array, converts the byte array to ASCII text, displays the text to the console, and continues the loop until all the data has been retrieved from the input buffer. Finally, it closes the port. Package Library: The System.IO.Ports.SerialPort class is part of the .NET Framework and does not require the installation of any external packages or libraries.
public Read ( byte buffer, int offset, int count ) : int | ||
buffer | byte | |
offset | int | |
count | int | |
return | int |