socat: log conversation with serial port

5.5k views Asked by At

After some time, I finally got socat to behave in the same way as putty, using:

stty -F /dev/ttyS2 115200 cs8 ixoff
socat $(tty),raw,echo=0,escape=0x03,nonblock /dev/ttyS2
stty sane

I would also like to log the conversation to a log file.

EDIT:

I figured out a way how to do it, (but I think it's a kludge), so I answered my own question - and it is a valid answer by stack overflow standards.

1

There are 1 answers

3
TrueY On BEST ANSWER

(my first approach was not correct, so I modify)

I have never used socat, but if it has -v log.txt option I have a possible solution for you as I see with -v option it prints the result also to stderr.

I assume You want to have the log file sent into log.txt, cut the first 10 lines and all the \r characters removed. On a which supports process substitution it can be done like this:

socat -v ... 2> >(awk 'NR>10 {gsub("\r","");print}'>log.txt)

(Mind the space between the two > marks!!!)

This will create a process and >(...) returns a file like /dev/fd/68 and redirects the stderr to it. Actually this is a pipe to the process specified inside the brackets (awk). So the text goes through and its output is written into log.txt.

As You mentioned, You do not have this can be solved using (or tail and tr pipe but it needs an extra process).

socat -v ... 2> >(sed -e '1,10 d' -e 's/\r//g' >log.txt)

(Mind the space between the two > marks!!!)

It seems does not support this, but it worked on a fairly old RedHat Linux.

Actually You can also use -lf (I'm not sure that this prints everything You need. Maybe -v if a better choice...):

-lf <logfile>
    Writes messages to <logfile> [filename] instead of stderr. 
    Some third party libraries, in particular libwrap, might not obey 
    this option. 

So the original approach also can work:

socat -lf >(sed -e '1,10 d' -e 's/\r//g' >log.txt) ...

I hope this helps!