modbus

The modbus package provides a convenience-oriented API for working with Modbus devices over RTU and TCP transports.

Index

Functions

func NewClient

1func NewClient(cfg *Config) (*Client, error)

NewClient creates new Modbus client and connects to the server.

This example demonstates how to connect over USB.

 1cfg, err := modbus.NewConfig("rtu:///dev/ttyUSB0")
 2if err != nil {
 3	panic(err)
 4}
 5cfg.UnitId = 1
 6cfg.Parity = modbus.ParityEven
 7
 8client, err := modbus.NewClient(cfg)
 9if err != nil {
10	panic(err)
11}
12
13value, err := client.ReadRegister(10, modbus.RegisterTypeHolding)
14if err != nil {
15	panic(err)
16}
17println("Value at address 10 =", value)

This example demonstrates how to create a TCP connection to a Modbus server and read the value of the holding register at address 10.

 1cfg, err := modbus.NewConfig("tcp://localhost:502")
 2if err != nil {
 3	panic(err)
 4}
 5cfg.UnitId = 1
 6client, err := modbus.NewClient(cfg)
 7if err != nil {
 8	panic(err)
 9}
10
11value, err := client.ReadRegister(10, modbus.RegisterTypeHolding)
12if err != nil {
13	panic(err)
14}
15println("Value at address 10 =", value)

func NewConfig

1func NewConfig(uri string) (*Config, error)

NewConfig creates a Config from a Modbus URI and applies reasonable defaults depending on the selected transport.

The following URL schemes are supported:

  • rtu://
  • tcp://
  • rtu:// – modbus RTU (serial, over both RS-232 and RS-485)
  • tcp:// – modbus TCP (a.k.a. MBAP)
  • udp:// or tcpoverudp:// – modbus TCP over UDP (a.k.a. MBAP over UDP),
  • rtuovertcp:// – RTU over TCP (RTU tunneled in TCP for use with e.g. remote serial ports or cheap TCP to serial bridges)
  • rtuoverudp:// – modbus RTU over UDP (RTU tunneled in UDP)

This example demonstrates how to create a serial (RTU) configuration.

1cfg, err := modbus.NewConfig("rtu:///dev/ttyUSB0")
2if err != nil {
3	panic(err)
4}
5cfg.UnitId = 1

This example demonstrates how to create a TCP configuration.

1cfg, err := modbus.NewConfig("tcp://localhost:502")
2if err != nil {
3	panic(err)
4}
5cfg.UnitId = 1

Types

type Client

1type Client struct {
2}

Client wraps modbus.ModbusClient and provides a simplified, typed API for common Modbus read and write operations.

func Disconnect

1func (cl *Client) Disconnect() error

Disconnect closes the connection.

func ReadBytes

1func (cl *Client) ReadBytes(address uint16, quantity uint16, registerType RegisterType) ([]byte, error)

func ReadCoil

1func (cl *Client) ReadCoil(address uint16) (bool, error)

func ReadCoils

1func (cl *Client) ReadCoils(address uint16, quantity uint16) ([]bool, error)

func ReadDiscreteInput

1func (cl *Client) ReadDiscreteInput(address uint16) (bool, error)

func ReadDiscreteInputs

1func (cl *Client) ReadDiscreteInputs(address uint16, quantity uint16) ([]bool, error)

func ReadFloat32

1func (cl *Client) ReadFloat32(address uint16, registerType RegisterType) (float32, error)

func ReadFloat32s

1func (cl *Client) ReadFloat32s(address uint16, quantity uint16, registerType RegisterType) ([]float32, error)

func ReadFloat64

1func (cl *Client) ReadFloat64(address uint16, registerType RegisterType) (float64, error)

func ReadFloat64s

1func (cl *Client) ReadFloat64s(address uint16, quantity uint16, registerType RegisterType) ([]float64, error)

func ReadInt16

1func (cl *Client) ReadInt16(address uint16, registerType RegisterType) (int16, error)

func ReadInt16s

1func (cl *Client) ReadInt16s(address uint16, quantity uint16, registerType RegisterType) ([]int16, error)

func ReadRegister

1func (cl *Client) ReadRegister(address uint16, registerType RegisterType) (uint16, error)

func ReadRegisters

1func (cl *Client) ReadRegisters(address uint16, quantity uint16, registerType RegisterType) ([]uint16, error)

func ReadUint16

1func (cl *Client) ReadUint16(address uint16, registerType RegisterType) (uint16, error)

func ReadUint16s

1func (cl *Client) ReadUint16s(address uint16, quantity uint16, registerType RegisterType) ([]uint16, error)

func SetUnitId

1func (cl *Client) SetUnitId(id uint8) error

func WriteBytes

1func (cl *Client) WriteBytes(address uint16, values []byte) error

func WriteCoil

1func (cl *Client) WriteCoil(address uint16, value bool) error

func WriteCoils

1func (cl *Client) WriteCoils(address uint16, values []bool) error

func WriteFloat32

1func (cl *Client) WriteFloat32(address uint16, value float32) error

func WriteFloat32s

1func (cl *Client) WriteFloat32s(address uint16, values []float32) error

func WriteFloat64

1func (cl *Client) WriteFloat64(address uint16, value float64) error

func WriteFloat64s

1func (cl *Client) WriteFloat64s(address uint16, values []float64) error

func WriteRegister

1func (cl *Client) WriteRegister(address uint16, value uint16) error

func WriteRegisters

1func (cl *Client) WriteRegisters(address uint16, values []uint16) error

func WriteUint32

1func (cl *Client) WriteUint32(address uint16, value uint32) error

func WriteUint32s

1func (cl *Client) WriteUint32s(address uint16, values []uint32) error

func WriteUint64

1func (cl *Client) WriteUint64(address uint16, value uint64) error

func WriteUint64s

1func (cl *Client) WriteUint64s(address uint16, values []uint64) error

type Config

 1type Config struct {
 2	// URL specifies the Modbus endpoint (e.g. rtu://, tcp://, udp://, rtuovertcp://, rtuoverudp://).
 3	URL	string
 4	// Speed defines the serial baud rate (RTU only).
 5	Speed	uint
 6	// DataBits defines the number of data bits per character.
 7	DataBits	uint
 8	// Parity defines the serial parity mode.
 9	Parity	Parity
10	// StopBits defines the number of stop bits.
11	StopBits	uint
12	// Timeout specifies the request timeout.
13	Timeout	time.Duration
14	// ByteOrder defines byte ordering within registers.
15	ByteOrder	ByteOrder
16	// WordOrder defines register ordering for multi-register values.
17	WordOrder	WordOrder
18	// UnitId is the modbus unit id.
19	UnitId	uint8
20}

Config contains all parameters required to configure a Modbus client, including transport, serial settings, timeouts, and data encoding.

func String

1func (cfg Config) String() string


© Matthias Hochgatterer – MastodonGithubRésumé