Skip to content

Go library

go-modbus is a pure Go Modbus client and server library (no CGO). It implements the Modbus Application Protocol Specification V1.1b3 over Modbus TCP, Modbus over TLS (MBAPS), Modbus RTU and Modbus ASCII on serial lines, RTU and ASCII framing tunnelled over TCP, RTU over UDP, and Modbus UDP. The full API reference is API.md in the repository.

Terminal window
go get github.com/otfabric/go-modbus

go-modbus requires Go 1.26 or later; CI tests Go 1.26 and 1.27. Its only dependency is go-serial, used for RTU and ASCII. The bundled command-line tool is a module of its own since v1.10.0, so the library no longer pulls in a CLI framework. This page follows go-modbus v1.12.0. In v1.12.0 the bundled modbus-cli exits with the organisation’s exit codes (2 usage, 3 invalid TLS files, 4 connection, 5 timeout, and so on) instead of 1 for every failure; the library is unchanged. Install modbus-cli with curl -fsSL https://get.otfabric.io/modbus-cli | sh, or on Windows irm https://get.otfabric.io/modbus-cli.ps1 | iex.

The library is organized in tiers. Use the lowest one that does the job:

Package Purpose
modbus Raw function-code methods, client configuration, server, errors, retry, metrics
modbus/codec Typed values over several registers (uint32, float32, strings, BCD, timestamps, IP addresses) with explicit byte and word layout
modbus/batch Plans a set of named reads into the fewest requests and executes them, with a state for every request and item
modbus/sunspec SunSpec marker detection and model-chain enumeration (headers only, no point decoding)

modbus.New validates a Config and returns a *Client. It does not open a connection; Open does. The URL scheme selects the transport.

main.go
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/otfabric/go-modbus"
"github.com/otfabric/go-modbus/codec"
)
func main() {
client, err := modbus.New(modbus.Config{
URL: "tcp://192.168.1.10:502",
Timeout: 2 * time.Second,
})
if err != nil {
log.Fatal(err)
}
if err := client.Open(); err != nil {
log.Fatal(err)
}
defer client.Close()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
// FC03: four raw holding registers from unit 1, starting at address 100.
regs, err := client.ReadRegisters(ctx, 1, 100, 4, modbus.HoldingRegister)
if err != nil {
log.Fatal(err)
}
fmt.Println(regs)
// The same registers as a big-endian float32 (two registers, layout 4321).
v, err := codec.ReadFromClient(client, ctx, 1, 100, modbus.HoldingRegister,
codec.MustNewFloat32Codec(codec.Layout32_4321))
if err != nil {
log.Fatal(err)
}
fmt.Println(v)
}

For larger setups, modbus.NewConfig(TransportConfig, ExecutionConfig, ObservabilityConfig) builds the same Config from grouped parts.

client, err := modbus.New(modbus.Config{
URL: "tcp://plc.local:502",
Timeout: 2 * time.Second,
DialTimeout: 5 * time.Second,
})
Config field Applies to Default
Timeout all 300 ms for RTU, 1 s for TCP and UDP modes
DialTimeout network transports 5 s for TCP and UDP, 15 s for TLS
Speed, DataBits, Parity, StopBits rtu://, ascii:// 19200 bps; 8 data bits (RTU) or 7 (ASCII); no parity; 2 stop bits without parity, 1 with
TLSClientCert, TLSRootCAs tcp+tls:// required
MinConns, MaxConns tcp, udp, rtuovertcp, rtuoverudp, asciiovertcp single connection

RTU and ASCII use go-serial underneath. You do not call it directly: the Speed, DataBits, Parity and StopBits fields are passed through. Serial device names are /dev/ttyUSB0 on Linux, /dev/cu.usbserial-1420 on macOS and COM3 on Windows.

