none
Using the DataReceivedHandler - C# Serial Port RRS feed

  • Question

  • Hi, I have successfully managed to implement an interface in order to continuously receive data through a Serial Port when available using the public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) method.

    However I noticed that there is quite a lot of latency that makes my software useless in real time. I am using a baud rate of 9600.

    public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e){
                dataLine = ComPort.ReadLine();
                try
                {
                    if(dataLine == "f")
                    {
                        frequency = ComPort.ReadLine();
                        Console.WriteLine(frequency);
                    }
                catch(TimeoutException)
                {
                    txtCarCount.Text = "Timeout Exception";
                }
    
            }

    The above is the method that I implemented. Any ideas on how to reduce this delay?

    Thanks


    • Edited by lukiss16 Sunday, March 4, 2018 7:42 PM
    Sunday, March 4, 2018 7:41 PM

Answers

  • Hello lukiss16,

    Hans Passant has view on low latency with serial communicate. The DataReceived event is delivered asynchronously by a threadpool thread that performs a blocking wait with WaitCommEvent(), you can't go faster than that.

    https://stackoverflow.com/questions/2718450/low-latency-serial-communications-in-net

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei HuModerator Monday, March 5, 2018 6:01 AM
    • Marked as answer by lukiss16 Monday, March 5, 2018 11:46 AM
    Monday, March 5, 2018 6:00 AM
    Moderator

All replies

  • Hello lukiss16,

    Hans Passant has view on low latency with serial communicate. The DataReceived event is delivered asynchronously by a threadpool thread that performs a blocking wait with WaitCommEvent(), you can't go faster than that.

    https://stackoverflow.com/questions/2718450/low-latency-serial-communications-in-net

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Fei HuModerator Monday, March 5, 2018 6:01 AM
    • Marked as answer by lukiss16 Monday, March 5, 2018 11:46 AM
    Monday, March 5, 2018 6:00 AM
    Moderator
  • Hi thanks a lot for your answer. I will have a look at that.

    Are there any alternatives to the DataReceived event in order to achieve faster speeds? At a point I used a while loop in order to continuously read the Serial Port however then I'd need a way to get out of it and also execute other events according to the buttons. 

    Monday, March 5, 2018 10:18 AM