C # serialPort Speed

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

Serial BaudRate = 187.5kb I am using the System.IO.Ports.SerialPort class.

This protocol has 4 types of frames. They have 1Byte, 3Bytes, 6Bytes, 10-255Bytes. I can work with them, but I get them too late to answer .

For starters, I get first packaged after ex. 96 ms (too late) and it contains about 1000B. This means 20-50 frames (too many, too late). Later, his work was more stable, 3-10Bytes, but still too late, because it contains 1-2 frames. Of course, 1 frame is fine, but 2 is too late.

Can you tell me how I can deal with it more reliably? I know this is possible.

Revision1:

I tried directly:

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

And a Backgroud worker: And ... a new tread (Read) and ... always the same. Too late, too slow. Should I go back to WinApi and import some kernel32.dll functions?

Revision 2: this is part of the use of code in the tread method:

int c = serialPort1.BytesToRead;
byte[] b = new byte[c];
serialPort1.Read(b, 0, c);

I assume this is some problem using the thread inside the SerialPort class. Or some synchronization problem.

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

Relations MarekK

+3
source share
1 answer

, , DataReceived, , - Framework 3.0/3.5. , - , Microsoft, , VS2008/NET 3.5... , ... , ... Microsoft .NET-, .NET 2.

.NET 1.1, , VB6 ActiveX, source , !!!

, , , , , .. , p/invokes.

, MSDN , , , .

Franson, , Hind.

, , , .

+1

Source: https://habr.com/ru/post/1715705/


All Articles