go-serial is also usable on its own for non-Modbus serial work. It runs on Linux, macOS, Windows, FreeBSD, NetBSD and OpenBSD, offers optional RS-485 driver configuration (Config.RS485.Enabled, platform dependent), returns serial.ErrTimeout on read timeouts, and validates configuration at Open with a *serial.ConfigError. Its modbus subpackage has a 19200 8E1 preset, modbus.DefaultRTUConfig(address). Note that this preset (even parity, 1 stop bit) differs from the go-modbus serial defaults (no parity, 2 stop bits); both are valid Modbus RTU settings, so match the device.

Every client method takes a context.Context first and the unit ID second. A request ends at Config.Timeout or at the context’s deadline, whichever comes first: an earlier context deadline ends it early, a later one does not extend it (since v1.9.0). To give a slow device more time, raise Timeout.

coils, err := client.ReadCoils(ctx, 1, 0, 8) // FC01
in, err := client.ReadInputRegisters(ctx, 1, 0, 10) // FC04
err = client.WriteCoil(ctx, 1, 8, true) // FC05
err = client.WriteRegister(ctx, 1, 100, 1234) // FC06
err = client.WriteRegisters(ctx, 1, 100, []uint16{10, 255, 65535}) // FC16
di, err := client.ReadAllDeviceIdentification(ctx, 1) // FC43/14
for _, obj := range di.Objects {
fmt.Printf("%s = %s\n", obj.Name, obj.Value)
}

Typed writes use the codec package: codec.WriteToClient(client, ctx, unitID, addr, value, enc). Common layouts are codec.Layout32_4321 (big-endian, ABCD) and codec.Layout32_2143 (word-swapped, CDAB), with 16, 48 and 64-bit equivalents. CODECS.md lists every codec with its stable ID.

The client also covers FC07, FC08 diagnostics, FC11, FC12, FC17, FC20, FC21, FC22 (MaskWriteRegister), FC23 (ReadWriteMultipleRegisters) and FC24. To find out what a unit supports, SupportsFunction(ctx, unitID, fc) probes one read-style function code, and ProbeFunction returns a ProbeResult with the outcome, exception code and raw payload.

The batch package (since v1.8.0) plans a set of named reads once and executes them on a client. Compile validates the items and groups them by unit ID and function code (FC01 to FC04) into windows: contiguous, overlapping and duplicate items share one request, and MaxGap opts in to reading unrequested addresses between items. MaxRegisters, MaxBits and Boundaries respect what the device accepts. Invalid input is a *batch.ValidationError, before any I/O.

plan, err := batch.Compile([]batch.Item{
{Name: "voltage", UnitID: 1, FC: modbus.FCReadHoldingRegisters, Addr: 100,
Codec: codec.MustNewFloat32Codec(codec.Layout32_4321)},
{Name: "current", UnitID: 1, FC: modbus.FCReadHoldingRegisters, Addr: 102,
Codec: codec.MustNewFloat32Codec(codec.Layout32_4321)},
{Name: "alarms", UnitID: 1, FC: modbus.FCReadCoils, Addr: 0, Count: 16},
}, batch.Options{MaxGap: 2})
if err != nil {
log.Fatal(err)
}
res, err := plan.Execute(ctx, client) // *modbus.Client satisfies batch.Reader
for _, it := range res.Items {
fmt.Println(it.Name, it.State, it.Value, it.Bits)
}
  • A Plan is immutable and safe for concurrent use. Stats counts requests and PDU bytes, and FunctionCodes lists the codes the plan needs, for Config.AllowedFunctionCodes.
  • Execute reads the windows one after another through the client’s own read methods, so its timeout, retry policy and metrics apply. It adds no goroutines, timers, caches or retries of its own.
  • A Modbus exception fails only its window. A timeout or transport error ends the batch unless Options.ContinueOnTransportError is set. A cancelled context always ends it.
  • Every window and item has a State: OK, Failed, DecodeFailed or NotAttempted. A batch that fails part-way returns what it read, with an *batch.ExecError that matches batch.ErrIncomplete.
  • Plan.Decode builds the same result from captured window data, without a client.

The sunspec subpackage detects the SunS marker and enumerates the model chain (model ID and length only). It reads through a one-method sunspec.Reader interface, so *modbus.Client needs a small adapter:

