I have been stuck on a project for a while now.
I have a loop in my C++ project on my Mac that sends strings to Arduino.
For each string, Arduino does some stuff with it and answers with another string (OK or KO).
So far, I used fopen, fprintf and fscanf to open the port, send strings to Arduino and read the answer. But I have to wait (put my main thread to sleep) to give time to Arduino to process the input string and get the answer. Translated to code, I use sleep between fprintf and fscanf.
My question is: Is there a way to detect when Arduino replies? This way I could avoid wasting time because my main thread would start again as soon as Arduino replies...
Sorry I'm not at all conversant with macs but is there no way to hook onto the SERIAL_DATA_ARRIVAL event. A method in Windows would be WaitCommEvent() but I wouldn't know what you'd use on a mac.
Ok. Thanks for your answer anyway
I guess I'll just do a loop with a fscanf until I get something... And I'll keep looking for a more "elegant" solution.
Gujie:
So far, I used fopen, fprintf and fscanf to open the port, send strings to Arduino and read the answer. But I have to wait (put my main thread to sleep) to give time to Arduino to process the input string and get the answer.
You don't say so, but I wonder if fclose is also part of that process ???
You should only open the port once and then keep it open until you are finished using your Arduino because the Arduino resets every time the PC opens the serial port.