从linux命令行写入串口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8877269/
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 04:05:39  来源:igfitidea点击:

writing to serial port from linux command line

linuxserial-portvirtualbox

提问by erin c

From windows I can communicate with a serial port device using following commands:

在 Windows 中,我可以使用以下命令与串行端口设备进行通信:

mode com1: baud=9600 data=8 parity=n stop=1
copy con com1
alt+18alt+2ctrl+z

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.

当我尝试从独立的 debian box 或同一台 windows 机器的 debian virtualbox 实例完成相同的操作时,到目前为止我没有运气。

Here's equivalent linux commands(at least I think so)

这是等效的 linux 命令(至少我是这么认为的)

stty -F /dev/ttyS0 speed 9600 cs8 -cstopb -parenb
echo '\x12\x02' > /dev/ttyS0

Nothing happens.

没发生什么事。

Can somebody please direct me to the right direction?

有人可以指导我走向正确的方向吗?

采纳答案by wallyk

echo '\x12\x02'

will not be interpreted, and will literally write the string \x12\x02(and append a newline) to the specified serial port. Instead use

不会被解释,并且会将字符串\x12\x02(并附加换行符)逐字写入指定的串行端口。而是使用

echo -n ^R^B

which you can construct on the command line by typing CtrlVCtrlRand CtrlVCtrlB. Or it is easier to use an editor to type into a script file.

您可以通过键入CtrlVCtrlR和在命令行上构建它CtrlVCtrlB。或者更容易使用编辑器键入脚本文件。

The sttycommand should work, unless another program is interfering. A common culprit is gpsdwhich looks for GPS devices being plugged in.

stty命令应该可以工作,除非另一个程序正在干扰。一个常见的罪魁祸首是gpsd寻找插入的 GPS 设备。

回答by praetorian droid

If you want to use hex codes, you should add -eoption to enable interpretation of backslash escapes by echo (but the result is the same as with echoCtrlRCtrlB). And as wallyk said, you probably want to add -nto prevent the output of a newline:

如果您想使用十六进制代码,您应该添加-e选项以通过 echo 启用反斜杠转义的解释(但结果与 with 相同echoCtrlRCtrlB)。正如 wallyk 所说,您可能想要添加-n以防止换行符的输出:

echo -en '\x12\x02' > /dev/ttyS0

Also make sure that /dev/ttyS0is the port you want.

还要确保这/dev/ttyS0是您想要的端口。

回答by MrUser

SCREEN:

屏幕:

NOTE: screen is actually not able to send hex, as far as I know. To do that, use echoor printf

注意:据我所知,屏幕实际上无法发送十六进制。为此,请使用echoprintf

I was using the suggestions in this post to write to a serial port, then using the info from another postto 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.)

我使用这篇文章中的建议写入串行端口,然后使用另一篇文章中的信息从端口读取,结果喜忧参半。我发现使用 screen 是一个“更简单”的解决方案,因为它直接使用该端口打开终端会话。(我用引号更容易,因为 screen 有一个非常奇怪的界面,IMO,需要进一步阅读才能弄清楚。)

You can issue this command to open a screen session, then anything you type will be sent to the port, plus the return values will be printed below it:

您可以发出此命令来打开一个屏幕会话,然后您输入的任何内容都将发送到该端口,并且返回值将打印在其下方:

screen /dev/ttyS0 19200,cs8

(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.

(更改上述内容以适应您对速度、奇偶校验、停止位等的需求)我意识到 screen 不是帖子特别要求的“linux 命令行”,但我认为它具有相同的精神。另外,您不必每次都输入 echo 和引号。

ECHO:

回声:

Follow praetorian droid's answer. HOWEVER, this didn't work for me until I also used the cat command (cat < /dev/ttyS0) whileI was sending the echo command.

按照 praetorian droid 的回答。然而,这并没有为我工作,直到我也使用cat命令(cat < /dev/ttyS0,而我会发送echo命令。

PRINTF:

打印:

I found that one can also use printf's '%x' command:

我发现还可以使用 printf 的 '%x' 命令:

c="\x"$(printf '%x' 0x12)
printf $c >> $SERIAL_COMM_PORT

Again, for printf, start cat < /dev/ttyS0before sending the command.

同样,对于 printf,cat < /dev/ttyS0在发送命令之前启动。