从Linux命令行写入串行端口
在 Windows 中,我可以使用以下命令与串行端口设备进行通信:
mode com1: baud=9600 data=8 parity=n stop=1
copy con com1
alt+18alt+2ctrl+z
设备启动请求的操作。
当我尝试从独立的 Debian 盒子或 Debian < 完成相同的操作时a href="https://en.wikipedia.org/wiki/VirtualBox" rel="nofollow noreferrer">VirtualBox 同一台 Windows 机器的实例,到目前为止我还没有运气。
以下是等效的 Linux 命令(至少我是这么认为):
stty -F /dev/ttyS0 speed 9600 cs8 -cstopb -parenb
echo '\x12\x02' > /dev/ttyS0
没有任何反应。
我该怎么做呢?
From Windows I can communicate with a serial port device using the following commands:
mode com1: baud=9600 data=8 parity=n stop=1
copy con com1
alt+18alt+2ctrl+z
The device starts the requested operation.
When I try to accomplish the same operation from a stand-alone Debian box or from a Debian VirtualBox instance of the same Windows machine, I had no luck so far.
Here are equivalent Linux commands (at least I think so):
stty -F /dev/ttyS0 speed 9600 cs8 -cstopb -parenb
echo '\x12\x02' > /dev/ttyS0
Nothing happens.
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果要使用十六进制代码,则应添加
-e
选项以启用 echo 解释反斜杠转义(但结果与echo
相同Ctrl + R、Ctrl + B)。正如 wallyk 所说 ,您可能需要添加-n
以防止输出换行符:还要确保
/dev/ttyS0
是您想要的端口。If you want to use hexadecimal codes, you should add the
-e
option to enable interpretation of backslash escapes by echo (but the result is the same as withecho
Ctrl + R,Ctrl + B). And as wallyk said, you probably want to add-n
to prevent the output of a newline:Also make sure that
/dev/ttyS0
is the port you want.不会被解释,并且会按字面意思将字符串
\x12\x02
(并附加换行符)写入指定的串行端口。相反,您可以通过键入 CtrlVCtrlR 和 Ctrl< 在命令行上构建它/kbd>VCtrlB。或者使用编辑器输入脚本文件更容易。
stty 命令应该可以工作,除非有其他程序干扰。常见的罪魁祸首是
gpsd
,它会查找插入的 GPS 设备。will not be interpreted, and will literally write the string
\x12\x02
(and append a newline) to the specified serial port. Instead usewhich you can construct on the command line by typing CtrlVCtrlR and CtrlVCtrlB. Or it is easier to use an editor to type into a script file.
The
stty
command should work, unless another program is interfering. A common culprit isgpsd
which looks for GPS devices being plugged in.使用Screen:
注意:Screen实际上无法发送十六进制, 据我所知。为此,请使用
echo
或printf
。我使用本文中的建议写入串行端口,然后使用 另一篇文章从端口读取,结果好坏参半。我发现使用 Screen 是一个“更简单”的解决方案,因为它直接与该端口打开终端会话。 (我把更容易的放在引号中,因为 Screen 有一个非常奇怪的界面,IMO,需要进一步阅读才能弄清楚。)
您可以发出此命令来打开 screen 会话,然后您输入的任何内容都将被发送到端口,加上返回值将打印在它下面:(
更改上面的内容以满足您对速度、奇偶校验、停止位等的需求)我意识到 Screen 不是帖子特别要求的“Linux 命令行”,但是我认为这也是本着同样的精神。另外,您不必每次都键入 echo 和引号。
echo
它遵循praetorian droid的答案。 但是,这对我不起作用,直到我也使用cat 命令(
cat )当我发送echo命令时。
printf
我发现还可以使用 printf 的 '%x' 命令:
同样,对于 printf,start
cat
cat < /dev/ttyS0
在发送命令之前。Using Screen:
Note: Screen is actually not able to send hexadecimal, as far as I know. To do that, use
echo
orprintf
.I was using the suggestions in this post to write to a serial port, and then using the information from another post to read from the port, with mixed results. I found that using Screen is an "easier" solution, since it opens a terminal session directly with that port. (I put easier in quotes, because Screen has a really weird interface, IMO, and takes some further reading to figure it out.)
You can issue this command to open a screen session, and then anything you type will be sent to the port, plus the return values will be printed below it:
(Change the above to fit your needs for speed, parity, stop bits, etc.) I realize Screen isn't the "Linux command line" as the post specifically asks for, but I think it's in the same spirit. Plus, you don't have to type echo and quotes every time.
echo
It follows praetorian droid's answer. However, this didn't work for me until I also used the cat command (
cat < /dev/ttyS0
) while I was sending the echo command.printf
I found that one can also use printf's '%x' command:
Again, for printf, start
cat < /dev/ttyS0
before sending the command.