C# 串行端口速度

C# serialPort speed(C# 串行端口速度)

本文介绍了C# 串行端口速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为某种基于串行通信的协议开发一些监控工具.

I am developing some monitoring tool for some kind of protocol based on serial communication.

串行波特率=187,5kb我使用 System.IO.Ports.SerialPort 类.

Serial BaudRate=187,5kb I use System.IO.Ports.SerialPort class.

此协议有 4 种帧.它们有 1Byte、3Bytes、6Bytes、10-255Bytes.我可以与他们合作,但我收到他们来得太晚,无法回复.

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.

一开始我收到的第一个包装是在 ex 之后.96ms(为时已晚),它包含大约 1000B.这意味着 20-50 帧(太多,太晚了).后来它的工作更稳定,3-10Bytes 但仍然为时已晚,因为它包含 1-2 帧.当然 1 帧还可以,但是 2 就太晚了.

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.

修订1:

我直接尝试了:

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
       if (!serialPort1.IsOpen) return;
       this.BeginInvoke(new EventHandler(this.DataReceived));
}

和后台工作者:并且......新的胎面(阅读)和......总是一样的.太晚了,太慢了.是不是必须回到 WinApi 并导入一些 kernel32.dll 函数?

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?

修订版 2:这是Treading方式中代码使用的一部分:

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);

我猜这是在 SerialPort 类中使用流的一些问题.或者一些同步问题.

I guess it is some problem with stream use inside SerialPort class. Or some synchronization problem.

修订版 3:我不会同时使用两个!!我只是尝试了不同的方法.

Revision 3: I do not use both at once!! I just tried different ways.

问候马立克

推荐答案

也许你应该看看这个 首先,因为这个错误与 DataReceived 事件有关,它有时不会触发,并且在 Framework 3.0/3.5 中仍未修复.另外,您应该阅读一些 这里关于这张海报,当他在 VS2008/NET 3.5 下使用它时遇到问题时向微软报告了这个问题......我不能说这是否属实......但值得记住的是...微软在 .NET 版本 2 中引入的 .NET 版本确实存在一定的困难.

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.

当时,我正在使用 .NET 1.1,我遇到了使用 VB6 的 ActiveX 控件附带的版本的建议,根据 来源s在互联网上,它是最好的串行通讯控件!!!

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!!!

您可能会更好地调查使用第三方代码来处理串行通信,通常是在托管世界之外编写的那些,即使用大量 p/invoke 的非托管.

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.

我尝试使用 MSDN 文章 这里,我确实觉得这很令人反感,因为没有办法异步执行它,而且这是一种可怕的编码体验..

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..

我最终使用了由 Franson 提供的一个,它比 Hind 的代码产生了更好的结果.

I did end up using one supplied by Franson which produced better results than Hind's code.

希望这会有所帮助,最好的祝福,汤姆.

Hope this helps, Best regards, Tom.

这篇关于C# 串行端口速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:C# 串行端口速度

基础教程推荐