Using socat for raw serial connection

Ulrik picture Ulrik · Oct 15, 2014 · Viewed 9.2k times · Source

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
  • raw on both sides allows a single key press to trigger a boot option
  • echo=0 on both sides prevents key press doubling
  • crnl on both sides prevent enter key press doubling
  • escape=0x03 allows me to quit the thing by pressing 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.

Any ideas and suggestions are greatly appreciated.

Answer

Chris Stratton picture Chris Stratton · Oct 15, 2014

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.