After few more hours of intensive research and testing many different options I came with a solution.
First of all, I had to enable terminal by adding line:
ttyS1
in file /etc/securetty
.
Without this you cannot login to terminal at /dev/ttyS1
.
Secondly, I tested many different socat configurations and the following command works:
/usr/local/bin/socat tcp-l:2020,reuseaddr,fork,crlf file:/dev/ttyACM0,echo=0,b115200,raw,crnl,icanon=1
However, this solution isn't perfect. Terminal breaks line after every command prompt so the user input is typed in the next line. Furthermore, after executing each command appears an empty command prompt (like I had entered empty command immediately after the one I had really entered).
I suppose that I should configure /dev/ttyACM0
properly with stty, but I'm still researching the topic.
I found a solution. It involves 4 instances of socat, arranged in the network diagram below:
/-<-[A - udp receiver] <=udp= [D - udp sender]-<-\
ttyS0 <--> [B - serial adapter] [C - tcp adapter] <=tcp=> Process A
\->-------------------------------------------->-/
- Socat "B" talks to the serial port, takes input on stdin and outputs to stdout.
- Socat "A" listens for UDP packets from anywhere and outputs them over stdout, where they are piped into socat "B".
- Socat "C" listens for a TCP connection from Process A. Data coming from socat "A" comes in stdin and is routed to Process A. Data from Process A is sent out stdout to socat "D".
- Socat "D" takes data from stdin and sends it out over UDP to socat "A".
Process B sends UDP packets to socat "B" when it wants to inject traffic.
The bash command to create this monstrosity is as follows:
socat -d -d -d -lpA_udp_rxr -u -T5 udp-recv:1111 - 2>>log.txt |
socat -d -d -d -lpB_serial_adapter -t5 - /dev/ttyS0,raw 2>>log.txt |
socat -d -d -d -lpC_tcp_adapter -T5 - tcp4-listen:3333 2>>log.txt |
socat -d -d -d -lpD_udp_sender -u -T5 - udp:localhost:1111,sourceport=2222 2>>log.txt
This also sets a timeout of 5 seconds and logs a lot of detail to "log.txt". Port numbers replaced for security.
Best Answer
Like this?
1st parameter gets
,fork
to havesocat
stay listening for more connectionssystem:
is preferred overexec:
to have a shell interpreter and easily run an additional command after thels
command,which is a new
socat
commandwhich will have stdin/stdout connected to the remote client's output/input and will forward it bidirectionally again to the next destination. Note that its
:
separator (and a few other special characters) must be escaped with a\
to not confuse the firstsocat
command. If this line becomes more complex it become easier to simply exec a script like this:with
/path/to/myscript.sh
:exec
in the shell command is optional but avoids uselessly leaving around the shell.socat
also exports a few variables of its own that can be reused in the script, that you could check for example like this (with a connection made):