Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending modem AT command and parsing result

I would like to send AT command to my modem by using shell script and parse the result in order to verify if the OK is returned.

at=`echo -ne "AT+CFUN1=1\r\n" > /dev/ttyUSB0 | cat /dev/ttyUSB0`

What is the best way to parse the at1 variable and extract "OK" or "ERROR" otherwise ?

like image 526
ogs Avatar asked Apr 24 '15 16:04

ogs


1 Answers

It is absolutely possible to send AT commands to a modem and capture its output from the command line like you are trying to do, however not by just using plain bash shell scripting. Which is why I wrote the program atinout specifically to support scenarios like you ask about.

Test like the following:

MODEM_DEVICE=/dev/ttyUSB0

MODEM_OUTPUT=`echo AT | atinout - $MODEM_DEVICE -`
case $MODEM_OUTPUT
in
        *OK*)
                echo "Hurray, modem is up and running :)"
                ;;
        *)
                echo "Oh no! Something is not working :("
                ;;
esac

If you intend to parse the output in any more sophisticated way you should save the output to a file instead by giving a filename instead of the last - and read that.

like image 77
hlovdal Avatar answered Oct 28 '22 11:10

hlovdal