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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:34:06  来源:igfitidea点击:

Using socat for raw serial connection

linuxbashserial-portsocat

提问by Ulrik

The goal is to connect to an embeddeddevice using serialinterface. 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 bootthat require you to press a single key without pressing enter, and readlinefails there. So my idea was to bind the ttyS2to cons0, but then I discovered multiple problems, such as inability to quit (ctr+c, ctr+qctr+]and even escdoesn't work), backspaceand deletedo not work, letters are typed twice, etc. So after some trial and error, I came up with this:

它工作得很好,但后来我发现在system boot此期间有一些选项需要您按一个键而不按enter,并且readline在那里失败。所以我的想法是绑定ttyS2cons0,但后来我发现了许多问题,如无法退出(ctr+cctr+qctr+]甚至esc无法正常工作),backspacedelete没有工作,来信使一些试验和错误后输入两次,等等,我想出了这个:

socat /dev/cons0,raw,echo=0,crnl /dev/ttyS2,raw,echo=0,escape=0x03,crnl
  • rawon both sides allows a single key pressto trigger a boot option
  • echo=0on both sides prevents key pressdoubling
  • crnlon both sides prevent enterkey pressdoubling
  • escape=0x03allows me to quitthe thing by pressing ctr+c
  • raw两边允许单个key press触发boot option
  • echo=0在两侧防止key press加倍
  • crnl两边防止enterkey press加倍
  • escape=0x03允许我quitctr+c

The problem is, when I quit, my cons0is all f****d up, as if it somehow preserved the raw,echo=0,crnlsettings. 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 socatbecause 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 saneto recover...

正如奥斯汀菲利普斯所说,你可以stty sane用来恢复......

...but what is even better is that you can (probably) append it to your socat command as socat xxxxx ; stty saneand 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 expectspawns 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: nonblockalso solved the "enter" problem

编辑:nonblock也解决了“输入”问题