I'm attempting to write a bash script that should be able to collect data from ttyS0 and put in a variable.
I need to talk with a device on the serial line, that is a module able to receive AT commands that I can send with echo > ttyS0 and capture the related answer in a variable. It's possible to do this without store the received answer in a variable (i.e. cat /dev/ttyS0 & ), but if I try to put this data in a variable (i.e. VAR=$(cat /dev/ttyS0 &) it doesn't work (in VAR I can not find anything after the answer of the module).
What I'm able to do "by hand" via gnome terminals (I'm working with a Ubuntu distribution) is the following:
From a GNOME terminal that I call (A), I run (as root)
# VAR=$(cat /dev/ttyS0)
This command doesn't return the root prompt
#, because probably
cat /dev/ttyS0is running and waiting for input.
From another GNOME terminal that I call (B), I run
# echo -en "hello in VAR\r" > /dev/ttyS0
hello in VARstring should go to
/dev/ttyS0and put by cat in
Then from (B):
# killall cat
From GNOME terminal (A) I can see that the prompt (
Finally from GNOME terminal (A):
# echo "$VAR"
and I receive the
hello in VARstring.
I tried to implement this via bash script in this way:
#!/bin/bash killall cat BASHTESTS_DIR=/root/Desktop/Tips_tricks_tutorials/bash_scripting cd $BASHTESTS_DIR echo "before VARcat_dev_ttyS0" VAR=$(cat /dev/ttyS0) echo "after VARcat_dev_ttyS0" echo -en "hello in VAR\r" > /dev/ttyS0 sleep 2 killall cat echo "content of VAR: $VAR" exit 0
but the script stops after
echo "before VARcat_dev_ttyS0"
How can I implement what I want or what I'm able to do with two GNOME terminals?