0

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.

Zappel
  • 268
  • 3
  • 13
  • having the same problem. Did you find a solution? I already tried using the driver that is supplied by FTDI, but with that it doesn't work either. The problem only occurs with OSX. Linux and Windows can choose baudrates>230400, both with the built-in and the FTDI drivers. – Stefan D. Dec 13 '15 at 15:23
  • @StefanD. Nope, I have no solution yet unfortunately. – Zappel Dec 14 '15 at 10:02

1 Answers1

0

There's a good answer here: How to set the baud rate for Macs in a terminal

Basically use stty to set the baud rate and background it so it doesn't exit:

stty -f /dev/cu.usbserial 460800 &
Community
  • 1
  • 1
aqua
  • 617
  • 4
  • 12