1.缓冲区不定字节读取
byte[] data = new byte[serialPort1.BytesToRead]; //定义缓冲区,因为串口事件触发时有可能收到不止一个字节
serialPort1.Read(data, 0, data.Length);
foreach (byte Member in data) //遍历用法
{
string str = Convert.ToString(Member, 16).ToUpper();
textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");
}
2.单字节读取
byte DataReceived = (byte)(serialPort1.ReadByte()); //单字节读取
时间: 2024-10-29 21:09:18