type reader struct{ client *modbus.Client }
func (r reader) ReadRawBytes(ctx context.Context, unitID uint8, addr, byteCount uint16, regType sunspec.RegType) ([]byte, error) {
return r.client.ReadRegisterBytes(ctx, unitID, addr, byteCount, regType)
}
result, err := sunspec.Discover(ctx, reader{client}, &sunspec.Options{UnitID: 1, RegType: sunspec.HoldingRegister})

For decoded, scaled SunSpec points, use go-sunspec.

The server supports tcp:// and tcp+tls:// only. You implement RequestHandler; the server owns framing, validation and exceptions.

type handler struct {
mu sync.Mutex
regs [65536]uint16
}
func (h *handler) HandleHoldingRegisters(ctx context.Context, req *modbus.HoldingRegistersRequest) ([]uint16, error) {
h.mu.Lock()
defer h.mu.Unlock()
if req.IsWrite {
for i, v := range req.Args {
h.regs[req.Addr+uint16(i)] = v
}
return nil, nil
}
out := make([]uint16, req.Quantity)
copy(out, h.regs[req.Addr:])
return out, nil
}
func (h *handler) HandleCoils(ctx context.Context, req *modbus.CoilsRequest) ([]bool, error) {
return nil, modbus.ErrIllegalDataAddress
}
func (h *handler) HandleDiscreteInputs(ctx context.Context, req *modbus.DiscreteInputsRequest) ([]bool, error) {
return nil, modbus.ErrIllegalDataAddress
}
func (h *handler) HandleInputRegisters(ctx context.Context, req *modbus.InputRegistersRequest) ([]uint16, error) {
return nil, modbus.ErrIllegalDataAddress
}
server, err := modbus.NewServer(&modbus.ServerConfig{URL: "tcp://[::]:5020", MaxClients: 20}, &handler{})
if err != nil {
log.Fatal(err)
}
if err := server.Start(); err != nil {
log.Fatal(err)
}
defer server.Shutdown(context.Background())
  • Returning a Modbus sentinel such as modbus.ErrIllegalDataAddress sends that exception. Any other error becomes Server Device Failure. Panics are recovered and also answered with Server Device Failure.
  • Each connection runs in its own goroutine, so handlers must be safe for concurrent use.
  • The other functions of the specification are served if the handler also implements their optional interface: ExceptionStatusHandler (FC07), DiagnosticsHandler (FC08), CommEventCounterHandler (FC11), CommEventLogHandler (FC12), ReportServerIDHandler (FC17), ReadFileRecordHandler and WriteFileRecordHandler (FC20, FC21), MaskWriteHandler (FC22), ReadWriteHandler (FC23), FIFOQueueHandler (FC24) and DeviceIdentificationHandler (FC43/14). The server parses and validates each request, the handler supplies the data, and the server encodes the response. Without the interface the answer is Illegal Function.
  • Vendor, user-defined and unknown function codes are not supported: they always get Illegal Function, and the connection stays usable. So does FC43 with an MEI type other than 14.
  • Returning modbus.ErrNoResponse sends no answer at all and keeps the connection, as a serial device does for Diagnostics Force Listen Only Mode.
  • The server does not filter unit IDs. That is the handler’s job (req.UnitID).
  • ServerConfig.Timeout is the idle session timeout (default 120 s). A response that cannot be written in full within it closes the connection, so a client that sends requests but never reads loses its slot. MaxClients defaults to 10.
  • On TLS, req.ClientRole carries the role from the client certificate. Check it before serving writes.

Config.AllowedFunctionCodes limits the function codes a client sends. A request with any other function code fails with ErrFunctionCodeNotAllowed before a connection is dialled or a byte is sent. Nil or empty, the default, means no restriction.

// A read-only client for holding and input registers.
client, err := modbus.New(modbus.Config{
URL: "tcp://plc:502",
AllowedFunctionCodes: []modbus.FunctionCode{
modbus.FCReadHoldingRegisters, modbus.FCReadInputRegisters,
},
})

