Skip to content
Advertisement

Sleep/block/wait unitil serial data is incoming

I have some live data which is streamed from a microcontroller via serial port to a Raspberry Pi (or for doing prototyping maybe to a PC) with Linux as OS. The data comes roughly every 100 ms.

I want to process this data, after receiving (checking of correctness and doing some calculation with some python scripts). However, I didn’t find a good way how to know if new data has come (expecting polling the port). I found already some solutions with sleeping, which isn’t a good solution for me (because I want to avoid polling). I also checked the serial library from Python and Watchdog (to watch over the device as file) but they seem to use polling as well if I got it right.

Is there any way to let sleep the whole program until new data on the serial port is available? In the sense of, sleeping (and literally do nothing; no NOPs or checking a condition) and only waking up if new data is incoming.

The language doesn’t matter, I would prefer Python here, but C or C++ as solution wouldn’t be any problem.

Advertisement

Answer

I was able to figure out a solution, rather with blocking than with sleeping. The inotify on Linux does exactly what I wanted, because the serial port can be dealt as file and thus any change to it will be monitored by inotify.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement