Categorygithub.com/grid-x/modbus
modulepackage
0.0.0-20250219144522-2b18d136199f
Repository: https://github.com/grid-x/modbus.git
Documentation: pkg.go.dev

# README

go modbus

Fault-tolerant, fail-fast implementation of Modbus protocol in Go.

Supported functions

Bit access:

  • Read Discrete Inputs
  • Read Coils
  • Write Single Coil
  • Write Multiple Coils

16-bit access:

  • Read Input Registers
  • Read Holding Registers
  • Write Single Register
  • Write Multiple Registers
  • Read/Write Multiple Registers
  • Mask Write Register
  • Read FIFO Queue

Supported formats

  • TCP
  • Serial (RTU, ASCII)
  • UDP

Usage

Basic usage:

// Modbus TCP
client := modbus.TCPClient("localhost:502")
// Read input register 9
results, err := client.ReadInputRegisters(8, 1)

// Modbus RTU/ASCII
// Default configuration is 19200, 8, 1, even
client = modbus.RTUClient("/dev/ttyS0")
results, err = client.ReadCoils(2, 1)

Advanced usage:

// Modbus TCP
handler := modbus.NewTCPClientHandler("localhost:502")
handler.Timeout = 10 * time.Second
handler.SlaveID = 0xFF
handler.Logger = log.New(os.Stdout, "test: ", log.LstdFlags)
// Connect manually so that multiple requests are handled in one connection session
err := handler.Connect()
defer handler.Close()

client := modbus.NewClient(handler)
results, err := client.ReadDiscreteInputs(15, 2)
results, err = client.WriteMultipleRegisters(1, 2, []byte{0, 3, 0, 4})
results, err = client.WriteMultipleCoils(5, 10, []byte{4, 3})
// Modbus RTU/ASCII
handler := modbus.NewRTUClientHandler("/dev/ttyUSB0")
handler.BaudRate = 115200
handler.DataBits = 8
handler.Parity = "N"
handler.StopBits = 1
handler.SlaveID = 1
handler.Timeout = 5 * time.Second

err := handler.Connect()
defer handler.Close()

client := modbus.NewClient(handler)
results, err := client.ReadDiscreteInputs(15, 2)

Modbus-CLI

We offer a CLI tool to read/write registers.

Usage

For simplicity, the following examples are all using Modbus TCP. For Modbus RTU, replace the address field and use the rtu- arguments in order to use different baudrates, databits, etc.

./modbus-cli -address=rtu:///dev/ttyUSB0 -rtu-baudrate=57600 -rtu-stopbits=2 -rtu-parity=N -rtu-databits=8 ...

For Modbus UDP, replace the address field with a UDP address.

./modbus-cli --address=udp://127.0.0.1:502 ...

Reading Registers

Read 1 register and get raw result

./modbus-cli -address=tcp://127.0.0.1:502 -quantity=1 -type-parse=raw -register=42

Read 1 register and decode result as uint16

./modbus-cli -address=tcp://127.0.0.1:502 -quantity=1 -type-parse=uint16 -register=42

Read 1 register and get all possible decoded results

./modbus-cli -address=tcp://127.0.0.1:502 -quantity=1 -type-parse=all -register=42

Read 2 registers and decode result as uint32

./modbus-cli -address=tcp://127.0.0.1:502 -quantity=2 -type-parse=uint32 -register=42

Read 2 registers and get all possible decoded results

./modbus-cli -address=tcp://127.0.0.1:502 -quantity=2 -type-parse=all -register=42

Reading multiple registers is only possible in the raw format

./modbus-cli -address=tcp://127.0.0.1:502 -quantity=16 -type-parse=raw -register=42

Writing Registers

Write 1 register

./modbus-cli -address=tcp://127.0.0.1:502 -fn-code=0x06 -type-exec=uint16 -register=42 -write-value=7

Write 2 registers

