Unable to echo to serial port from a bash script?
by Atoss from LinuxQuestions.org on (#5NB9X)
I am working on a small bash script, which involves going through devices on several serial ports, sending them a message and listening for their response. What I am using so far is:
Code:for d in $(ls /dev | grep -E 'ttyUSB|ttyACM') ;
do
echo testing $d ;
device="/dev/$d ;
stty 9600 --file=$device; echo "" > $device ;
(read -n32 -t5 resp < $device ; echo $resp)&
read -p "" -t 0.5
echo "id" > $device ;
wait ;
done;The device on the serial port responds with an affirmative message if it receives "id" (terminated either by newline or \0), or a negative message if it receives anything else.
Unfortunately, when I launch the script, it seems like nothing is returned from the device - the "testing /dev/ttyUSB0" gets echoed, but after that, I just get an empty line, and get thrown back to the prompt. When I try to execute the same commands manually - namely the (read -n32 -t5 resp < /dev/ttyUSB0 ; echo $resp)& and then echo "id" > /dev/ttyUSB0, I do get a response, however, it is not always the affirmative one. So far it seems like the first attempt gets an affirmative, then the next one doesn't unless I echo anything else to the serial port before running read.
Some help in getting the script to work would be appreciated.


Code:for d in $(ls /dev | grep -E 'ttyUSB|ttyACM') ;
do
echo testing $d ;
device="/dev/$d ;
stty 9600 --file=$device; echo "" > $device ;
(read -n32 -t5 resp < $device ; echo $resp)&
read -p "" -t 0.5
echo "id" > $device ;
wait ;
done;The device on the serial port responds with an affirmative message if it receives "id" (terminated either by newline or \0), or a negative message if it receives anything else.
Unfortunately, when I launch the script, it seems like nothing is returned from the device - the "testing /dev/ttyUSB0" gets echoed, but after that, I just get an empty line, and get thrown back to the prompt. When I try to execute the same commands manually - namely the (read -n32 -t5 resp < /dev/ttyUSB0 ; echo $resp)& and then echo "id" > /dev/ttyUSB0, I do get a response, however, it is not always the affirmative one. So far it seems like the first attempt gets an affirmative, then the next one doesn't unless I echo anything else to the serial port before running read.
Some help in getting the script to work would be appreciated.