C # .Net Serial DataReceived Event Response Too Slow for High Speed ​​Data

I installed SerialDataReceivedEventHandler, with a form-based program in VS2008 express. My serial port is configured as follows:

115200, 8N1

Dtr and Rts included

ReceivedBytesThreshold = 1

I have a device with which I interact with BlueTooth, from USB to Serial. Hyper terminal receives only fine data at any data rate. Data is regularly sent in 22 byte packets. This device has an adjustable data rate. At low data rates, 10-20 Hz, the code below works fine, no problem. However, when I increase the data transfer rate to 25 Hz, one call starts to receive reusable packets. I mean, there must be an event trigger for every incoming packet. With higher output speeds, I immediately checked the buffer size (BytesToRead command) when the event is fired, and then there are several packets in the buffer. I think that the event fires slowly, and by the time the code is reached, more packets are in the buffer. One test I doshows how many times the event is a trigger per second. At 10 Hz, I get 10 event triggers, awesome. At 100 Hz, I get something like 40 event triggers, not good. My goal for a data rate of 100 Hz is acceptable, preferably 200 Hz and optimal 300 Hz. This should work, because even at 300 Hz, it is only 52,800 bps, less than half the set baud rate of 115200. Anything I'm looking at?Anything I'm looking at?Anything I'm looking at?

    public Form1()
    {
        InitializeComponent();
        serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);            
    }


    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        this.Invoke(new EventHandler(Display_Results));
    }


    private void Display_Results(object s, EventArgs e)
    {
        serialPort1.Read(IMU, 0, serial_Port1.BytesToRead);
    }
+5
4

USB-? FTDI USB . , IN OUT, , , . 16 . 2 , . USB- , 2 . . .

+4

() DisplayResults?

MessageLoop, .

, DataReceived() () .

, .

+3

ReceivedBytesThreshold = 22, , , 22 . , 22. .

, , , , , , 12 ? 12 , .

1, , 1 . , Henk.

Note that the DataReceivedEvent does not know what you think is a data packet, of course. It just fires when there are available bytes. The developer must collect these bytes into a meaningful message or packet.

+1
source

The problem is the resulting data handler.

I ran a separate thread with a loop while(true)and serial.ReadLine()everything works fine.

using System.Threading;

Thread readThread = new Thread(Read);
readThread.Start();

Hope someone else doesn't have to spend 3 hours on this.

0
source

All Articles