./modbus-cli -address=tcp://127.0.0.1:502 -fn-code=0x10 -type-exec=uint32 -register=42 -write-value=7

Release

To release the Modbus-CLI tool, run either make release if you have installed goreleaser or make ci_release. The generated files can be found in the directory in the dist directory.

Take the .tar.gz and .zip files and create a new GitHub release.

References

# Packages

No description provided by the author
No description provided by the author

# Functions

ASCIIClient creates ASCII client with default handler and given connect string.
ASCIIOverTCPClient creates ASCII over TCP client with default handler and given connect string.
NewASCIIClientHandler allocates and initializes a ASCIIClientHandler.
NewASCIIOverTCPClientHandler allocates and initializes a ASCIIOverTCPClientHandler.
NewClient creates a new modbus client with given backend handler.
NewClient2 creates a new modbus client with given backend packager and transporter.
NewRTUClientHandler allocates and initializes a RTUClientHandler.
NewRTUOverTCPClientHandler allocates and initializes a RTUOverTCPClientHandler.
NewRTUOverUDPClientHandler allocates and initializes a RTUOverUDPClientHandler.
NewTCPClientHandler allocates a new TCPClientHandler with the given options.
RTUClient creates RTU client with default handler and given connect string.
RTUOverTCPClient creates RTU over TCP client with default handler and given connect string.
RTUOverUDPClient creates RTU over UDP client with default handler and given connect string.
TCPClient creates TCP client with default handler and given connect string.
WithTLSConfig returns a TCPClientHandlerOption that enables TLS encryption with the given options.

# Constants

ExceptionCodeAcknowledge error code.
ExceptionCodeGatewayPathUnavailable error code.
ExceptionCodeGatewayTargetDeviceFailedToRespond error code.
ExceptionCodeIllegalDataAddress error code.
ExceptionCodeIllegalDataValue error code.
ExceptionCodeIllegalFunction error code.
ExceptionCodeMemoryParityError error code.
ExceptionCodeServerDeviceBusy error code.
ExceptionCodeServerDeviceFailure error code.
FuncCodeMaskWriteRegister 16-bit wise access.
FuncCodeReadCoils for bit wise access.
FuncCodeReadDiscreteInputs for bit wise access.
FuncCodeReadFIFOQueue 16-bit wise access.
FuncCodeReadHoldingRegisters 16-bit wise access.
FuncCodeReadInputRegisters 16-bit wise access.
FuncCodeReadWriteMultipleRegisters 16-bit wise access.
FuncCodeWriteMultipleCoils for bit wise access.
FuncCodeWriteMultipleRegisters 16-bit wise access.
FuncCodeWriteSingleCoil for bit wise access.
FuncCodeWriteSingleRegister 16-bit wise access.

# Structs

ASCIIClientHandler implements Packager and Transporter interface.
ASCIIOverTCPClientHandler implements Packager and Transporter interface.
DataSizeError represents an error for invalid data-sizes i.e.
Error implements error interface.
InvalidLengthError is returned by readIncrementally when the modbus response would overflow buffer implemented to simplify testing.
ProtocolDataUnit (PDU) is independent of underlying communication layers.
RTUClientHandler implements Packager and Transporter interface.
RTUOverTCPClientHandler implements Packager and Transporter interface.
RTUOverUDPClientHandler implements Packager and Transporter interface.
TCPClientHandler implements Packager and Transporter interface.

# Interfaces

Client declares the functionality of a Modbus client regardless of the underlying transport stream.
ClientHandler is the interface that groups the Packager and Transporter methods.
Connector exposes the underlying handler capability for open/connect and close the transport channel.
Logger is the interface to the required logging functions.
Packager specifies the communication layer.
Transporter specifies the transport layer.

# Type aliases

ErrADURequestLength informs about a wrong ADU request length.
ErrADUResponseLength informs about a wrong ADU request length.
ErrTCPHeaderLength informs about a wrong header length.
TCPClientHandlerOption configures a TCPClientHandler.