1

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!

1
  • One thing I forgot to mention, the device also support AT commands, and the device responds to AT commands properly running from shell script, with either bash or dash shell. Jul 29 '19 at 9:20
1

Ok, I don't know why, but after I changed to use bash shell, the script works. The default shell is dash.

2
  • Probably because echo -ne is not portable. Use printf instead.
    – muru
    Jul 29 '19 at 4:59
  • Thanks @muru for suggestion. Unfortunately I don't know what I did, but now even bash doesn't work. I also tried printf but same result. Could it be due to the device itself? One thing I forgot to mention, the device also support AT commands, and the device responds to AT commands properly running from shell script, with either bash or dash shell. Jul 29 '19 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.