Config.PDUTap receives the raw request and response PDUs (function code and data, without MBAP, CRC or LRC framing) of every answered request, with the time the response arrived. It is what a recorder needs to capture a device and serve it again later; modbusctl writes its MBCAP captures from it.

conf.PDUTap = modbus.PDUTapFunc(func(ctx context.Context, ex modbus.PDUExchange) {
log.Printf("unit %d: % X -> % X", ex.UnitID, ex.Request, ex.Response)
})
  • The tap is called synchronously, once per answered request, and must not block.
  • Every request on the wire is reported: reads, writes and probes. On a retry only the attempt that returned the response is reported.
  • It is called before the method validates the response, so exception responses and responses the method then rejects are reported too.
  • A request refused by AllowedFunctionCodes, a transport error or a timeout is not reported. With no tap set, nothing is copied.

Errors fall into five categories. Use errors.Is and errors.As:

Situation Reported as
Bad configuration *ConfigurationError (ErrConfigurationError)
Invalid arguments, nothing sent *ParameterError (ErrUnexpectedParameters)
Malformed or unexpected response *ProtocolError (ErrProtocolError), or ErrBadCRC, ErrBadLRC, ErrShortFrame
Device answered with an exception *ExceptionError, matches ErrIllegalDataAddress and the other sentinels
Timeout ErrRequestTimedOut
Connection lost or refused standard library error (io.EOF, *net.OpError)
_, err := client.ReadRegisters(ctx, 1, 0x9000, 10, modbus.HoldingRegister)
var exc *modbus.ExceptionError
switch {
case errors.As(err, &exc):
log.Printf("device exception: fc=0x%02x code=0x%02x", exc.FunctionCode, exc.ExceptionCode)
case errors.Is(err, modbus.ErrRequestTimedOut):
log.Print("timeout")
case err != nil:
log.Print(err)
}

Treat a protocol error differently from an exception: the first means the link or the peer misbehaved, the second is a well-formed answer from the device.

  • Reconnects. An open client stays open. On tcp:// and tcp+tls://, a timeout, framing error or transport error closes that connection and the next request dials a new one. A Modbus exception does not. Serial-framed transports and udp:// keep their link unless it is down.
  • Retries. Off by default. modbus.ExponentialBackoff(base, maxDelay, maxAttempts) or modbus.NewExponentialBackoff(cfg) retry only known transient transport errors. Timeouts are retried only with RetryOnTimeout: true. Exceptions and protocol errors are never retried.
  • Concurrency. A *Client is safe for concurrent use. With MaxConns of 0 or 1, requests are serialized on one connection. With MaxConns > 1 on a poolable transport, requests run in parallel on pooled connections. Serial and TLS always use one connection.

Client logging is off by default. Set Config.Logger with modbus.NewStdLogger, modbus.NewSlogLogger, modbus.NewSlogFieldLogger or modbus.NopLogger(). Debug level can log raw TX and RX frames, including register values; treat those logs as sensitive. The server is silent by default too: a nil ServerConfig.Logger disables logging.

Metrics are callbacks. Implement ClientMetrics (OnRequest, OnResponse, OnError, OnTimeout), optionally AttemptMetrics for per-attempt and re-dial events, or ServerMetrics, and assign it to the Metrics field. Callbacks run on the request path and must not block. client.Info() returns a snapshot for health checks.

  • The server is TCP and TLS only. There is no RTU or ASCII server.
  • FC43 MEI type 13 (CANopen) is not supported.
  • UDP transports are not in the Modbus specification and assume one request and one response per datagram with no loss or reordering. Validate them against your device before relying on them.
  • The sunspec subpackage does not decode points or scale factors.
  • The serial framings carry no transaction ID. After a failed exchange the client drains the link for a short window (3 × t3.5 for RTU, 20 ms for ASCII); an answer later than that can still be read as the answer to the next request.
  • The interop suite covers Modbus TCP only. Other transports are covered by the repository’s own tests. The library has not been run against certified test equipment.