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.
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: