In my script I am trying to read data from several serial ports to coordinate several devices.
I can open file descriptors to the serial ports using exec 9</dev/ttyACM0 && exec 8</dev/ttyACM1
etc.
I was hopping I could then do something like select()
on these file descriptors to wait on data arriving, then process it with read 0<&9
or read 0<&8
depending on which one had received data. Unfortunately I cannot find an equivalent of select()
for Bash.
The closest I can find is using read -t 0 0<&9
to poll for data to be read. I don't like this as it requires a sleep
to prevent the script from consuming 100% the processor. Although sub second sleeps are possible e.g. while true; do sleep 0.01; done
this consumes ~4% of the processor on my system. I can knock it back to 0.1 seconds and reduce it to <1% but it still does not "feel" the right way to do it as it adds at least 100ms of additional delay to any coordination between the devices.
Are there any solutions to this? Is there a Bash select() equivalent?
Quoting Greg's Bash Wiki http://mywiki.wooledge.org/ProcessManagement
There is no shell scripting equivalent to the select(2) or poll(2) system calls. If you need to manage a complex suite of child processes and events, don't try to do it in a shell script. (That said, there are a few tricks in the advanced section http://mywiki.wooledge.org/ProcessManagement#advanced of this page.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With