Skip to content

Instantly share code, notes, and snippets.

@kalkov
Last active July 22, 2025 12:28
Show Gist options
  • Save kalkov/478bfa7104018920689ce7e4504bc0b2 to your computer and use it in GitHub Desktop.
Save kalkov/478bfa7104018920689ce7e4504bc0b2 to your computer and use it in GitHub Desktop.
Give me a short one - for title

This Bash script is a Modbus device scanner for a serial RTU (RS-485) bus using mbpoll. It attempts to detect a Modbus-compatible device (like an Orno power meter) by cycling through common communication settings.

πŸ” What it does, step-by-step: Loops over baud rates: 9600, 4800, 2400, 1200

Loops over parity settings: none, even, odd

For each combination of baud rate and parity, it:

Tries Modbus slave addresses 1 through 10

Uses mbpoll to read holding register 305 (typically voltage or similar)

If a response is received (detected via [305] in output), it:

βœ… Prints success and breaks the loop, skipping to the next combination

If no response:

❌ Prints failure and waits 0.25s before trying the next address

#!/bin/bash
for baud in 9600 4800 2400 1200; do
for parity in none even odd; do
echo "=== Testing baudrate $baud with parity $parity ==="
for addr in $(seq 1 10); do
echo -n "Polling address $addr... "
if sudo mbpoll -m rtu -a $addr -b $baud -P $parity -d 8 -s 1 /dev/ttyUSB0 -r 305 -t 4 -1 -c 1 2>/dev/null | grep -q '\[305\]'; then
echo "βœ… Device responded at ID $addr"
break
else
echo "❌"
sleep 0.25
fi
done
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment