I have a USB serial device connected to my Linux VM (Ubuntu 18.04LTS). It accepts byte streams in certain format and will return some content. To read from the device, I have written the following script:
#!/bin/sh
DATA="$1"; shift
stty -F /dev/ttyUSB0 115200 raw -echo
cat /dev/ttyUSB0 > /tmp/ttyDump.dat &
PID=$!
sleep 1s
echo -ne "$DATA" > /dev/ttyUSB0
sleep 1s
kill $PID
hexdump -ve '1/1 "%.2x"' /tmp/ttyDump.dat
If I execute line by line, I'm able to get the reply from device in ttyDump.dat. However If I run the script, the ttyDump.dat is empty.
I also tried with FD:
exec 3</dev/ttyUSB0
cat <&3 >/tmp/ttyDump.dat&
But same result.
Any clue?
Thanks!