Alan
Alan

Reputation: 2086

Bash Serial Scripting

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?

Upvotes: 1

Views: 2038

Answers (2)

antlersoft
antlersoft

Reputation: 14786

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.

Upvotes: 1

Phantom Lord
Phantom Lord

Reputation: 360

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

Upvotes: 0

Related Questions