Hello,
I am writing an interface to a peripheral.
When getting data from the peripheral I send data through USB. the device then sends a reply containing the requested data.
As the device can also send data without me requesting it I set up a seperate thread (hi prio) to receive messages.
The transaction then goes as follow:
The send function sends a message to the peripheral, then loops waiting until the arrival of a reply is signalled. This is done by setting / reseting a boolean.
the receiver thread sets the boolean to signal that a specific reply came in.
Basically I have 2 problems.
1:
The wait loop in the send thread is now implemented with a while / Sleep(1) combination.
Sleep(1) often takes much much more than 1 ms (10 - 80ms) , thus killing the transaction performance.
Instead I would need some semaphore like solution that immediately wakes up the thread when the requested data has arrived and does not first serve a 100 other threads in between like Sleep() seems to do. How should I implement this? Can this be done using the Semaphore class?
2:
Both the receiver and the sender thread output debug data into a textbox. How should I prevent them from accessing it at the same time?
Currently I use a shared boolean/Sleep(1) construction, but it slows down things a lot
Start Free Trial