dimanche 1 mars 2015

attempting to put dynamic input data in a variable via cat method


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:




  1. 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/ttyS0 is running and waiting for input.




  2. From another GNOME terminal that I call (B), I run



    # echo -en "hello in VAR\r" > /dev/ttyS0


    The hello in VAR string should go to /dev/ttyS0 and put by cat in VAR




  3. Then from (B):



    # killall cat


    From GNOME terminal (A) I can see that the prompt (#) returns;




  4. Finally from GNOME terminal (A):



    # echo "$VAR"


    and I receive the hello in VAR string.




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?



Aucun commentaire:

Enregistrer un commentaire