7

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 ?

leu
  • 2,051
  • 2
  • 12
  • 25
ogs
  • 1,139
  • 8
  • 19
  • 42
  • 4
    You cannot send AT command from the normal command line to a modem. You need to use something like `socat` or `minicom` to establish a serial connection with the modem. See: [**How to send AT commands to a modem in Linux?**](http://unix.stackexchange.com/questions/97242/how-to-send-at-commands-to-a-modem-in-linux) – David C. Rankin Apr 24 '15 at 16:49
  • You could also use [kermit](http://www.kermitproject.org/). – Elliott Frisch Apr 26 '15 at 16:17
  • 1
    I am using minicom to establish the serial connection to the board. Once modem started, I use microcom or socat to speak with it. However, It is possible to send AT comands thru this interface by using a simple echo, isn't ? – ogs Apr 27 '15 at 06:15
  • 1
    Before talk to the modem serial port, you should use the stty or setserial to set the baud rate of the SerialPort. – TonyHo Jul 08 '16 at 07:24

2 Answers2

8

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.

hlovdal
  • 26,565
  • 10
  • 94
  • 165
  • To get this working on my Raspberry Pi, I had to switch the source file from using '\r\n' to '\n'. Other than that this works very well! – ctag May 05 '18 at 21:25
  • Can atinout be used to send SMS for example? I know that it can be used for executing commands without arguments, butt can it be used when we need to type in the message and termination character for sending it? – Miloš Mladenović Feb 02 '21 at 15:50
0

I was unable to communicate with my Huawei E3372h-158 with @hlovdal's solution, so I rolled my own using expect and screen, in this case for reading the temperature sensors via ^CHIPTEMP?:

output=$(sudo expect <<EOF
set timeout 5
log_user 0                                                                                                                 spawn sudo screen /dev/ttyUSB0-
sleep 1
send "AT\x5ECHIPTEMP?\r"
expect "OK"
puts "\n-->\$expect_out(buffer)<--"
# Send C-a \ to end the session
send "\x01"
send "\x5C"
EOF
)
# Strip non-printable control characters
output=$(printf "$output" | tr -dc '[:print:]\n' )
printf "$output\n" | grep -P "^\^CHIPTEMP"

Hints and caveats: Set log_user 1 to get the output of screen. Not sure screen works in all cases as it produces some non-printing characters and perhaps repeating output.

Robin Dinse
  • 1,491
  • 14
  • 20