Linux 如何将一系列 AT 命令发送到 bash 中的串行端口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14878684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:00:53  来源:igfitidea点击:

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

linuxbashserial-portat-command

提问by Alex

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 OKanswer before the next command is sent. I could imagine doing something like

在 Linux 中,我需要将一系列 AT 命令发送到 eg 上的串行端口,在发送下一个命令之前/dev/ttyS0必须等待OK应答。我可以想象做类似的事情

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 verysimple way to automatethis within a bash script, probably with the help of socatand/or microcombut no tools which cannot found on the most simple linux system.

是否有一种非常简单的方法可以在 bash 脚本中自动执行此操作,可能借助socat和/或microcom但没有在最简单的 linux 系统上找不到的工具

采纳答案by Keith

If you install the PPP package you can use the chatprogram that comes with it. Or you can use kermit. Or the cuprogram 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.

如果您安装了 PPP 包,您可以使用chat它附带的程序。或者你可以使用kermit. 或者cuuucp自带的程序。但是用纯 shell 来做这件事比较棘手。您可以使用 read 和 printf 函数,并将 stdio 重定向到端口。

some snippet:

一些片段:

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

Should get you started.

应该让你开始。

回答by Gilles Quenot

You have an error :

你有一个错误:

cat 'AT'

means display AT filewich not exists I guess.

意味着显示我猜不存在的AT 文件

Instead, try doing :

相反,请尝试执行以下操作:

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

EOF

That uses shellhere-doc

使用shell here-doc

回答by hlovdal

Here is a verysimple way to automatethis within a bash script:

这是在 bash 脚本中自动执行此操作的一种非常简单的方法:

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

by using the atinoutprogram 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".

通过使用专门为此功能编写的atinout程序作为其唯一目的。上面的输出假设ATE1; 如果没有回声,来自调制解调器的响应将是"\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.

在上面的例子中,atinout 将发送第一个命令AT(用 正确终止命令行\r),等到它收到最终结果代码(例如OK),然后首先继续处理下一个命令。

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.

您可以通过指定-文件名而不是第一个来提供来自文件的输入,如果您想捕获输出,请提供文件名而不是最后一个-。如果您愿意,您可以在此处使用 doc 而不是分组回显命令。

回答by len

To show the messages from:

显示来自以下方面的消息:

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

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

或任何其他调制解调器命令类型who以查找终端号,然后运行:

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

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

然后来自 AT 命令的消息将显示在您的终端上。