Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
1)
C#
SerialDataReceivedEventHandler


event or simply loop checking the data received or not

2)
C#
 m_objSerialPort.Write(array, 0, length);                             
 while ((iBytesRead = m_objSerialPort.BytesToRead) 
 {
 Thread.Sleep(ID_DELAY); 
}



Which is more fastest way to get result. I want communicate to a serial device within 15ms interval using C#. Which is the best way to read data from serial port.
Posted
Updated 25-Feb-16 1:24am

I guess that:
  • There is no noticeable performance difference between method (1) and (2).
  • There is no way to guarantee the within 15ms responsivity.
 
Share this answer
 
You can't guarantee that time frame with windows: it's a preemptive multitasking OS, so it decided when your app gets to run, and for how long. If it decides to suspend you at the wrong time, then you will miss the time slot and there is nothing you can do about it.

My personal preference would be the DataRecieved event handler (especially since the code you show for polling will probably never exit, unless you have a separate thread actually reading the data - in which case why use your code at all?)

Polling can be useful when you set upo a background thread to continually monitor the port for data, and read it, checking for complete messages before passing those to a "higher" thread for actual processing. But it doesn't look like the code you show! :laugh:
 
Share this answer
 
First option
SerialDataReceivedEventHandler

Event handler is there to prevent consuming resources while continuously checking the next char.
 
Share this answer
 
Use the event!
Your other approach is blocking the thread for nothing!
(and 15 ms is not fast!)
 
Share this answer
 
Umm, the rule I have is always the event handler as polling can result in the locking the system. Griff, Peter are right. If it's a simple thing you don't want to get too bogged down in Windows guts! Also 15 mS as Peter said is not really to quick if the messages are small i.e. you are getting over 256K it would be fine.
 
Share this answer
 
Use the event but if you are using net 4.5 or newer you may use async, await: Asynchronous Programming with Async and Await (C# and Visual Basic)[^] and Simple Async Await Example for Asynchronous Programming | Stephen Haunts { Coding in the Trenches }[^].
You do this by calling ReadAsync on the BaseStream object.
This is from working code. You should be able to see what is happening.

C#
try
{
    while ((drb_args.NumBytes = await _serialPortNet.BaseStream.ReadAsync(drb_args.BytesOut, 0, BufferLen, cancellationToken)) != 0)
    {

        cancellationToken.ThrowIfCancellationRequested();

        if ((drb_args.NumBytes > 0))
        {
            // Do something here.
        }

    }
}
catch (Exception ex)
{
    if (ex.GetType().Name == "TaskCanceledException")
    {
        stop();
    }
}


As for actual performance this will depend on other factors as already pointed out.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900