1

I am connecting to a modem over a serial port and trying to figure out how to send an AT command, and add conditionals depending on the output. I can connect using screen or minicom to /dev/ttyAMA0 and send the AT command and receive the response OK, but when I use

echo -en "AT" >/dev/ttyAMA0 && cat /dev/ttyAMA0

I only see what I am echoing, not what the response is. I need to be able to send the AT command, check to see if the output is OK, or ERROR, then based off that response, do something different. Why am I not getting any response from the serial device?

I am trying to create a bash script that can connect to the modem and send a text message, but need to know if there are errors rather than just blasting things through assuming it is working. Is there a better way to accomplish this?

2 Answers 2

1

Scripting a dialog through a terminal-like port is a complex enough problem that people have written special tools to do it; the classic is the Expect/Tcl library. I think Tcl is simple to learn, but there are Expect bindings for other scripting languages.

I found this script that uses Expect to communicate over a serial port.

Sign up to request clarification or add additional context in comments.

Comments

0

After echoing you cannot simply cat the device file. You need to start a listening loop:

while read -r str < /dev/ttyAMA0; do
  # $str will contain a line of text returned from modem.
done

2 Comments

I don't understand why I can't just cat the device? When I have it open in two terminals, I see all the AT commands I put into terminal 1 on terminal 2 that has cat /dev/ttyAMA0 running. With this loop, I didn't see anything at all returned to the read variable.
I used this method and it worked fine. Sorry if this is not universal in all cases. And btw you need to check $str variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.