C# 串口速度
我正在为某种基于串行通信的协议开发一些监控工具。
串口波特率=187,5kb 我使用 System.IO.Ports.SerialPort 类。
该协议有4种帧。它们有1字节、3字节、6字节、10-255字节。 我可以与他们合作,但收到邮件太晚,无法回复。
一开始,我收到的是前货后的第一个包装。 96ms(太晚了),并且包含大约1000B。这意味着 20-50 帧(太多,太晚了)。 后来它的工作更加稳定,3-10Bytes,但仍然为时已晚,因为它包含1-2帧。当然1帧还可以,但是2帧就太晚了。
你能指点我如何更可靠地处理它吗?我知道这是可能的。
修订1:
我尝试了直接方式:
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (!serialPort1.IsOpen) return;
this.BeginInvoke(new EventHandler(this.DataReceived));
}
和后台工作人员: 还有......新的胎面(阅读) 而且……总是一样。太晚了,太慢了。 我是否必须返回 WinApi 并导入一些 kernel32.dll 函数?
修订版 2: 这是以 Treading 方式使用的代码的一部分:
int c = serialPort1.BytesToRead;
byte[] b = new byte[c];
serialPort1.Read(b, 0, c);
我猜这是 SerialPort 类内部流使用的一些问题。或者一些同步问题。
修订版 3: 我不会同时使用两者!我只是尝试了不同的方法。
问候 马雷克
I am developing some monitoring tool for some kind of protocol based on serial communication.
Serial BaudRate=187,5kb
I use System.IO.Ports.SerialPort class.
This protocol has 4 kinds of frames. They have 1Byte,3Bytes,6Bytes, 10-255Bytes. I can work with them but I receive them too late to respond.
For the beginning I receive first packed after ex. 96ms (too late), and it contains about 1000B. This means 20-50 frames (too much, too late).
Later its work more stable, 3-10Bytes but it is still too late because it contains 1-2 frames. Of Course 1 frame is OK, but 2 is too late.
Can you point me how can I deal with it more reliable? I know it is possible.
Revision1:
I tried straight way:
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (!serialPort1.IsOpen) return;
this.BeginInvoke(new EventHandler(this.DataReceived));
}
And Backgroud worker:
And ... new Tread(Read)
and... always the same. Too late, too slow.
Do I have to go back to WinApi and import some kernel32.dll functions?
Revision 2:
this is the part of code use in the Treading way:
int c = serialPort1.BytesToRead;
byte[] b = new byte[c];
serialPort1.Read(b, 0, c);
I guess it is some problem with stream use inside SerialPort class. Or some synchronization problem.
Revision 3:
I do not use both at once!! I just tried different ways.
Regards
MarekK
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您应该首先阅读此内容,因为此错误与 DataReceived 事件有关有时不会触发,并且在 Framework 3.0/3.5 中仍未修复。另外,您应该阅读一些内容 这里关于这位发帖者,当他在 VS2008/NET 3.5 下使用它时遇到问题时,向 Microsoft 报告了该问题...我不能说这是否属实...但值得记住的事情...微软在 .NET 版本上确实遇到了一定的困难,该版本被引入到 .NET 版本 2 中。
当时,我使用的是 .NET 1.1,我遇到了使用 VB6 ActiveX 附带的版本的建议控制,根据互联网上的来源,它是最好的串行通信控制!!!
您可能最好研究使用第三方代码来处理串行通信,通常是在托管世界之外编写的代码,即使用大量 p/invoke 的非托管代码。
我尝试使用 MSDN 文章此处中找到的 John Hind 版本,我确实发现这非常令人反感,因为无法异步执行此操作,而且这是一种可怕的编码体验。
我最终使用了 Franson 产生的结果比 Hind 的代码更好。
希望这有帮助,
此致,
汤姆.
Maybe you should read this first, as this bug has to do with DataReceived Event which does not fire at times, and it is still not fixed in Framework 3.0/3.5. Also, you should read something here about this poster who reported the problem to Microsoft when he had issues using it, under VS2008/NET 3.5...I cannot say if that is true or not...but is something worth keeping in mind about...and Microsoft did have a certain amount of difficulty with the .NET version which was introduced into .NET version 2.
At the time, I was using .NET 1.1, I came across suggestions to use the version that came with VB6's ActiveX control, which according to sources on the internet, that it was the best serial comms control!!!
You might be better to investigate using a third party code to handle the serial communications, usually, the ones written outside of the managed world, i.e. unmanaged using lots of p/invokes.
I have tried using John Hind's version found in the MSDN article here, which I did find very off-putting as there was no way to do it asynchronously, plus it was a horrible coding experience..
I did end up using one supplied by Franson which produced better results than Hind's code.
Hope this helps,
Best regards,
Tom.