Skip to content

A quick serial logging application

Recently when debugging a device connected to a rs485 bus, I needed a simple application to dump the raw data coming over the rs485 bus.  Minicom does all kinds of weird terminal stuff, plus it will not display binary data, so that was out.  While looking for serial analyzer programs for Linux, and pondering writing my own simple C application, it occurred to me to use pyserial.  With a few lines of code, I got exactly what I needed:

import serial
import sys
import struct

if len(sys.argv) != 3:
    print "Usage: <serial port> <baud rate>"

print "Dumping data on %s" % (sys.argv[1])

ser = serial.Serial(sys.argv[1], sys.argv[2], timeout=0.3)

    buf =
    if len(buf) > 0:
        print "Read %i bytes:" % (len(buf)),

        for i in buf:
            value = struct.unpack('B', i)[0]
            print "%02x" % (value),

        print ""

The full source is available here.  This could obviously be improved to do true blocking reads, automatically parse protocols, implement a gui, etc.  Ideas for efficiently reading data using pyserial is available in a previous post.  Another example of using a scripting language for quick tasks is