0

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.

Verax
  • 2,409
  • 5
  • 27
  • 42
Jardo421
  • 85
  • 1
  • 3
  • 8

1 Answers1

2

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

Community
  • 1
  • 1
James Black
  • 41,583
  • 10
  • 86
  • 166