Skip to content

Instantly share code, notes, and snippets.

@edwios
Created January 7, 2020 07:03
Show Gist options
  • Save edwios/6d1d784f70552ec3a62a0e668071f06e to your computer and use it in GitHub Desktop.
Save edwios/6d1d784f70552ec3a62a0e668071f06e to your computer and use it in GitHub Desktop.
Code that put a trace on the serial port and dump both in and out traffic
#!/bin/bash
#
# Following code will put a trace on the serial port and dump both in and out traffic
#
# Method 1, using socat
# Physical device connected to /dev/USB0 and program read/write to /tmp/ttyV0
#
# socat /dev/ttyUSB0,raw,echo=0 SYSTEM:'tee in.txt |socat - "PTY,link=/tmp/ttyV0,raw,echo=0,waitslave" |tee out.txt'
# Method 2, using strace
# Program use the port physical device connected to, no change
#
# strace -s 9999 -e read -ffp $(sed '/ttyUSB0/s/^.*proc.\([0-9]\+\).fd.*/\1/p;d' <(ls -l /proc/[1-9]*/fd/* 2>/dev/null)) 2>&1 | perl -e '$|=1;my %qa=('a'=>7,'b'=>10,'e'=>33,'f'=>14,'n'=>12,'r'=>15,'t'=>11);sub cnv { my $ch=$_[0];$ch=$qa[$1] if $ch=~/([abefnrt])/;return chr(oct($ch)); };while (<>) { /^read.\d+,\s+"(.*)",\s\d+.*$/ && do { $_=$1;s/\\(\d+|[abefnrt])/cnv($1)/eg;print; };};'
strace -s 9999 -e read -ffp $(
sed "/tty${1:-USB0}/s/^.*proc.\([0-9]\+\).fd.*/\1/p;d" <(
ls -l /proc/[1-9]*/fd/* 2>/dev/null
)
) 2>&1 |
perl -e '
$|=1;
my %qa=('a'=>7,'b'=>10,'e'=>33,'f'=>14,'n'=>12,'r'=>15,'t'=>11);
sub cnv {
my $ch=$_[0];
$ch=$qa[$1] if $ch=~/([abefnrt])/;
return chr(oct($ch));
};
while (<>) {
/^read.\d+,\s+"(.*)",\s\d+.*$/ && do {
$_=$1;
s/\\(\d+|[abefnrt])/cnv($1)/eg;
print;
};
};
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment