bash 使用 socat 进行原始串行连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26373006/
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
Using socat for raw serial connection
提问by Ulrik
The goal is to connect to an embedded
device using serial
interface.
So far, I've used:
目标是embedded
使用serial
接口连接到设备。到目前为止,我已经使用了:
stty -F /dev/ttyS2 115200 cs8 ixoff
socat readline,history=/etc/socat.history /dev/ttyS2,raw,echo=0
And it works excellent, but then I discovered that there are some options during system boot
that require you to press a single key without pressing enter
, and readline
fails there. So my idea was to bind the ttyS2
to cons0
, but then I discovered multiple problems, such as inability to quit (ctr+c
, ctr+q
ctr+]
and even esc
doesn't work), backspace
and delete
do not work, letters are typed twice, etc. So after some trial and error, I came up with this:
它工作得很好,但后来我发现在system boot
此期间有一些选项需要您按一个键而不按enter
,并且readline
在那里失败。所以我的想法是绑定ttyS2
到cons0
,但后来我发现了许多问题,如无法退出(ctr+c
,ctr+q
ctr+]
甚至esc
无法正常工作),backspace
并delete
没有工作,来信使一些试验和错误后输入两次,等等,我想出了这个:
socat /dev/cons0,raw,echo=0,crnl /dev/ttyS2,raw,echo=0,escape=0x03,crnl
raw
on both sides allows a singlekey press
to trigger aboot option
echo=0
on both sides preventskey press
doublingcrnl
on both sides prevententer
key press
doublingescape=0x03
allows me toquit
the thing by pressingctr+c
raw
两边允许单个key press
触发boot option
echo=0
在两侧防止key press
加倍crnl
两边防止enter
key press
加倍escape=0x03
允许我quit
按ctr+c
The problem is, when I quit, my cons0
is all f****d up, as if it somehow preserved the raw,echo=0,crnl
settings. I know this problem is probably too specific for my scenario, but I just need a simple way to send keystrokes to serial as I would with putty
(which is not available on my platform). I am using socat
because it is extremely lightweight, does not require any aditional libraries, and because the shown commands are a part of the greater script that uses expect
.
问题是,当我退出时,我cons0
全神贯注,好像它以某种方式保留了raw,echo=0,crnl
设置。我知道这个问题对于我的场景来说可能太具体了,但我只需要一种简单的方法来像我一样将击键发送到串行putty
(这在我的平台上不可用)。我使用socat
它是因为它非常轻量级,不需要任何额外的库,并且因为显示的命令是使用expect
.
Any ideas and suggestions are greatly appreciated.
任何想法和建议都非常感谢。
采纳答案by Chris Stratton
As Austin Phillips says, you can use stty sane
to recover...
正如奥斯汀菲利普斯所说,你可以stty sane
用来恢复......
...but what is even better is that you can (probably) append it to your socat command as socat xxxxx ; stty sane
and have the recovery be automaticwhen you quit with ctrl-c.
...但更好的是,您可以(可能)将它附加到您的 socat 命令中,socat xxxxx ; stty sane
并在您使用 ctrl-c 退出时自动恢复。
回答by Ulrik
Thanks, that worked for me!
I just want to point out that the script should not rely on "static" console identification, because when expect
spawns the script, it is going to have a completely different tty
, therefor:
谢谢,这对我有用!我只想指出脚本不应该依赖“静态”控制台标识,因为当expect
生成脚本时,它会有一个完全不同的tty
,因此:
socat $(tty),raw,echo=0,escape=0x03 /dev/ttyS2,raw,echo=0,nonblock ; stty sane
edit: nonblock
also solved the "enter" problem
编辑:nonblock
也解决了“输入”问题