Skip to content

Instantly share code, notes, and snippets.

@arberg
Created November 25, 2018 10:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arberg/cf8f2492ef8e2ec21910997adb3e19ea to your computer and use it in GitHub Desktop.
Save arberg/cf8f2492ef8e2ec21910997adb3e19ea to your computer and use it in GitHub Desktop.
In linux redirect RS232 Serial Port traffic (in+out) to TCP port. Log all input + output to files. Its possible to connect with telnet to tcp-port. Short circuit COM pin 2+3 (in+out) to do loop-back test of connection without device attached, see http://www.ni.com/tutorial/3450/en/
#!/bin/bash
# netcat -l 7000 </dev/ttyUSB0 >/dev/ttyUSB0 &
cat /dev/ttyUSB0 | tee /var/log/lyngdorfSerial.ComToTcp.log | netcat -l 7000 | tee /var/log/lyngdorfSerial.TcpToCom.log > /dev/ttyUSB0 &
# Linux resets state when netcat closes com-port, so we need to se this after opening netcat
stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenb
killtree() {
local _pid=$1
local _sig=${2:--TERM}
kill -stop ${_pid} # needed to stop quickly forking parent from producing children between child killing and parent killing
for _child in $(ps -o pid --no-headers --ppid ${_pid}); do
killtree ${_child} ${_sig}
done
kill ${_sig} ${_pid}
}
background_pid=$!
trap "logger 'killing lyngdorf'; killtree $background_pid -KILL 2>/dev/null; exit" INT TERM EXIT
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment