I am working with Linux file descriptor at the moment to communicate with an external device via serial port, and I wonder if it is possible for me to duplicate the result returned the device.
In details, I used file descriptor number 5 to read and write to the serial port /dev/ttyACM0 in a raspbian device.
exec 5<>"/dev/ttyACM0"
In the background, I ran a function to continuously read from the port whenever a response was present to decide the next step.
function __processing__(){
local line
while read line<&5; do
... some processing ...
... echo $line > "log.txt"
done
}
And it has been working alright if I just sent single command to the serial port and let the background function to process the response. The issue appeared when I start working with other software to transfer data using XMODEM protocol, in the foreground.
sx -vv -k "./firmware_update.bin" >&5 <&5
The process kept timing out on ACK, despite the ACK responses recorded in the log file. So I suspected that it was because of the background process that read from FD5, leaving sx nothing. I then tried to remove the background process and run only the sx command, and my suspicion was confirmed when it successfully sent all data packages.
But then I don't have any recorded response from the serial port to actually know what to do next.
So my question is: Is there a way to rewrite the background function so that it can still read the response, without interfering with sx? I know that we can duplicate data from stdout using tee command, can I achieve something similar with FD5 instead?
Thank you all in advance.