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.
-
Is it looping as fast as possible? If so then it's likely the serial connection that's too slow, and not your code. – Firearrow5235 Mar 24 '16 at 21:08
-
SerialPort.Read() alone takes 5 to 6 ms and it doesn't even start to process incoming data. – Jardo421 Mar 24 '16 at 21:12
1 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/

- 1
- 1

- 41,583
- 10
- 86
- 166