Skip to content

snhobbs/ModbusSniffer

Repository files navigation

ModbusSniffer

Modbus RTU packet sniffer

ModbusSniffer is a simple Modbus RTU packet sniffer for serial buses.

It wraps pymodbus to decode and print all packets observed on the wire, from either the master or slave perspective. This is especially useful for debugging communication between Modbus devices, verifying protocol behavior, or reverse-engineering device interactions.

Features

  • Pure Python, built on pymodbus
  • Captures and decodes Modbus RTU packets from both master and slave devices
  • Great for protocol debugging, development, and test benches -️ Simple command-line interface with readable log output
  • Easily extensible for custom logging or automation

Installation

git clone https://github.com/snhobbs/ModbusSniffer
cd ModbusSniffer
pip install .

Usage

Terminal output only

modbus_sniffer --port /dev/ttyUSB0 --baud 9600
python3 modbus_sniffer.py --port /dev/ttyUSB0 --baud 9600

With logfile output

modbus_sniffer --port /dev/ttyUSB0 --baud 19200 --logfile modbus.log
python3 modbus_sniffer.py --port /dev/ttyUSB0 --baud 19200 --logfile modbus.log
Option Description
--port, -p Serial port to open (default: /dev/ttyUSB0)
--baud, -b Baud rate (default: 9600)
--timeout Set Modbus read timeout manually
--debug Enable verbose debug logging

Testing with socat

  1. Setup a simulated serial link
socat -d -d pty,raw,echo=0,link=/tmp/ttyS0 pty,raw,echo=0,link=/tmp/ttyS1
  1. Start modbus_sniffer
python modbus_sniffer.py --port /tmp/ttyS0 --baud 9600
  1. Run test script
python tests/send_client_requests.py /tmp/ttyS1 --baud 9600