Static int buffersize = 18; // hexadecimal size (6 bytes)
Byte [] buffer = new Byte [buffersize]; // create a buffer
Private void button#click (object sender, EventArgs e)
{
SerialPort1.Read (buffer, 0, buffersize );
String ss;
Ss = byteToHexStr (buffer); // The byteToHexStr function is used.
TextBox2.Text = ss;
SerialPort1.Close ();
MessageBox. Show ("data received successfully! "," System prompt ");
}
// Convert byte array to hexadecimal string
Public static string byteToHexStr (byte [] bytes)
{
String returnStr = "";
If (bytes! = Null)
{
For (int I = 0; I <bytes. Length; I ++)
{
ReturnStr + = bytes [I]. ToString ("X2 ");
}
}
Return returnStr;
}
Private void Form1_Load (object sender, EventArgs e)
{
SerialPort1.PortName = "COM1 ";
SerialPort1.BaudRate = 9600;
SerialPort1.DataBits = 8; // data bit
SerialPort1.Parity = System. IO. Ports. Parity. None; // No Parity bit
SerialPort1.StopBits = System. IO. Ports. StopBits. One; // a stop bit
SerialPort1.ReadBufferSize = 1024; // size of the receiving buffer
SerialPort1.Encoding = Encoding. BigEndianUnicode;
SerialPort1.Open ();
}
From brian0031