The goal is to connect to an embedded device using serial interface.
So far, I've used:
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:
socat /dev/cons0,raw,echo=0,crnl /dev/ttyS2,raw,echo=0,escape=0x03,crnl
rawon both sides allows a singlekey pressto trigger aboot optionecho=0on both sides preventskey pressdoublingcrnlon both sides prevententerkey pressdoublingescape=0x03allows me toquitthe thing by pressingctr+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.
Any ideas and suggestions are greatly appreciated.
As Austin Phillips says, you can use stty sane to recover...
...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 automatic when you quit with ctrl-c.
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:
socat $(tty),raw,echo=0,escape=0x03 /dev/ttyS2,raw,echo=0,nonblock ; stty sane
edit: nonblock also solved the "enter" problem
来源:https://stackoverflow.com/questions/26373006/using-socat-for-raw-serial-connection