Is there any port monitoring tool to watch the packets written on the port? I especially want to check if my program written in Java works so I need some kind of tool to see if my little application is writing the messages to the port. How do I do this?
-
5Packets aren't written on the port. Characters are. It's not like Ethernet at all. – LawrenceC Apr 30 '11 at 18:33
-
1Similar questions from sibling SE sites: stackoverflow.com/q/940374/12892 and serverfault.com/q/112957/4276 – Cristian Ciupitu Feb 4 '15 at 8:12
I found projects called Linux Serial Sniffer, jpnevulator, and Moni. The first two look like they do exactly what you want. The last one calls itself a monitor, but it actually looks like a standard serial communication program.
-
1thanks for that !! i will give it a try. by the way i solved the issue from my java side. i was missing a \r, so that prevented my message from writing on to the port. thanks for that anyways!! – Deepak Apr 30 '11 at 18:24
-
3The «LInux Serial Sniffer» is buggy, it absolutely takes out incoming data, thus another application which is actually listen to serial see nothing. But, at least, the data that goes outside seems to go without problem. – Hi-Angel Mar 31 '15 at 11:12
-
3From the jpnevulator FAQ: "Jpnevulator was never built to sit in between the kernel and your application." – Shelvacu Jun 12 '17 at 2:14
-
1
-
1-1 because of 3 comments: LInux Serial Sniffer is buggy, then Jpnevulator was never built to sit in between kernel and app and finaly Moni is dead... This answer just point to 3 externals links and don't give a real solution. (3 fail on 3 link, left nothing!) – F. Hauri Sep 20 at 11:49
socat is a tool to connect (nearly) everything to (nearly) everything, and tee can duplicate streams.
In your usecase you could connect your serial port /dev/ttyS0
to a PTY /tmp/ttyV0
, then point your application to the PTY, and have socat tee
out Input and Output somewhere for you to observe.
Googling "socat serial port pty tee debug" will point you to several examples, one being:
socat /dev/ttyS0,raw,echo=0 \
SYSTEM:'tee in.txt |socat - "PTY,link=/tmp/ttyV0,raw,echo=0,waitslave" |tee out.txt'
The files in.txt
and out.txt
will then contain the captured data.
This has been confirmed to work by commenters (@ogurets).
-
1Just tried it and have both input and output recorded. Socat version "1.7.2.4+sigfix" from Debian Jessie packages. – ogurets May 17 '17 at 21:41
-
The idea is good, but not even
socat
can proxy ioctl calls. – peterh says reinstate Monica Jan 24 at 13:34
I don't think the serial driver has any tracing functionality that would allow you to watch packets. You can use strace
to observe all the reads and writes from your application:
strace -s9999 -o myapp.strace -eread,write,ioctl ./myapp
-
1
-
strace will tell you if it tried to send characters to the port, and what the kernel responded with when it tried. depending on your flow control settings characters may arrive at the disconnected TXD pin or may not. – Jasen Jun 28 '18 at 5:19
-
interceptty
does that job:
interceptty /dev/ttyACM0 /dev/ttyDUMMY
or, with a nice output format and with configuring the backend device, and with line buffering:
interceptty -s 'ispeed 19200 ospeed 19200' -l /dev/ttyACM0 /dev/ttyDUMMY | interceptty-nicedump
and then connect with your programme to /dev/ttyDUMMY
.
-
@AlexStragies: I have it on my arch linux system. AUR page: aur.archlinux.org/packages/interceptty, copy of the sources: repo.j5lx.eu/archive/interceptty/interceptty-0.6.tar.gz – Golar Ramblar Apr 3 '17 at 10:22
-
I had to download it (using
wget
since clicking on the.tar.gz
file seemed to corrupt it somehow), installgcc
andmake
, then run./configure
andmake install
. Does exactly what the OP and I want though. – Graeme Moss May 31 '17 at 23:18 -
Try this:
screen /dev/tty.usbserial-blahblah 9600
works for me.
-
25This opens the port and assumes control over it, so nothing else can use it. This does not "monitor" or "sniff" the traffic. – Ian M Jan 23 '15 at 7:12
This is the way I finally choose
Thanks to Gilles's answer!
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; };};'
Sorry, I will explain...
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;
};
};
'
- I use
ls -l /proc/[0-9]*/fd/* | grep ttyUSB0
instead oflsof ttyUSB0
because I seen them sometime slow. - So strace will trace current program using
ttyUSB0
- Syntax:
tty${1:-USB0}
will permit, used as a script, to run then with serial device name as argument:ttySniff USB0
orttySniff S0
and so on. - Perl script will
unbackslash
strings logged bystrace
.
Nota: I run them by using script -t
so I could replay the whole and trace timing executions.
-
There is no security consideration, about what could be comming by serial port! – F. Hauri Oct 29 at 17:35
-
minicom
is missing from the list of tools to monitor serial ports. Use it as for example to listen to arduino device:
minicom --device /dev/ttyACM0 --baud 9600
-
OP wrote "monitor", but meant "sniffer" ( = is able to read traffic in transit), while minicom is a serial port "client", and as such is not an answer to this question. The answer below from mike made the same mistake, and the comment there explains the terminology problem as well. – Alex Stragies Mar 17 at 21:03