I'm trying to write a little protable script that allows serial port sniffing without any further sniffer software requirements using just some standard command line utilities.
I can call ./serialSniffer.sh /dev/cu.usbserial-xyz
and then connect to an displayed device with the software i want to sniff.
serialSniffer.sh:
#! /usr/bin/env zsh
TEMPORARYDIR="$(mktemp -d 2>/dev/null || mktemp -d -t 'serialSniffer')"
cleanup ()
{
kill $(cat $TEMPORARYDIR/rx_tee.pid) 2> /dev/null
kill $(cat $TEMPORARYDIR/rx_cat.pid) 2> /dev/null
kill $(cat $TEMPORARYDIR/tx_tee.pid) 2> /dev/null
kill $(cat $TEMPORARYDIR/tx_cat.pid) 2> /dev/null
rm $TEMPORARYDIR/rxfifo
rm $TEMPORARYDIR/txfifo
kill $(cat $TEMPORARYDIR/socat.pid) 2> /dev/null
rm $TEMPORARYDIR/rx_tee.pid
rm $TEMPORARYDIR/rx_cat.pid
rm $TEMPORARYDIR/tx_tee.pid
rm $TEMPORARYDIR/tx_cat.pid
rm $TEMPORARYDIR/socat.pid
exit 0
}
trap cleanup SIGINT SIGTERM
socat pty,raw,echo=0,link=$TEMPORARYDIR/ttyDevice pty,raw,echo=0,link=$TEMPORARYDIR/ttyHost &
echo $! > $TEMPORARYDIR/socat.pid
sleep 0.01
mkfifo $TEMPORARYDIR/rxfifo
cat $TEMPORARYDIR/ttyDevice > $TEMPORARYDIR/rxfifo &
echo $! > $TEMPORARYDIR/rx_cat.pid
tee $1 $TEMPORARYDIR/rx < $TEMPORARYDIR/rxfifo > /dev/null &
echo $! > $TEMPORARYDIR/rx_tee.pid
mkfifo $TEMPORARYDIR/txfifo
cat $1 > $TEMPORARYDIR/txfifo &
echo $! > $TEMPORARYDIR/tx_cat.pid
tee $TEMPORARYDIR/ttyDevice $TEMPORARYDIR/tx < $TEMPORARYDIR/txfifo > /dev/null &
echo $! > $TEMPORARYDIR/tx_tee.pid
echo "Connect computer side software to '$TEMPORARYDIR/ttyHost'."
tail -f $TEMPORARYDIR/tx $TEMPORARYDIR/rx
It does work with low baudrates up to 230400. But when choosing 460800 in my PySerial software for example, i got 'Inappropriate ioctl for device' errors.
Using stty
does not work to set high baudrates.
A direct connection between PySerial and dev/cu.usbserial-xyz
works so it is not a hardware issue.