Documentation
¶
Overview ¶
Package modbus provides a client for MODBUS TCP and RTU/ASCII.
Index ¶
- Constants
- Variables
- type ASCIIClientHandler
- func (mb *ASCIIClientHandler) Decode(adu []byte) (pdu *ProtocolDataUnit, err error)
- func (mb *ASCIIClientHandler) Encode(pdu *ProtocolDataUnit) (adu []byte, err error)
- func (mb *ASCIIClientHandler) Send(ctx context.Context, aduRequest []byte) (aduResponse []byte, err error)
- func (mb *ASCIIClientHandler) SetSlave(slaveID byte)
- func (mb *ASCIIClientHandler) Verify(aduRequest []byte, aduResponse []byte) (err error)
- type ASCIIOverTCPClientHandler
- func (mb *ASCIIOverTCPClientHandler) Decode(adu []byte) (pdu *ProtocolDataUnit, err error)
- func (mb *ASCIIOverTCPClientHandler) Encode(pdu *ProtocolDataUnit) (adu []byte, err error)
- func (mb *ASCIIOverTCPClientHandler) Send(ctx context.Context, aduRequest []byte) (aduResponse []byte, err error)
- func (mb *ASCIIOverTCPClientHandler) SetSlave(slaveID byte)
- func (mb *ASCIIOverTCPClientHandler) Verify(aduRequest []byte, aduResponse []byte) (err error)
- type Client
- func ASCIIClient(address string) Client
- func ASCIIOverTCPClient(address string) Client
- func NewClient(handler ClientHandler) Client
- func NewClient2(packager Packager, transporter Transporter) Client
- func RTUClient(address string) Client
- func RTUOverTCPClient(address string) Client
- func RTUOverUDPClient(address string) Client
- func TCPClient(address string) Client
- type ClientHandler
- type Connector
- type DataSizeError
- type DialFunc
- type ErrADURequestLength
- type ErrADUResponseLength
- type ErrTCPHeaderLength
- type Error
- type InvalidLengthError
- type Logger
- type Packager
- type ProtocolDataUnit
- type RTUClientHandler
- func (mb *RTUClientHandler) Decode(adu []byte) (pdu *ProtocolDataUnit, err error)
- func (mb *RTUClientHandler) Encode(pdu *ProtocolDataUnit) (adu []byte, err error)
- func (mb *RTUClientHandler) Send(ctx context.Context, aduRequest []byte) (aduResponse []byte, err error)
- func (mb *RTUClientHandler) SetSlave(slaveID byte)
- func (mb *RTUClientHandler) Verify(aduRequest []byte, aduResponse []byte) (err error)
- type RTUOverTCPClientHandler
- func (mb *RTUOverTCPClientHandler) Decode(adu []byte) (pdu *ProtocolDataUnit, err error)
- func (mb *RTUOverTCPClientHandler) Encode(pdu *ProtocolDataUnit) (adu []byte, err error)
- func (mb *RTUOverTCPClientHandler) Send(ctx context.Context, aduRequest []byte) (aduResponse []byte, err error)
- func (mb *RTUOverTCPClientHandler) SetSlave(slaveID byte)
- func (mb *RTUOverTCPClientHandler) Verify(aduRequest []byte, aduResponse []byte) (err error)
- type RTUOverUDPClientHandler
- func (mb *RTUOverUDPClientHandler) Close() error
- func (mb *RTUOverUDPClientHandler) Connect(ctx context.Context) error
- func (mb *RTUOverUDPClientHandler) Decode(adu []byte) (pdu *ProtocolDataUnit, err error)
- func (mb *RTUOverUDPClientHandler) Encode(pdu *ProtocolDataUnit) (adu []byte, err error)
- func (mb *RTUOverUDPClientHandler) Send(ctx context.Context, aduRequest []byte) (aduResponse []byte, err error)
- func (mb *RTUOverUDPClientHandler) SetSlave(slaveID byte)
- func (mb *RTUOverUDPClientHandler) Verify(aduRequest []byte, aduResponse []byte) (err error)
- type ReadDeviceIDCode
- type TCPClientHandler
- func (mb *TCPClientHandler) Close() error
- func (mb *TCPClientHandler) Connect(ctx context.Context) error
- func (mb *TCPClientHandler) Decode(adu []byte) (pdu *ProtocolDataUnit, err error)
- func (mb *TCPClientHandler) Encode(pdu *ProtocolDataUnit) (adu []byte, err error)
- func (mb *TCPClientHandler) Send(ctx context.Context, aduRequest []byte) (aduResponse []byte, err error)
- func (mb *TCPClientHandler) SetSlave(slaveID byte)
- func (mb *TCPClientHandler) Verify(aduRequest []byte, aduResponse []byte) error
- type TCPClientHandlerOption
- type Transporter
Constants ¶
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 // FuncCodeReadDeviceIdentification for byte wise access FuncCodeReadDeviceIdentification = 43 )
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 = 10 // ExceptionCodeGatewayTargetDeviceFailedToRespond error code ExceptionCodeGatewayTargetDeviceFailedToRespond = 11 )
Variables ¶
var ErrStreamDesynced = errors.New("modbus: response stream out of sync with requests")
ErrStreamDesynced reports that responses on the connection can no longer be paired with the requests that were sent. It is returned wrapped around the underlying cause, and covers two conditions that cannot be told apart from the outside:
- The byte stream is misaligned. A read failed part-way through a frame, so the rest of that frame is still queued and the next read would parse those leftover bytes as a header.
- A complete, well-formed frame arrived that cannot be attributed to the request that was sent, and is not a late response that can be accounted for. Here the bytes are still frame-aligned, but a request is left outstanding whose answer would corrupt the next exchange, and there is no way to tell how many such orphans are queued.
Neither is recoverable in place: the transporter closes the connection so that the next Send dials afresh. Callers that want the request retried must issue it again, which encodes a new transaction ID. The transporter deliberately does not retry by itself — re-writing the same ADU would put a duplicate frame with an identical transaction ID on the wire, and for a write function code that makes the device execute the command twice.
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 an ASCII frame:
Start : 1 char Address : 2 chars Function : 2 chars Data : 0 up to 2x252 chars LRC : 2 chars End : 2 chars
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 an ASCII frame:
Start : 1 char Address : 2 chars Function : 2 chars Data : 0 up to 2x252 chars LRC : 2 chars End : 2 chars
type Client ¶
type Client interface {
// ReadCoils reads from 1 to 2000 contiguous status of coils in a
// remote device and returns coil status.
ReadCoils(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, address, quantity uint16) (results []byte, err error)
// WriteSingleRegister writes a single holding register in a remote
// device and returns register value.
WriteSingleRegister(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, address uint16) (results []byte, err error)
// ReadDeviceIdentification reads the device identification objects using Modbus Function Code 0x2B (MEI type 0x0E).
// It supports Basic, Regular, and Extended identification requests, including handling multi-part responses via
// the "More Follows" field and iteratively fetching objects until all are received.
ReadDeviceIdentification(ctx context.Context, readDeviceIDCode ReadDeviceIDCode) (results map[byte][]byte, err error)
// ReadDeviceIdentificationSpecificObject reads a specific device identification object.
ReadDeviceIdentificationSpecificObject(ctx context.Context, objectID byte) (results map[byte][]byte, err error)
}
Client declares the functionality of a Modbus client regardless of the underlying transport stream.
func ASCIIClient ¶
ASCIIClient creates ASCII client with default handler and given connect string.
func ASCIIOverTCPClient ¶
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 RTUOverTCPClient ¶
RTUOverTCPClient creates RTU over TCP client with default handler and given connect string.
func RTUOverUDPClient ¶
RTUOverUDPClient creates RTU over UDP 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 ¶
Connector exposes the underlying handler capability for open/connect and close the transport channel.
type DataSizeError ¶
DataSizeError represents an error for invalid data-sizes i.e. for cases where the data-size does not match the expectation.
func (*DataSizeError) Error ¶
func (e *DataSizeError) Error() string
type DialFunc ¶
DialFunc is the prototype of a function that connects to an address on a named network. It Satisfies the net.Dialer.DialContext function signature.
type ErrADURequestLength ¶
type ErrADURequestLength int
ErrADURequestLength informs about a wrong ADU request length.
func (ErrADURequestLength) Error ¶
func (length ErrADURequestLength) Error() string
type ErrADUResponseLength ¶
type ErrADUResponseLength int
ErrADUResponseLength informs about a wrong ADU request length.
func (ErrADUResponseLength) Error ¶
func (length ErrADUResponseLength) Error() string
type ErrTCPHeaderLength ¶
type ErrTCPHeaderLength int
ErrTCPHeaderLength informs about a wrong header length.
func (ErrTCPHeaderLength) Error ¶
func (length ErrTCPHeaderLength) Error() string
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 ¶
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 an RTU frame:
Slave Address : 1 byte Function : 1 byte Data : 0 up to 252 bytes CRC : 2 byte
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 an 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(ctx context.Context, aduRequest []byte) (aduResponse []byte, err error)
Send sends data to server and ensures adequate response for request type
type RTUOverUDPClientHandler ¶
type RTUOverUDPClientHandler struct {
// contains filtered or unexported fields
}
RTUOverUDPClientHandler implements Packager and Transporter interface.
func NewRTUOverUDPClientHandler ¶
func NewRTUOverUDPClientHandler(address string) *RTUOverUDPClientHandler
NewRTUOverUDPClientHandler allocates and initializes a RTUOverUDPClientHandler.
func (*RTUOverUDPClientHandler) Close ¶
func (mb *RTUOverUDPClientHandler) Close() error
Close closes current connection.
func (*RTUOverUDPClientHandler) Connect ¶
Connect establishes a new connection to the address in Address.
func (*RTUOverUDPClientHandler) Decode ¶
func (mb *RTUOverUDPClientHandler) Decode(adu []byte) (pdu *ProtocolDataUnit, err error)
Decode extracts PDU from RTU frame and verify CRC.
func (*RTUOverUDPClientHandler) Encode ¶
func (mb *RTUOverUDPClientHandler) Encode(pdu *ProtocolDataUnit) (adu []byte, err error)
Encode encodes PDU in an RTU frame:
Slave Address : 1 byte Function : 1 byte Data : 0 up to 252 bytes CRC : 2 byte
func (*RTUOverUDPClientHandler) Send ¶
func (mb *RTUOverUDPClientHandler) Send(ctx context.Context, aduRequest []byte) (aduResponse []byte, err error)
Send sends data to server and ensures adequate response for request type
type ReadDeviceIDCode ¶
type ReadDeviceIDCode byte
ReadDeviceIDCode specifies a Read Device ID Code as defined in https://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b.pdf#page=45
const ( // ReadDeviceIDCodeBasic queries for VendorName, ProductCode, and MajorMinorRevision. ReadDeviceIDCodeBasic ReadDeviceIDCode = iota + 1 // ReadDeviceIDCodeRegular queries for VendorURL, ProductName, ModelName, and UserApplicationName. ReadDeviceIDCodeRegular // ReadDeviceIDCodeExtended queries for regular and private (custom) objects. ReadDeviceIDCodeExtended // ReadDeviceIDCodeSpecific queries for specific objects. ReadDeviceIDCodeSpecific )
type TCPClientHandler ¶
type TCPClientHandler struct {
// contains filtered or unexported fields
}
TCPClientHandler implements Packager and Transporter interface.
func NewTCPClientHandler ¶
func NewTCPClientHandler(address string, options ...TCPClientHandlerOption) *TCPClientHandler
NewTCPClientHandler allocates a new TCPClientHandler with the given options.
func (*TCPClientHandler) Close ¶
func (mb *TCPClientHandler) Close() error
Close closes current connection.
func (*TCPClientHandler) Connect ¶
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(ctx context.Context, aduRequest []byte) (aduResponse []byte, err error)
Send sends data to server and ensures response length is greater than header length.
type TCPClientHandlerOption ¶
type TCPClientHandlerOption func(*TCPClientHandler)
TCPClientHandlerOption configures a TCPClientHandler.
func WithDialer ¶
func WithDialer(d DialFunc) TCPClientHandlerOption
WithDialer returns a TCPClientHandlerOption that sets a custom Dial function.
func WithTLSConfig ¶
func WithTLSConfig(config *tls.Config) TCPClientHandlerOption
WithTLSConfig returns a TCPClientHandlerOption that enables TLS encryption with the given options.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
modbus-cli
command
|
|
|
commw32
command
Port of commw32.c To generate go types: go tool cgo commw32.go
|
Port of commw32.c To generate go types: go tool cgo commw32.go |