【发布时间】:2014-06-04 15:20:23
【问题描述】:
我有一个类,它使用 C# 中的 DataReceived 事件处理程序从串行端口读取。当我收到数据时,我知道标头将有 5 个字节,所以我不想对数据做任何事情,直到我至少有这个。我当前的代码如下:
while (serialPort.BytesToRead<5)
{
//Do nothing while we have less bytes than the header size
}
//Once at least 5 bytes are received, process header
据我了解,这段代码是阻塞的,需要改进。我正在寻找有关如何执行此操作的建议。 DataReceived 事件处理程序中的另一个事件处理程序是否合适?
【问题讨论】:
标签: c# asynchronous serial-port blocking