Skip to content

Command-line tool

modbusctl is a command-line tool for Modbus over TCP, UDP and serial lines (RTU and ASCII). It is built on go-modbus and covers the whole path from “what is this device?” to a hardware-free replay of it. Output is text, a table or JSON, with stable exit codes for scripts and CI. This page follows modbusctl v3.5.1, built on go-modbus v1.11.1.

Terminal window
curl -fsSL https://get.otfabric.io/modbusctl | sh

Binaries for Linux (x86-64, ARM64, ARMv7), macOS (Intel, Apple silicon) and Windows (x86-64, ARM64). The installer checks the release's SHA-256 checksum, installs per user without sudo (~/.local/bin, on Windows %LOCALAPPDATA%\OTFabric\bin) and upgrades in place when you run it again. Pin a version with OTF_VERSION; the CI line installs into a fixed directory without touching PATH or shell profiles. All installer options, release archives for offline machines. To build from source instead: make build in a checkout. Check the install with modbusctl version.

For a serial port without sudo on Linux, add your user to the dialout group: sudo usermod -aG dialout "$USER", then log out and back in.

Every client command takes either --url or --ip with an optional --port (Modbus TCP). The two styles are mutually exclusive.

URL Transport
tcp://host:port Modbus TCP (same as --ip host --port port)
tcp+tls://host:port Modbus/TCP Security: Modbus TCP over TLS with a client certificate (port 802 by convention)
udp://host:port Modbus TCP framing over UDP
rtu://<device> Modbus RTU on a serial line
ascii://<device> Modbus ASCII on a serial line
rtuovertcp://host:port RTU framing through a serial-to-Ethernet bridge
rtuoverudp://host:port RTU framing over UDP
asciiovertcp://host:port ASCII framing through a serial-to-Ethernet bridge

The serial flags apply to rtu:// and ascii:// and are ignored for network URLs:

Flag Values Default
--baud any rate the adapter supports 19200
--data-bits 5, 6, 7, 8 8 for RTU, 7 for ASCII
--parity none, even, odd none
--stop-bits 1, 2 2 without parity, 1 with
Terminal window
# RTU at 9600 8E1, unit 3
modbusctl client read --url rtu:///dev/ttyUSB0 --baud 9600 --parity even --unit 3 --start 0 --count 4
# RTU device behind a serial-to-Ethernet bridge
modbusctl client scan --url rtuovertcp://192.168.1.50:4001 --unit 7 --start 0 --end 200 --output scan.mbcap

--unit accepts a single ID, a range (1-10), a list (1,5,25), a mix (1-10,255) or all on the commands that probe several units. Each absent unit costs one --timeout, so use a short timeout (for example --timeout 300) when sweeping a bus. On serial lines --parallel is ignored.

A tcp+tls:// device authenticates the client by its certificate (mutual TLS, as the Modbus/TCP Security specification requires), and the client checks the device’s certificate. Three flags, on every client command and ignored for other URLs, give the PEM files:

Flag Environment Content
--tls-cert MODBUSCTL_TLS_CERT Client certificate (required); the file may also hold the key
--tls-key MODBUSCTL_TLS_KEY Client private key (default: read from --tls-cert)
--tls-ca MODBUSCTL_TLS_CA CA certificates that sign the device certificate (default: the system roots)
Terminal window
modbusctl client read --url tcp+tls://192.168.1.10:802 --tls-cert client.pem --tls-key client-key.pem --tls-ca plant-ca.pem --start 100 --count 4

