Alex Alex - 25 days ago 5
Linux Question

How to send a sequence of AT commands to a serial port in bash?

In Linux I need to send a sequence of AT commands to a serial port on e.g.

/dev/ttyS0
, which has to wait for an
OK
answer before the next command is sent. I could imagine doing something like

echo 'AT' > /dev/ttyS0
echo 'ATS0=0' > dev/ttyS0
...


but this does not evaluate the answer from the device on that port.

Is there a very simple way to automate this within a bash script, probably with the help of
socat
and/or
microcom
but no tools which cannot found on the most simple linux system.

Answer

If you install the PPP package you can use the chat program that comes with it. Or you can use kermit. Or the cu program that comes with uucp. But to do it with pure shell is trickier. You might be able to use the read and printf functions, with stdio redirected to the port.

some snippet:

stty -F /dev/ttyS0 38400 raw
chat -f script.txt < /dev/ttyS0 > /dev/ttyS0

Should get you started.