Ulrik
Ulrik

Reputation: 1141

Using socat for raw serial connection

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

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.

Upvotes: 8

Views: 15037

Answers (3)

Seth Robertson
Seth Robertson

Reputation: 31441

In 2023, there are more native mechanisms for socat...

socat STDIO,raw,echo=0,crnl /dev/ttyUSB1,sane,rawer,b115200,cs8,parenb=0; stty sane

In this configuration, socat will natively set the baud rate and the various tty parameters. Note that "rawer" is a combination of "raw" and "echo=0" and maybe other undocumented things--it also supports cfmakeraw.

Note that you probably still want to do stty sane after socat exits to solve this actual questions as the other answers suggest. While socat will try to restore the STDIO tty settings, not all killing methods will allow this to run (e.g. kill -9). However, this approach is better than trying to run stty -F /dev/ttyUSB1 before running socat--sometimes those terminal settings get lost.

I stumbled on this question when I was having massive problems with minicom dropping characters (the serial line did not support any flow control). I switched to socat (with the internal termio settings) and the problem went away--presumably because socat is so much more lightweight. (I also eventually discovered, after many many years of using it, that screen has native serial terminal support, which gave me the ability to do emacs, ^C, and other tty-aware commands, and it too didn't drop characters, so maybe this minicom version is just stupid).

Upvotes: 1

Ulrik
Ulrik

Reputation: 1141

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

Upvotes: 4

Chris Stratton
Chris Stratton

Reputation: 40337

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.

Upvotes: 4

Related Questions