modbus

package module
v0.0.0-...-3afca28 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2021 License: BSD-3-Clause Imports: 10 Imported by: 1

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)

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)

References

Documentation

Overview

Package modbus provides a client for MODBUS TCP and RTU/ASCII.

Index

Constants

View Source
const (
	// FuncCodeReadDiscreteInputs for bit wise access
	FuncCodeReadDiscreteInputs = 2
	// FuncCodeReadCoils for bit wise access
	FuncCodeReadCoils = 1
	// FuncCodeWriteSingleCoil for bit wise access
	FuncCodeWriteSingleCoil = 5
	// FuncCodeWriteMultipleCoils for bit wise access
	FuncCodeWriteMultipleCoils = 15

	// FuncCodeReadInputRegisters 16-bit wise access
	FuncCodeReadInputRegisters = 4
	// FuncCodeReadHoldingRegisters 16-bit wise access
	FuncCodeReadHoldingRegisters = 3
	// FuncCodeWriteSingleRegister 16-bit wise access
	FuncCodeWriteSingleRegister = 6
	// FuncCodeWriteMultipleRegisters 16-bit wise access
	FuncCodeWriteMultipleRegisters = 16
	// FuncCodeReadWriteMultipleRegisters 16-bit wise access
	FuncCodeReadWriteMultipleRegisters = 23
	// FuncCodeMaskWriteRegister 16-bit wise access
	FuncCodeMaskWriteRegister = 22
	// FuncCodeReadFIFOQueue 16-bit wise access
	FuncCodeReadFIFOQueue = 24
)
View Source
const (
	// ExceptionCodeIllegalFunction error code
	ExceptionCodeIllegalFunction = 1
	// ExceptionCodeIllegalDataAddress error code
	ExceptionCodeIllegalDataAddress = 2
	// ExceptionCodeIllegalDataValue error code
	ExceptionCodeIllegalDataValue = 3
	// ExceptionCodeServerDeviceFailure error code
	ExceptionCodeServerDeviceFailure = 4
	// ExceptionCodeAcknowledge error code
	ExceptionCodeAcknowledge = 5
	// ExceptionCodeServerDeviceBusy error code
	ExceptionCodeServerDeviceBusy = 6
	// ExceptionCodeMemoryParityError error code
	ExceptionCodeMemoryParityError = 8
	// ExceptionCodeGatewayPathUnavailable error code
	ExceptionCodeGatewayPathUnavailable = 10
	// ExceptionCodeGatewayTargetDeviceFailedToRespond error code
	ExceptionCodeGatewayTargetDeviceFailedToRespond = 11
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ASCIIClientHandler

type ASCIIClientHandler struct {
	// contains filtered or unexported fields
}

ASCIIClientHandler implements Packager and Transporter interface.

func NewASCIIClientHandler

func NewASCIIClientHandler(address string) *ASCIIClientHandler

NewASCIIClientHandler allocates and initializes a ASCIIClientHandler.

func (*ASCIIClientHandler) Decode

func (mb *ASCIIClientHandler) Decode(adu []byte) (pdu *ProtocolDataUnit, err error)

Decode extracts PDU from ASCII frame and verify LRC.

func (*ASCIIClientHandler) Encode

func (mb *ASCIIClientHandler) Encode(pdu *ProtocolDataUnit) (adu []byte, err error)

Encode encodes PDU in a ASCII frame:

Start           : 1 char
Address         : 2 chars
Function        : 2 chars
Data            : 0 up to 2x252 chars
LRC             : 2 chars
End             : 2 chars

func (*ASCIIClientHandler) Send

func (mb *ASCIIClientHandler) Send(aduRequest []byte) (aduResponse []byte, err error)

func (*ASCIIClientHandler) SetSlave

func (mb *ASCIIClientHandler) SetSlave(slaveID byte)

SetSlave sets modbus slave id for the next client operations

func (*ASCIIClientHandler) Verify

func (mb *ASCIIClientHandler) Verify(aduRequest []byte, aduResponse []byte) (err error)

Verify verifies response length, frame boundary and slave id.

type ASCIIOverTCPClientHandler

type ASCIIOverTCPClientHandler struct {
	// contains filtered or unexported fields
}

ASCIIOverTCPClientHandler implements Packager and Transporter interface.

func NewASCIIOverTCPClientHandler

func NewASCIIOverTCPClientHandler(address string) *ASCIIOverTCPClientHandler

NewASCIIOverTCPClientHandler allocates and initializes a ASCIIOverTCPClientHandler.

func (*ASCIIOverTCPClientHandler) Decode

func (mb *ASCIIOverTCPClientHandler) Decode(adu []byte) (pdu *ProtocolDataUnit, err error)

Decode extracts PDU from ASCII frame and verify LRC.

func (*ASCIIOverTCPClientHandler) Encode

func (mb *ASCIIOverTCPClientHandler) Encode(pdu *ProtocolDataUnit) (adu []byte, err error)

Encode encodes PDU in a ASCII frame:

Start           : 1 char
Address         : 2 chars
Function        : 2 chars
Data            : 0 up to 2x252 chars
LRC             : 2 chars
End             : 2 chars

func (*ASCIIOverTCPClientHandler) Send

func (mb *ASCIIOverTCPClientHandler) Send(aduRequest []byte) (aduResponse []byte, err error)

func (*ASCIIOverTCPClientHandler) SetSlave

func (mb *ASCIIOverTCPClientHandler) SetSlave(slaveID byte)

SetSlave sets modbus slave id for the next client operations

func (*ASCIIOverTCPClientHandler) Verify

func (mb *ASCIIOverTCPClientHandler) Verify(aduRequest []byte, aduResponse []byte) (err error)

Verify verifies response length, frame boundary and slave id.

type Client

type Client interface {

	// ReadCoils reads from 1 to 2000 contiguous status of coils in a
	// remote device and returns coil status.
	ReadCoils(address, quantity uint16) (results []byte, err error)
	// ReadDiscreteInputs reads from 1 to 2000 contiguous status of
	// discrete inputs in a remote device and returns input status.
	ReadDiscreteInputs(address, quantity uint16) (results []byte, err error)
	// WriteSingleCoil write a single output to either ON or OFF in a
	// remote device and returns output value.
	WriteSingleCoil(address, value uint16) (results []byte, err error)
	// WriteMultipleCoils forces each coil in a sequence of coils to either
	// ON or OFF in a remote device and returns quantity of outputs.
	WriteMultipleCoils(address, quantity uint16, value []byte) (results []byte, err error)

	// ReadInputRegisters reads from 1 to 125 contiguous input registers in
	// a remote device and returns input registers.
	ReadInputRegisters(address, quantity uint16) (results []byte, err error)
	// ReadHoldingRegisters reads the contents of a contiguous block of
	// holding registers in a remote device and returns register value.
	ReadHoldingRegisters(address, quantity uint16) (results []byte, err error)
	// WriteSingleRegister writes a single holding register in a remote
	// device and returns register value.
	WriteSingleRegister(address, value uint16) (results []byte, err error)
	// WriteMultipleRegisters writes a block of contiguous registers
	// (1 to 123 registers) in a remote device and returns quantity of
	// registers.
	WriteMultipleRegisters(address, quantity uint16, value []byte) (results []byte, err error)
	// ReadWriteMultipleRegisters performs a combination of one read
	// operation and one write operation. It returns read registers value.
	ReadWriteMultipleRegisters(readAddress, readQuantity, writeAddress, writeQuantity uint16, value []byte) (results []byte, err error)
	// MaskWriteRegister modify the contents of a specified holding
	// register using a combination of an AND mask, an OR mask, and the
	// register's current contents. The function returns
	// AND-mask and OR-mask.
	MaskWriteRegister(address, andMask, orMask uint16) (results []byte, err error)
	//ReadFIFOQueue reads the contents of a First-In-First-Out (FIFO) queue
	// of register in a remote device and returns FIFO value register.
	ReadFIFOQueue(address uint16) (results []byte, err error)
}

Client declares the functionality of a Modbus client regardless of the underlying transport stream.

func ASCIIClient

func ASCIIClient(address string) Client

ASCIIClient creates ASCII client with default handler and given connect string.

func ASCIIOverTCPClient

func ASCIIOverTCPClient(address string) Client

ASCIIOverTCPClient creates ASCII over TCP client with default handler and given connect string.

func NewClient

func NewClient(handler ClientHandler) Client

NewClient creates a new modbus client with given backend handler.

func NewClient2

func NewClient2(packager Packager, transporter Transporter) Client

NewClient2 creates a new modbus client with given backend packager and transporter.

func RTUClient

func RTUClient(address string) Client

RTUClient creates RTU client with default handler and given connect string.

func RTUOverTCPClient

func RTUOverTCPClient(address string) Client

RTUOverTCPClient creates RTU over TCP client with default handler and given connect string.

func TCPClient

func TCPClient(address string) Client

TCPClient creates TCP client with default handler and given connect string.

type ClientHandler

type ClientHandler interface {
	Packager
	Transporter
	Connector
}

ClientHandler is the interface that groups the Packager and Transporter methods.

type Connector

type Connector interface {
	Connect() error
	Close() error
}

Connector exposes the underlying handler capability for open/connect and close the transport channel.

type ErrTCPHeaderLength

type ErrTCPHeaderLength int

ErrTCPHeaderLength informs about a wrong header length.

func (ErrTCPHeaderLength) Error

func (length ErrTCPHeaderLength) Error() string

type Error

type Error struct {
	FunctionCode  byte
	ExceptionCode byte
}

Error implements error interface.

func (*Error) Error

func (e *Error) Error() string

Error converts known modbus exception code to error message.

type InvalidLengthError

type InvalidLengthError struct {
	// contains filtered or unexported fields
}

InvalidLengthError is returned by readIncrementally when the modbus response would overflow buffer implemented to simplify testing

func (*InvalidLengthError) Error

func (e *InvalidLengthError) Error() string

Error implements the error interface

type Packager

type Packager interface {
	SetSlave(slaveID byte)
	Encode(pdu *ProtocolDataUnit) (adu []byte, err error)
	Decode(adu []byte) (pdu *ProtocolDataUnit, err error)
	Verify(aduRequest []byte, aduResponse []byte) (err error)
}

Packager specifies the communication layer.

type ProtocolDataUnit

type ProtocolDataUnit struct {
	FunctionCode byte
	Data         []byte
}

ProtocolDataUnit (PDU) is independent of underlying communication layers.

type RTUClientHandler

type RTUClientHandler struct {
	// contains filtered or unexported fields
}

RTUClientHandler implements Packager and Transporter interface.

func NewRTUClientHandler

func NewRTUClientHandler(address string) *RTUClientHandler

NewRTUClientHandler allocates and initializes a RTUClientHandler.

func (*RTUClientHandler) Decode

func (mb *RTUClientHandler) Decode(adu []byte) (pdu *ProtocolDataUnit, err error)

Decode extracts PDU from RTU frame and verify CRC.

func (*RTUClientHandler) Encode

func (mb *RTUClientHandler) Encode(pdu *ProtocolDataUnit) (adu []byte, err error)

Encode encodes PDU in a RTU frame:

Slave Address   : 1 byte
Function        : 1 byte
Data            : 0 up to 252 bytes
CRC             : 2 byte

func (*RTUClientHandler) Send

func (mb *RTUClientHandler) Send(aduRequest []byte) (aduResponse []byte, err error)

func (*RTUClientHandler) SetSlave

func (mb *RTUClientHandler) SetSlave(slaveID byte)

SetSlave sets modbus slave id for the next client operations

func (*RTUClientHandler) Verify

func (mb *RTUClientHandler) Verify(aduRequest []byte, aduResponse []byte) (err error)

Verify verifies response length and slave id.

type RTUOverTCPClientHandler

type RTUOverTCPClientHandler struct {
	// contains filtered or unexported fields
}

RTUOverTCPClientHandler implements Packager and Transporter interface.

func NewRTUOverTCPClientHandler

func NewRTUOverTCPClientHandler(address string) *RTUOverTCPClientHandler

NewRTUOverTCPClientHandler allocates and initializes a RTUOverTCPClientHandler.

func (*RTUOverTCPClientHandler) Decode

func (mb *RTUOverTCPClientHandler) Decode(adu []byte) (pdu *ProtocolDataUnit, err error)

Decode extracts PDU from RTU frame and verify CRC.

func (*RTUOverTCPClientHandler) Encode

func (mb *RTUOverTCPClientHandler) Encode(pdu *ProtocolDataUnit) (adu []byte, err error)

Encode encodes PDU in a RTU frame:

Slave Address   : 1 byte
Function        : 1 byte
Data            : 0 up to 252 bytes
CRC             : 2 byte

func (*RTUOverTCPClientHandler) Send

func (mb *RTUOverTCPClientHandler) Send(aduRequest []byte) (aduResponse []byte, err error)

Send sends data to server and ensures adequate response for request type

func (*RTUOverTCPClientHandler) SetSlave

func (mb *RTUOverTCPClientHandler) SetSlave(slaveID byte)

SetSlave sets modbus slave id for the next client operations

func (*RTUOverTCPClientHandler) Verify

func (mb *RTUOverTCPClientHandler) Verify(aduRequest []byte, aduResponse []byte) (err error)

Verify verifies response length and slave id.

type TCPClientHandler

type TCPClientHandler struct {
	// contains filtered or unexported fields
}

TCPClientHandler implements Packager and Transporter interface.

func NewTCPClientHandler

func NewTCPClientHandler(address string) *TCPClientHandler

NewTCPClientHandler allocates a new TCPClientHandler.

func (*TCPClientHandler) Close

func (mb *TCPClientHandler) Close() error

Close closes current connection.

func (*TCPClientHandler) Connect

func (mb *TCPClientHandler) Connect() error

Connect establishes a new connection to the address in Address. Connect and Close are exported so that multiple requests can be done with one session

func (*TCPClientHandler) Decode

func (mb *TCPClientHandler) Decode(adu []byte) (pdu *ProtocolDataUnit, err error)

Decode extracts PDU from TCP frame:

Transaction identifier: 2 bytes
Protocol identifier: 2 bytes
Length: 2 bytes
Unit identifier: 1 byte

func (*TCPClientHandler) Encode

func (mb *TCPClientHandler) Encode(pdu *ProtocolDataUnit) (adu []byte, err error)

Encode adds modbus application protocol header:

Transaction identifier: 2 bytes
Protocol identifier: 2 bytes
Length: 2 bytes
Unit identifier: 1 byte
Function code: 1 byte
Data: n bytes

func (*TCPClientHandler) Send

func (mb *TCPClientHandler) Send(aduRequest []byte) (aduResponse []byte, err error)

Send sends data to server and ensures response length is greater than header length.

func (*TCPClientHandler) SetSlave

func (mb *TCPClientHandler) SetSlave(slaveID byte)

SetSlave sets modbus slave id for the next client operations

func (*TCPClientHandler) Verify

func (mb *TCPClientHandler) Verify(aduRequest []byte, aduResponse []byte) error

Verify confirms transaction, protocol and unit id.

type Transporter

type Transporter interface {
	Send(aduRequest []byte) (aduResponse []byte, err error)
}

Transporter specifies the transport layer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL