How to Monitor and Analyze Serial Port Communication Packets in Linux


1 views

When working with embedded systems or hardware devices in Linux, monitoring serial communication is often essential for debugging and reverse engineering. The serial port (typically /dev/ttyS* or /dev/ttyUSB*) provides raw byte streams that need proper capturing and interpretation.

sudo apt-get install screen minicom socat tshark wireshark

These tools provide different approaches to serial monitoring:

  • screen/minicom: Basic terminal communication
  • socat: Powerful data redirection
  • wireshark/tshark: Advanced protocol analysis

This creates a virtual serial port pair and logs all traffic:

socat -d -d -lf serial.log pty,raw,echo=0 pty,raw,echo=0

Connect your device to one virtual port and your application to the other. All communication will be logged to serial.log.

For applications opening serial ports directly:

strace -e trace=read,write -s 1024 -o serial_trace.txt your_application

For structured protocol analysis:

sudo modprobe usbmon
sudo wireshark

In Wireshark, capture on the usbmon interface and apply filter "usb.transfer_type == 0x03" for bulk transfers.

This script bridges between two serial ports while logging:

import serial
import time

port1 = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
port2 = serial.Serial('/dev/ttyUSB1', 115200, timeout=1)

with open('serial_dump.log', 'wb') as f:
    while True:
        if port1.in_waiting > 0:
            data = port1.read(port1.in_waiting)
            f.write(b"PORT1->PORT2: " + data + b"\n")
            port2.write(data)
        
        if port2.in_waiting > 0:
            data = port2.read(port2.in_waiting)
            f.write(b"PORT2->PORT1: " + data + b"\n")
            port1.write(data)
        
        time.sleep(0.01)

Some devices may require specific configurations:

  • Add user to dialout group: sudo usermod -a -G dialout $USER
  • Set correct permissions: sudo chmod 666 /dev/ttyS*
  • For USB serial converters: dmesg | grep tty to identify the device

When working with embedded systems or legacy hardware interfaces, serial port communication remains crucial. Linux provides robust tools for monitoring and analyzing this data traffic. The fundamental approach involves intercepting the data stream between the serial device and the application without disrupting normal operation.

Several powerful utilities exist for this purpose:

  • socat: Creates bidirectional byte streams
  • strace: Traces system calls and signals
  • minicom: Terminal emulator with logging
  • ttylog: Dedicated serial port logger

Here's a reliable method using socat to create a monitoring bridge:

socat -x -v PTY,link=/tmp/serial_sniffer,rawer \
      PTY,link=/tmp/serial_target,rawer

The -x option shows hexadecimal output, while -v provides verbose ASCII translation. Connect your application to /tmp/serial_target and your device to /tmp/serial_sniffer.

For programmatic analysis, Python's serial module combined with pyserial offers flexibility:

import serial
import binascii

ser = serial.Serial('/dev/ttyUSB0', 115200)

while True:
    data = ser.read(ser.in_waiting or 1)
    print(f"Raw: {binascii.hexlify(data)}")
    print(f"ASCII: {data.decode('ascii', errors='replace')}")

For low-level analysis, consider these approaches:

  • Use strace -e trace=read,write -p PID on the serial process
  • Configure syslog to capture tty messages
  • Implement kernel module hooks for in-depth inspection

Remember these key points when sniffing serial data:

  • Baud rate and parity settings must match the monitored connection
  • Buffer sizes may need adjustment for high-speed interfaces
  • Timestamping is essential for protocol analysis
  • Some USB-serial converters may introduce latency