The device certificate must name the host of the URL (an IP address SAN for tcp+tls://192.168.1.10:802, a DNS name otherwise). A missing or unreadable file is invalid input (exit 3) and nothing is sent; a device the CA does not vouch for, or one that rejects the client certificate, is a transport error (exit 4). A capture made over TLS records the transport as tls, never the certificate files.

modbusctl
├── client
│ ├── identify FC43 Read Device Identification (+ optional FC17)
│ ├── fingerprint Probe supported read functions per unit
│ ├── diagnostic FC08 Diagnostics
│ ├── reportserverid FC17 Report Server ID
│ ├── read Read registers (FC01/FC02/FC03/FC04)
│ ├── decode Read and decode registers via a codec (FC03/FC04)
│ ├── codecs List available register codecs
│ ├── poll Poll coils/registers at an interval
│ ├── write Write coils (FC05/FC15) or holding registers (FC06/FC16)
│ ├── scan Scan for readable register blocks
│ ├── record Record every spec read function over time to MBCAP
│ └── sunspec detect | models | map | probe
├── server
│ ├── static Serve the values of a capture
│ └── replay Replay a capture over time, at any speed
├── capture decode | extract | strings | frequencies
├── discover Discover Modbus devices on subnets
├── completion bash | zsh | fish | powershell
└── version
  1. Identify the device and check which read functions it answers:

    Terminal window
    modbusctl client identify --ip 192.168.1.10 --server-id
    modbusctl client fingerprint --ip 192.168.1.10 --unit 1-10
  2. Find the readable register blocks. Start gently:

    Terminal window
    modbusctl client scan --ip 192.168.1.10 --start 0 --end 1000 --delay 100 --output scan.mbcap
  3. Decode a value you found:

    Terminal window
    modbusctl client decode --ip 192.168.1.10 --start 100 --codec uint32/layout:4321 --count 8 --format json
  4. Watch it change:

    Terminal window
    modbusctl client poll --ip 192.168.1.10 --function 4 --start 0 --codec float32/layout:4321 --interval 500 --polls 20

client scan --algo selects the strategy. The default is safe.

Algorithm Use it for
safe Fragile devices and first passes. Linear, descending block sizes, fixed delay, no retry storms.
smart Unknown devices. Adaptive interval splitting. The README calls it the best default.
deep smart plus refinement around block edges. Lab work and reverse engineering.
stepped Cheap triage of a huge range at --step positions.
linear Long contiguous maps, such as PLCs.
boundary Grow a known-good read (--seed-start, --seed-count) to its full extent.
sunspec Find the SunSpec base and capture every model to an MBCAP file.

ALGOS.md specifies each algorithm, including worst-case request counts.

client decode, client poll and client write share the go-modbus codec registry. modbusctl client codecs lists the IDs, with filters such as --family float or --registers 2. Examples: float32/layout:4321 (big-endian float), uint32/layout:2143 (word-swapped), ascii/registers:8, ip_addr.

Terminal window
# One holding register (FC06)
modbusctl client write --ip 192.168.1.10 --start 100 --values 1234 --yes
# Switch coil 8 on (FC05)
modbusctl client write --ip 192.168.1.10 --function 5 --start 8 --values on --yes
# A big-endian float32 setpoint: two registers, FC16
modbusctl client write --ip 192.168.1.10 --start 200 --codec float32/layout:4321 --values 21.5 --yes

The safety model is explicit:

  • client write and the state-changing client diagnostic sub-functions (restartcommunications, changeasciiinputdelimiter, forcelistenonlymode, clearcountersanddiagnosticreg, clearoverruncounterandflag) need --yes. Without it they exit 2 (confirmation_required) and send nothing. --yes has no environment variable, so it cannot be set by accident.
  • --start is required. There is no default address for a write.
  • Values, codec, quantity limits and the address range are all validated before anything is sent. A rejected command exits with code 2 or 3 and has sent nothing. Quantities are limited per function code, and ranges past 65535 are refused.
  • A write is sent exactly once and never retried. If it times out (exit 5), read the value back before sending it again.
  • There is no dry-run mode. Rehearse against a simulator.

--format text|json|table (or MODBUSCTL_FORMAT; the older MODBUSCTL_OUTPUT_FORMAT is still accepted) controls the final result on stdout for the client commands. Progress for scan and record goes to stderr, so stdout stays a single parseable result. Capture files take their encoding from the file name (.mbcap, .mbcap.jsonl, .mbcap.csv). client poll --format json writes one JSON object per poll (JSON Lines). With --format json, every fatal error, including an unknown flag or command, is one {"error":{"kind","code","message"}} object on stdout, with nothing on stderr unless --debug is set. server static and server replay print their progress to stderr.

Code Meaning
0 Success
2 Usage: unknown command or flag, wrong arguments, or a write without --yes (confirmation_required)
3 Invalid input, flags or configuration
4 Transport: TCP or UDP dial failure, or a serial port that cannot be opened
5 Timeout: a request or network timeout on a client command (SunSpec and FC08 diagnostics report timeouts as 6)
6 Protocol error or Modbus exception
7 Partial success: valid output, but some units, scan requests or polls failed
10 Output, internal or unknown error
130 Interrupted by Ctrl-C or SIGTERM before a result

The codes follow the OT Fabric CLI exit-code table shared by every *ctl tool; 1, 8 and 9 are not used. Match on the exit code or the JSON code, not on message text.

Most flags have a MODBUSCTL_* equivalent, for example MODBUSCTL_URL, MODBUSCTL_IP, MODBUSCTL_UNIT, MODBUSCTL_TIMEOUT (milliseconds; 0 means 10000), MODBUSCTL_BAUD, MODBUSCTL_PARITY, MODBUSCTL_CODEC and MODBUSCTL_ALGO. Note that client read, decode, poll and write take their start address from MODBUSCTL_ADDRESS, while client scan uses MODBUSCTL_START.

Terminal window
MODBUSCTL_URL=rtu:///dev/ttyUSB0 MODBUSCTL_BAUD=9600 MODBUSCTL_PARITY=even MODBUSCTL_UNIT=3 \
modbusctl client poll --start 0 --count 4

client read, client scan and client record write MBCAP capture files; server static, server replay and the capture commands read them. MBCAP is the capture format of go-modbus-capture: every answered read is stored as its exact request and response PDUs, with the time the response arrived, for several devices and acquisition streams in one file. Its tool mbcapctl inspects, verifies, converts, filters, merges, splits, trims and repairs capture files; modbusctl does not duplicate those. Captures and traces describes the format and the tools around it.

Terminal window
# Turn a scan into a plan, then record those blocks every second for a minute
modbusctl capture extract --input scan.mbcap --output register-ranges.json
modbusctl client record --ip 192.168.1.10 --blocks-file register-ranges.json --interval 1000 --duration 60000 --output session.mbcap
# Check the file, and turn it into JSON Lines to read or diff
mbcapctl verify session.mbcap
mbcapctl convert session.mbcap session.mbcap.jsonl
# Serve it as a Modbus TCP device: its last values, or replayed over time
modbusctl server static --input session.mbcap --port 1502
modbusctl server replay --input session.mbcap --port 1502 --speed 10 --loops 0

--interval and --duration are milliseconds. A failed or retried attempt leaves no sample, exception responses are kept only with --record-exceptions, and the file is flushed every 10 seconds, so an interrupted recording is readable up to the last flush.

Captures hold the read functions of the Modbus specification and nothing else: FC01 to FC04, FC07, FC08 (the sub-functions that return data: 0x00, 0x02, 0x0B to 0x12), FC11, FC12, FC17, FC20, FC24 and FC43 Read Device Identification. The plan file of client record names them:

[
{"start_address": 100, "register_count": 10},
{"function": 1, "start_address": 0, "register_count": 16},
{"function": 8, "sub_function": 0, "data": "A537"},
{"function": 17},
{"function": 20, "file_records": [{"file": 1, "record": 0, "length": 10}]},
{"function": 24, "fifo_address": 1234},
{"function": 43, "device_id_code": 1}
]

An entry without function is an address block read with --function, which is what client scan and capture extract write. Any other function code, vendor and unknown ones included, or an FC08 sub-function that changes device state, is refused before anything is sent, and a capture file holding one is refused when loaded (capture_unsupported_function, exit 3).

server static serves the last recorded values, or the values at --at (a time since the start of the capture). server replay follows the recording at its original pace (--speed 1), faster (--speed 60, a minute per second) or slower (--speed 0.1); --loops 0 repeats forever, --loops N plays N times and then keeps the last values, and --from starts part-way in.

Flag Effect
--target, --unit Every target of a capture answers on its recorded unit ID. --target picks one, --unit serves it on another unit ID
--missing exception|zero|silence The answer for data the capture does not hold. Default: Illegal Function for a function never recorded, Illegal Data Address otherwise
--before-first hold|absent Replay only: before an address is first sampled, serve its first sample (default) or treat it as unrecorded
--listen Address to listen on, for example 127.0.0.1. Default: every interface
--max-clients Concurrent connections served (default 10); further ones are closed
--tls-cert, --tls-key, --tls-ca Serve Modbus/TCP Security. --tls-ca is required: every client must present a certificate it signed

FC01 to FC04 are answered from the values recorded per address, also for a range that was never requested as such, as long as every address in it was recorded. FC43 is answered from the recorded objects, and the other functions with the response recorded for exactly that request. Writes are refused with Illegal Function: a replayed device is read-only. Nothing is decoded or interpolated: between two samples the earlier value holds, and what was never recorded does not exist. On shutdown both servers print what they served, including the requests for data the capture does not hold.

Terminal window
# Serve a capture over TLS on the Modbus/TCP Security port
modbusctl server static --input plant.mbcap --port 802 --tls-cert server.pem --tls-key server-key.pem --tls-ca plant-ca.pem

The capture commands work on the FC01 to FC04 samples of one target and one function; a capture with several takes --target and --function.

Terminal window
modbusctl capture decode --input site.mbcap --target 2 --function 4 --profile meter.json
modbusctl capture strings --input session.mbcap # device names, in four byte orders
modbusctl capture frequencies --input session.mbcap # registers that read like ~50 Hz

Text and table output escape control characters, bidi controls and invalid UTF-8 in strings that come from a device or a file, so a device cannot inject terminal escape sequences; JSON output is unchanged. modbusctl does not write through a symbolic link at an output path, refuses an --output that names the command’s own input, and creates captures, exports and discovery results with mode 0600. Blocks files and device profiles over 16 MiB are refused.

discover is Modbus TCP only. Unless you pass --listen, the servers listen on all interfaces, and without TLS they accept any client, so run them on trusted networks. discover caps large subnets unless you pass --force-large-scan, and --resolve-mac uses raw sockets that may need CAP_NET_RAW.

client sunspec does transport-level SunSpec discovery: the SunS marker, the base address and the model headers. It does not decode points; use sunspecctl for that.

Terminal window
modbusctl client sunspec detect --url tcp://192.168.1.10:502 --unit 1
modbusctl client sunspec models --ip 192.168.1.10 --unit 1 --base 40000 --format json
modbusctl client sunspec probe --url tcp://192.168.1.10:502 --unit 1