Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
  • 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.

like image 560
Ulrik Avatar asked Oct 15 '14 01:10

Ulrik


People also ask

What is Socat tool used for?

Socat allows for bidirectional data transfers from one location to another. The socat utility is a relay for bidirectional data transfers between two independent data channels.

What is Socat command?

Socat is a flexible, multi-purpose relay tool. Its purpose is to establish a relationship between two data sources, where each data source can be a file, a Unix socket, UDP, TCP, or standard input.

What is netcat and Socat?

Netcat and Socat allows you to pass simple messages between computers interactively over the network. The below setup will allow both client and server to send data to the other party.


1 Answers

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.

like image 80
Chris Stratton Avatar answered Sep 30 '22 17:09

Chris Stratton