我编写了一个应用程序，它以非常快的速度从串行设备中读取数据.然而，串行端口对象在连续运行约 20 分钟后未能触发
DataReceieved 事件.以编程方式断开和重新连接串行允许事件再次工作，但仅再持续 20 分钟.
I've written an application which reads from a serial device at a very fast rate. The serial port object however, fails to fire the
DataReceieved event after about 20 min of continuous operation. Disconnecting and reconnecting the serial programmatically allows the event to work again but for only another 20 min.
DiscardInBuffer，这似乎已经解决了问题.但是这种方法会消耗大量的 cpu 时间，并且会降低应用程序的性能.MSDN 提到丢弃串行驱动程序接收缓冲区中的数据"方法，但没有建议何时使用.
I tried using
DiscardInBuffer after every
DataReceived event and this has appeared to have solved the problem. But the method consumes a lot of cpu time and is degrading the performance of the application. MSDN mentions that the method "Discards data from the serial driver's receive buffer.", but fails to suggest when it should be used.
When and how should
DiscardInBuffer be used, and am I using it in the correct context for my particular problem ?
After implementing the
ErrorReceived event, the event data returned indicated the event type was an "RXOver".
经过更多调查，我的问题似乎是一个更根本的问题.由于数据以极快的速度涌入，因此需要连续清除或处理 SerialPort 缓冲区以防止RXOver"错误.我通过在
After more investigations, it appears my problem was more a fundamental issue. Since the data was flooding in at a hot pace, the SerialPort buffer needed to be cleared or processed continuously to prevent the "RXOver" error. I achieved this by reading into another buffer during the
DataReceived event and processed it in another separate thread.
From my understanding the
DiscardInBuffer should only be used selectively to clear the contents of the ports for initialisation purposes, such as before opening a port. The process of clearing the driver buffer does take some time to complete and therefore should be used wisely in a performance orientated application.