I want to read messages sent from Arduino via FTDI (serial) interface in a simple C or C ++ program under Linux. Arduino sends two "header" characters, a command byte, followed by several bytes of data depending on the command.
My first attempt was to simply poll the data using open () and read (), but this results in approximately 12% CPU usage. It is not like doing something.
Secondly, I read about libevent about an implemented event loop that fires an event when data is present in a file descriptor. My processor usage was almost nothing, but I could not read the whole message before another event was triggered. Events did not fire when an entire message was received, but as soon as any data was available in the file descriptor. Looking at it more, it was obvious that it would not work the way I wanted it to. This is my event code: http://pastebin.com/b9W0jHjb
Thirdly, I implemented a buffered event with libevent. It seemed to work a little better, but still split some messages. My event code is: http://pastebin.com/PQNriUCN
Fourth, I dropped libevent and tried out the Boost ASIO class. The example I used was http://www.webalice.it/fede.tft/serial_port/serial_port.html . Everything seemed to be in order, but the "event loop" was "while (1) {}", due to which the processor load increased again. The loop simply checks the error state, and sequential reads occur in a callback in another thread. I added usleep (1) to the while loop, and it brought my processor 2%, which is good, but it still seems difficult for such an easy program.
Most libevent and even base epoll examples use TCP sockets, which seem to have nothing to do with serial port data.
, : ? ( Linux, C ++)