指向一个串口,它扫出哪些从站应答、哪些寄存器可读,再对寄存器逐个采样、给出基于证据的画像——全程只读,代码里不存在写路径。
English: Point it at a serial port: it finds which Modbus RTU slaves answer, maps readable registers, and probes them for an evidence-based profile — read-only, with no write path at all.
Sibling project of SerialLens (same philosophy: face unknown hardware, report only evidence) and comfy-preflight.
python modbus_lens.py --demo # full workflow on virtual slaves, no hardware
python modbus_lens.py --port COM3 --baud 9600 --probe 8Safety by default: every bus operation is a READ — function codes are whitelisted to 0x03/0x04 in code (
_check_funcrejects anything else), and there is no write path at all. On an industrial bus, that is the whole point.
Demo output (trimmed):
== Slave discovery (addr 1-247): 2 found
- slave 1 responds
== Slave 1 readable ranges (regs 0-200):
- 0 .. 4 (5 regs)
- 20 .. 25 (6 regs)
== Slave 1 probe (8 samples/reg):
- reg 0 CHANGING min=250 max=252 mean=250.6 looks like x10 fixed-point (e.g. 250 -> 25.0)
- reg 2 static value=263 (0x0107)
| Step | How |
|---|---|
| Slave discovery | Read Holding Registers (0x03), 1 register, addr 1-247; an exception response still proves the slave is alive |
| Register mapping | Block reads (default 8); an answering block proves its whole span; illegal-address blocks are hole-probed one register at a time |
| Probing | N samples per register: min/max/mean, CHANGING vs static, hex of static values |
| Hunches | Evidence-based only: "range fits 0-100 (percent?)", "looks like x10 fixed-point", "uses high bits". Never a claim. |
--demo simulates two virtual slaves in memory:
- slave 1 — temperature/humidity sensor: temp (x10, moving), humidity (x10, moving), firmware version, serial, status bits, calibration block
- slave 17 — I/O module: 8 static channel registers
so you can see discovery → mapping → probing in seconds without a single wire attached.
pip install pyserial
python modbus_lens.py --port COM3 --baud 9600 [--reg-hi 999] [--probe 8]- The full slave sweep (1-247) takes ~100 s at the default 0.4 s timeout; progress is printed to stderr so silence doesn't look like a hang.
- Register scan speed is bounded by the bus (65535 registers at 9600 baud
is hours), so keep
--reg-hisane first; widen after the first map. - Probing is a burst (N samples within a short window): minute-scale drift will read as static. Corrupted frames (CRC mismatch) are skipped, never fatal - a noisy link bends the map, it doesn't kill the scan.
--probe 0skips probing (discovery + mapping only)--slave-lo/--slave-hi,--reg-lo/--reg-hibound the scans- Probing reads are single-register; interleaved writes from other masters will show up as noise — that is evidence too
modbus-lens is part of the Lens series — one philosophy: face unknown hardware and protocols, report only evidence, never claim certainty.
| Tool | Domain | Status |
|---|---|---|
| SerialLens | Serial / UART protocol identification & decoding | ✅ available |
| modbus-lens | Modbus RTU slave discovery & register recon | ✅ available |
| can-lens | CAN bus traffic profiling | 🚧 planned |
| ble-lens | BLE GATT service recon | 💡 idea |
MIT
Full English documentation: README_EN.md