如何正确使用SerialPort.DiscardInBuffer? [英] How to correctly use SerialPort.DiscardInBuffer?

查看:5411
本文介绍了如何正确使用SerialPort.DiscardInBuffer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写其内容从串行设备以非常快的速度的应用程序。串行端口对象然而,失败约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 后,每 DataReceived 事件,这也似乎已经解决了这个问题。但该方法消耗了大量的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.

在如何应 DiscardInBuffer 中使用,我使用它在正确的情况下对我的具体问题?

When and how should DiscardInBuffer be used, and am I using it in the correct context for my particular problem ?

编辑:

实施 ErrorReceived 活动结束后,返回的事件数据所指示的事件类型是一个RXOver。

After implementing the ErrorReceived event, the event data returned indicated the event type was an "RXOver".

推荐答案

经过调查,看来我的问题更多的是根本性的问题。由于数据是在一个热速度涌入,所需要的缓冲器的SerialPort要清除或连续到prevent的RXOver错误处理。我通过阅读到另一个缓冲区中的 DataReceived 事件中做到了这一点,并在另一个单独的线程中处理它。

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.

这是我的理解 DiscardInBuffer 只能有选择地使用以清除初始化目的,例如打开一个端口之前端口的内容。清除驱动器缓冲的过程中确实需要一些时间才能完成,因此应明智地用在性能导向的应用程序。

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.

这篇关于如何正确使用SerialPort.DiscardInBuffer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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