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

5

8

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.

Lemal answered 14/2, 2013 at 15:50 Comment(2)
How simple? Most Linux may have any number of tools installed very easily.Filial
I really cannot tell for sure. Name me a tool and I can tell you if I am able to use that.Lemal
F
13

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.

Filial answered 14/2, 2013 at 15:57 Comment(3)
Ok, that makes sense. But I tried to do sudo bash -c "chat -v -f chat.txt < /dev/ttyS0 > /dev/ttyS0" with the verbose action and the file consisting of '' AT OK AT OK, but got no output at all. Is it working or not? Did I oversee something?Lemal
You won't see anything normally, but it looks you you can use the -sv options to see it logged to stderr. The -v by itself logs to syslog. BTW, you could also use the expect tool.Filial
I'm doing the same thing, but on android mobile. can this answer help me? if yes, what is ppp packet and who can I install it.Arkose
N
4

Here is a very simple way to automate this within a bash script:

$ (echo AT; echo ATS0=0) | atinout - /dev/ttyS0 -
AT
OK
ATS0=0
OK
$

by using the atinout program which is written specifically with this functionality as its sole purpose. The output above is assuming ATE1; without echo the response from the modem will be "\r\n\r\nOK\r\n\r\nOK\r\n".

In the example above, atinout will send the first command AT (properly terminating the command line with \r), wait until it receives a Final Result Code (e.g. OK) and first then continue processing the next command.

You can give input from a file by specifying that instead of the first -, and if you want to capture the output give a file name instead of the last -. You can use here doc instead of grouped echo commands if you like.

Nanettenani answered 15/6, 2015 at 22:4 Comment(0)
T
2

To show the messages from:

echo -e "ATHO\r" > /dev/ttyACM0

or any other modem commands type who to find the terminal number then run:

cat /dev/ttyACM0 >& /dev/pts/8 &

Then the messages from the AT command will show on your terminal.

Tonguelashing answered 12/11, 2016 at 3:8 Comment(1)
You can use /dev/tty for your current TTY and avoid the detour to check the correct one from who.Yeomanly
C
0

You have an error :

cat 'AT'

means display AT file wich not exists I guess.

Instead, try doing :

cat<<EOF>/dev/ttyS0
AT
ATS0=0

EOF

That uses here-doc

Collum answered 14/2, 2013 at 15:52 Comment(1)
ups, I meant echo of course. Also your answer does not answer my problem. How can I automate so all the commands are sent automatically.Lemal
D
0
  1. go to terminal
  2. enter "sudo minicom -s"
  3. go to "serial port setup" and set the serial port settings
  4. enter "change with setting?"
  5. enter "save setup as dfl". By dfl it means default
  6. enter "exit"
  7. Write any at command on the new open minicom terminal.
Dancer answered 14/1, 2021 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.