是什么原因导致了.NET SerialPort类DataReceived事件检索火? [英] what causes the .NET SerialPort class DataReceived event to fire?

查看:137
本文介绍了是什么原因导致了.NET SerialPort类DataReceived事件检索火?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从MSDN文档,该事件DataReceived不一定每字节触发一次明白了。

I understand from the MSDN docs that the event DataReceived will not necessarily fire once per byte.

但没有人知道究竟是什么导致了事件触发机制?

But does anyone know what exactly is the mechanism that causes the event to fire?

是否接收到每个字节的重新启动已达到一个计时器,说字节之间10毫秒,事件触发之前?

Does the receipt of each byte restart a timer that has to reach, say 10 ms between bytes, before the event fires?

我问,因为我想编写一个应用程序读取XML数据从一个串行端口进入。

I ask because I'm trying to write an app that reads XML data coming in from a serial port.

由于我的笔记本电脑没有串口,我使用的是虚拟串口仿真器。 (我知道,我知道 - 我不能做任何事情ATM)

Because my laptop has no serial ports, I use a virtual serial port emulator. (I know, I know--I can't do anything about it ATM).

当我通过模拟端口的数据到我的应用程序,该事件触发一次,每个XML记录(大约1500字节)。完善。但是,当一个同事在另一间办公室试图用两台电脑通过一个实际的电缆时,DataReceived事件检索大火反复连接,每10个左右字节的XML,这完全抛出了应用程序后。

When I pass data through the emulated port to my app, the event fires once for each XML record (about 1500 bytes). Perfect. But when a colleague at another office tries it with two computers connected by an actual cable, the DataReceived event fires repeatedly, after every 10 or so bytes of XML, which totally throws off the app.

推荐答案

DataReceived能火在任何时候一个或多个字节就可以读取。确切地,当它被触发取决于操作系统和驱动程序上,还可以将有被接收的数据和事件被触发在.NET之间有一个小的延迟。

DataReceived can fire at any time one or more bytes are ready to read. Exactly when it is fired depends on the OS and drivers, and also there will be a small delay between the data being received and the event being fired in .NET.

您应该不依靠DataReceived事件控制流的时机。

You shouldn't rely on the timing of DataReceived events for control flow.

相反,解析底层协议,如果你还没有收到一个完整的消息,等待更多。如果收到多条信息,一定要保持在左侧额从分析的第一条消息,因为他们将是下一个信息的开始。

Instead, parse the underlying protocol and if you haven't received a complete message, wait for more. If you receive more than one message, make sure to keep the left overs from parsing the first message because they will be the start of the next message.

这篇关于是什么原因导致了.NET SerialPort类DataReceived事件检索火?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