.NET 串口通信中断接收,包含0X1A(作为EOF)

.NET串口通信中将`0X1A`当做EOF处理,.NET接收到EOF会触发一次接收中断,此时事件形参`SerialDataReceivedEventArgs`值为枚举 `Eof`,其他为`Chars`。

参考: * 、MSDN

## 测试

.NET编程,中断方式接收,发送一串数据后等待回应,打印进中断次数;另外一端用串口助手来实现。通信用虚拟串口。

 [TestClass]
public class EofByteSerialTest
{
private int _count = ;
private byte[] _receivedDataPacket;
SerialPort _serialPort = new SerialPort()
{
PortName = "COM5",
BaudRate = ,
DataBits = ,
StopBits = StopBits.One,
Parity = Parity.None
};
[TestMethod]
public void TestMethod1()
{
_serialPort.Open();
byte[] sendedData = { 0x01, 0x02, 0x03, 0x1a, 0x2a, 0x1a };
_serialPort.DataReceived += ComReceive;
_serialPort.Write(sendedData, , sendedData.Length);
Thread.Sleep();
//foreach (byte b in _receivedDataPacket)
//{
// Console.WriteLine(b);
//}
Console.WriteLine(_count);
}
private void ComReceive(object sender, SerialDataReceivedEventArgs e)
{
//if (e.EventType == SerialData.Eof)
//{
// //特殊处理
//}
_count++;
_receivedDataPacket = new byte[_serialPort.BytesToRead];
_serialPort.Read(_receivedDataPacket, , _receivedDataPacket.Length);
// 协议解析...
}
}

串口助手数据:

.NET 串口通信中断接收,包含0X1A(作为EOF)

测试结果:

.NET 串口通信中断接收,包含0X1A(作为EOF)

上一篇:jquery属性选择器(匹配具有指定属性的元素)


下一篇:ubuntu常用命令解释