C#的SerialPort速度速度、SerialPort

2023-09-04 02:30:30 作者:眉眼如初

我开发了一些监控工具某种协议基于串行通信。

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种帧。他们有1字节,3字节,6个字节,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.

有关一开始我收到第一次除权后包装。 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.

Revision1:

Revision1:

我想笔直的路:

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: 这是code使用的踩踏方式的一部分:

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.

问候 MarekK

推荐答案

也许你应该阅读的第一,因为这个错误,是因为有DataReceived事件检索不火的时候,它依然不动的框架3.0 / 3.5。此外,你应该读的东西这里这个海报谁报告给Microsoft问题时,他使用它有问题,在VS2008 / .NET 3.5 ......我不能说,如果这是真的还是假的......但东西值得牢记约......与微软确实有其引入.NET版本了一定的难度与.NET版本的2

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

您可能会更好使用第三​​方code来处理串行通讯进行调查,通常情况下,那些被管理的世界之外编写的,即非托管使用大量的P /调用的。

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 提供其生产比后肢的code较好的效果。

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

希望这会有所帮助, 诚挚的问候, 汤姆。

Hope this helps, Best regards, Tom.