Socat: communicate with a serial port
After a while, I finally got socat
to behave the same way 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 record the conversation to a log file.
EDIT:
I figured out a way how to do this (but I think it is kludge), so I answered my own question - and this is the correct answer to standards.
source to share
(my first approach was wrong, so I change)
I've never used it socat
, but if it has an option , as I see with the option -v log.txt
, I have a possible solution for you-v
, it also outputs the result to stderr.
I am assuming that you want the log file to be sent to log.txt, cut the first 10 lines and all characters \r
removed. On bash , which supports process substitution , this can be done like this:
socat -v ... 2> >(awk 'NR>10 {gsub("\r","");print}'>log.txt)
(Remember the space between the two labels >
!!!)
This will create a process and >(...)
return a file of type /dev/fd/68
and redirect it stderr
. It is actually a pipe for the process specified inside the brackets ( awk
). This way the text is passed through awk and its output is written to log.txt
.
As you mentioned, you don't have awk this can be solved using sed(or tail
and tr
, but this requires an additional process).
socat -v ... 2> >(sed -e '1,10 d' -e 's/\r//g' >log.txt)
(Remember the space between the two labels >
!!!)
It seems cygwin doesn't support this, but it did work on pretty old RedHat Linux.
Actually you can also use -lf
(I'm not sure if this prints whatever you want. Perhaps -v
if 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 might work as well:
socat -lf >(sed -e '1,10 d' -e 's/\r//g' >log.txt) ...
Hope this helps!
source to share