How can I monitor serial port traffic?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

is a tool to connect (nearly) everything to (nearly) everything, and 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 “standard procedure” 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.

Updated after comments:

  • The socat syntax looks confusing at first, but in fact, it’s just 2 nested statements.
    A small price to pay to such a powerful, versatile tool.
  • If you need to setup your serial port, or send other ioctls, do so before calling socat, as socat cannot proxy them.
  • The single-purpose-tool interceptty from 2006 has slightly simpler syntax, but can only intercept TTYs (while proxying ioctls), and is probably not in your package manager. (Most Linux distros never added it to their repos)

Method 2

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

Method 3

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.

Method 4

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.

Method 5

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+"(.*)",sd+.*$/ && do { $_=$1;s/\(d+|[abefnrt])/cnv($1)/eg;print; };};'

Sorry, I will explain…

#!/bin/bash

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+"(.*)",sd+.*$/ && do {
                $_=$1;
                s/\(d+|[abefnrt])/cnv($1)/eg;
                print;
            };
        };
    '
  • I use ls -l /proc/[0-9]*/fd/* | grep ttyUSB0 instead of lsof 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 or function, to run them with serial device name as argument: ttySniff USB0 or ttySniff S0 and so on.
  • Perl script will unbackslash strings logged by strace -s 9999.
  • You could replace strace -e read by strace -e read,write or strace -e write depending on your need.

Note: I run them by using syntax:

 script -t ttySniff.log 2>ttySniff.tm -c "./ttySniff.sh USB0"

so I could replay the whole operation and trace timing executions.

Method 6

When I debug interaction of my application with a serial port, I use moserial.

Method 7

Try this:

screen /dev/tty.usbserial-blahblah 9600

works for me.

Method 8

Have a look at ttyUSBSpy.
It is on alpha stage, but it works.

Method 9

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

Method 10

I use Wireshark. Here you can find a complete how-to guide and here the USB filter reference.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x