Read serialport data in under 2ms in C#

I need to receive 3 characters every 2 milliseconds in a loop. I am using SerialPort.Read() but it takes 5 to 6 miliseconds to read incoming data. Is there possible way to do it? Thanks a lot.

Answers


You may want to call the serial port in C++, https://stackoverflow.com/a/15795522/67566

Then you can call that from C#, https://msdn.microsoft.com/en-us/library/ms235281.aspx

I expect this will be faster, but you will have a slow down in getting the data from C++ -> C#, and your window of 2-3 ms is very tiny.

You can get the data in that time, I expect, from C++, but you may need to buffer it, so I would put a timestamp with each piece of data, and when you call it from C# you may need to return more than one piece, and then process them.

In C++ DLL you will want to have a separate thread for reading from the serial port.

You will want to use the thread class, https://msdn.microsoft.com/en-us/library/hh920601.aspx and detach it after creation.

And finally you will want to use a thread safe queue, so you can look at these two links and pick which one you like better:

https://juanchopanzacpp.wordpress.com/2013/02/26/concurrent-queue-c11/

C++11 thread-safe queue


Need Your Help

How do I generate points that match a histogram?

c algorithm histogram probability numerical-analysis

I am working on a simulation system. I will soon have experimental data (histograms) for the real-world distribution of values for several simulation inputs.

iOS ARC block retain cycle

ios automatic-ref-counting block retain-cycle

Can someone confirm if the block below is turning into a retain cycle please? Please note the block is being called by SampleClass2 not SampleClass1.