Linux 中单声道的串行端口不响应 DataReceived 事件
我正在编写一个应用程序,它使用单声道中的 SerialPort 类公开的串行端口。到目前为止,我所写的内容在 Windows 中运行得很好,但是在 Linux 中,从未输入 DataReceived 事件处理程序,因此我无法从我的设备接收任何数据。我已声明事件处理程序如下:
comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
基本上我正在探索良好的跨平台选项,这是一个破坏性的事情。关于如何解决这个问题或发生了什么事有什么建议吗?
编辑- 我还应该指出,我已经使用其他应用程序在 Linux 上测试了串行端口和设备,并且一切似乎都正常工作。
I am writing an app that uses the serial port exposed by the SerialPort class in mono. What I have written so far works perfect in windows, however in linux the DataReceived event handler is never entered, so I cannot receive any data form my device. I have declared the event handler as follows:
comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
Basically I am exploring good cross-platform options and this is a deal-breaker. Any advise on how to fix this or what is going on?
Edit-
I should also point out that I have tested the serial port and device on linux with other applications and all appears to be working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许它最后改变了,但据我所知,Mono的串口目前还没有实现事件。您必须创建任何风格的另一个线程才能从串行端口读取数据,这是以阻塞方式发生的。尝试一下,看看是否有效。
Maybe it has changed lastly, but as far as I know, events are not currently implemented in Mono's serial port. You have to make another thread in any flavour to read data from serial port, which happens in blocking manner. Try it and tell if it worked.
在 Antanas Veiverys 博客上,您可以找到两种可能的方法来解决该问题。
(2012) 通过调整单声道源代码。
http://antanas.veiverys.com/enabling-serialport-datareceived- event-in-mono/
(2013) 通过不触及单声道源代码,而是在派生类中解决问题。
http://antanas.veiverys.com/单串行端口数据接收事件解决方法使用派生类/
(2014)
不过,我鼓励您阅读 Ben Voigts 的博客文章,其中他忽略使用 DataReceivedEvent,而是使用 BaseStream 异步 BeginRead/EndRead 函数从串行端口读取数据。
http://www.sparxeng .com/blog/software/must-use-net-system-io-ports-serialport#comment-840
实现和使用给定的代码示例适用于 Windows/Unix,所以我有已测试。而且它比以线程方式使用阻塞 Read() 函数更优雅。
On Antanas Veiverys blog you can find two possible ways to solve it.
(2012) By adjusting the mono source code.
http://antanas.veiverys.com/enabling-serialport-datareceived-event-in-mono/
(2013) By not touching the mono source but solving the issue in a derived class.
http://antanas.veiverys.com/mono-serialport-datareceived-event-workaround-using-a-derived-class/
(2014)
However, I encourage you to read Ben Voigts blog post where he ignores using the DataReceivedEvent and instead used the BaseStream async BeginRead/EndRead functions to read data from the serial port.
http://www.sparxeng.com/blog/software/must-use-net-system-io-ports-serialport#comment-840
Implementing and using the given code sample works on both Windows/Unix, so I have tested. And it is more elegant than using the blocking Read() function in a threaded fashion.
mono 不支持串行端口事件。
它显示在 mono 网站
mono does not support Event for serialport.
It is shown on mono's website
我对 SerialPort.DataReceived 有同样的问题。康拉德的建议。
I have the same problem with SerialPort.DataReceived. Konrad's advice.