Documentation
¶
Index ¶
- Constants
- func LoadCertPool(filePath string) (cp *x509.CertPool, err error)
- func WithEndianess(endianness Endianness) func(*Client)
- func WithUnitID(unitID uint8) func(*Client)
- func WithWordOrder(wordOrder WordOrder) func(*Client)
- type Client
- func (mc *Client) Close() error
- func (mc *Client) Open() (err error)
- func (mc *Client) ReadBytes(addr uint16, quantity uint16, regType RegisterType, options ...func(*Client)) (values []byte, err error)
- func (mc *Client) ReadCoil(addr uint16, options ...func(*Client)) (value bool, err error)
- func (mc *Client) ReadCoils(addr uint16, quantity uint16, options ...func(*Client)) (values []bool, err error)
- func (mc *Client) ReadDiscreteInput(addr uint16, options ...func(*Client)) (value bool, err error)
- func (mc *Client) ReadDiscreteInputs(addr uint16, quantity uint16, options ...func(*Client)) (values []bool, err error)
- func (mc *Client) ReadFloat32(addr uint16, regType RegisterType, options ...func(*Client)) (value float32, err error)
- func (mc *Client) ReadFloat32s(addr uint16, quantity uint16, regType RegisterType, options ...func(*Client)) (values []float32, err error)
- func (mc *Client) ReadFloat64(addr uint16, regType RegisterType, options ...func(*Client)) (value float64, err error)
- func (mc *Client) ReadFloat64s(addr uint16, quantity uint16, regType RegisterType, options ...func(*Client)) (values []float64, err error)
- func (mc *Client) ReadRawBytes(addr uint16, quantity uint16, regType RegisterType, options ...func(*Client)) (values []byte, err error)
- func (mc *Client) ReadRegister(addr uint16, regType RegisterType, options ...func(*Client)) (value uint16, err error)
- func (mc *Client) ReadRegisters(addr uint16, quantity uint16, regType RegisterType, options ...func(*Client)) (values []uint16, err error)
- func (mc *Client) ReadUint32(addr uint16, regType RegisterType, options ...func(*Client)) (value uint32, err error)
- func (mc *Client) ReadUint32s(addr uint16, quantity uint16, regType RegisterType, options ...func(*Client)) (values []uint32, err error)
- func (mc *Client) ReadUint64(addr uint16, regType RegisterType, options ...func(*Client)) (value uint64, err error)
- func (mc *Client) ReadUint64s(addr uint16, quantity uint16, regType RegisterType, options ...func(*Client)) (values []uint64, err error)
- func (mc *Client) SetEncoding(endianness Endianness, wordOrder WordOrder) (err error)
- func (mc *Client) SetUnitID(id uint8) (err error)
- func (mc *Client) WriteBytes(addr uint16, values []byte, options ...func(*Client)) (err error)
- func (mc *Client) WriteCoil(addr uint16, value bool, options ...func(*Client)) (err error)
- func (mc *Client) WriteCoils(addr uint16, values []bool, options ...func(*Client)) (err error)
- func (mc *Client) WriteFloat32(addr uint16, value float32, options ...func(*Client)) (err error)
- func (mc *Client) WriteFloat32s(addr uint16, values []float32, options ...func(*Client)) (err error)
- func (mc *Client) WriteFloat64(addr uint16, value float64, options ...func(*Client)) (err error)
- func (mc *Client) WriteFloat64s(addr uint16, values []float64, options ...func(*Client)) (err error)
- func (mc *Client) WriteRawBytes(addr uint16, values []byte, options ...func(*Client)) (err error)
- func (mc *Client) WriteRegister(addr uint16, value uint16, options ...func(*Client)) (err error)
- func (mc *Client) WriteRegisters(addr uint16, values []uint16, options ...func(*Client)) (err error)
- func (mc *Client) WriteUint32(addr uint16, value uint32, options ...func(*Client)) (err error)
- func (mc *Client) WriteUint32s(addr uint16, values []uint32, options ...func(*Client)) (err error)
- func (mc *Client) WriteUint64(addr uint16, value uint64, options ...func(*Client)) (err error)
- func (mc *Client) WriteUint64s(addr uint16, values []uint64, options ...func(*Client)) (err error)
- type CoilsRequest
- type Configuration
- type DiscreteInputsRequest
- type Endianness
- type Error
- type HoldingRegistersRequest
- type InputRegistersRequest
- type RegisterType
- type RequestHandler
- type Server
- type ServerConfiguration
- type WordOrder
Constants ¶
const ( // HoldingRegister is a writable 16 bit register. HoldingRegister RegisterType = 0 // InputRegister is a read-only 16 bit register. InputRegister RegisterType = 1 // BigEndian means that the most significant bit is first, this is the default. BigEndian Endianness = 1 // LittleEndian means that the least significant bit is first. LittleEndian Endianness = 2 // HighWordFirst means that the most significant register is first, this is the default. HighWordFirst WordOrder = 1 // LowWordFirst means that the least significant register is first. LowWordFirst WordOrder = 2 )
Variables ¶
This section is empty.
Functions ¶
func LoadCertPool ¶
LoadCertPool loads a certificate store from a file into a CertPool object.
func WithEndianess ¶ added in v1.6.5
func WithEndianess(endianness Endianness) func(*Client)
WithEndianess is an option that can be passed to the Read and Write functions
func WithUnitID ¶ added in v1.6.5
WithUnitID is an option that can be passed to the Read and Write functions
func WithWordOrder ¶ added in v1.6.5
WithWordOrder is an option that can be passed to the Read and Write functions
Types ¶
type Client ¶ added in v1.6.5
type Client struct {
// contains filtered or unexported fields
}
Client is the actual client for a Modbus transport.
func NewClient ¶
func NewClient(conf *Configuration) (mc *Client, err error)
NewClient creates, configures and returns a modbus client object.
func (*Client) Open ¶ added in v1.6.5
Open opens the underlying transport (network socket or serial line).
func (*Client) ReadBytes ¶ added in v1.6.5
func (mc *Client) ReadBytes(addr uint16, quantity uint16, regType RegisterType, options ...func(*Client)) (values []byte, err error)
ReadBytes reads one or multiple 16-bit registers (function code 03 or 04) as bytes. A per-register byteswap is performed if endianness is set to LittleEndian.
func (*Client) ReadCoils ¶ added in v1.6.5
func (mc *Client) ReadCoils(addr uint16, quantity uint16, options ...func(*Client)) (values []bool, err error)
ReadCoils reads multiple coils (function code 01).
func (*Client) ReadDiscreteInput ¶ added in v1.6.5
ReadDiscreteInput reads a single discrete input (function code 02).
func (*Client) ReadDiscreteInputs ¶ added in v1.6.5
func (mc *Client) ReadDiscreteInputs(addr uint16, quantity uint16, options ...func(*Client)) (values []bool, err error)
ReadDiscreteInputs reads multiple discrete inputs (function code 02).
func (*Client) ReadFloat32 ¶ added in v1.6.5
func (mc *Client) ReadFloat32(addr uint16, regType RegisterType, options ...func(*Client)) (value float32, err error)
ReadFloat32 reads a single 32-bit float register.
func (*Client) ReadFloat32s ¶ added in v1.6.5
func (mc *Client) ReadFloat32s(addr uint16, quantity uint16, regType RegisterType, options ...func(*Client)) (values []float32, err error)
ReadFloat32s reads multiple 32-bit float registers.
func (*Client) ReadFloat64 ¶ added in v1.6.5
func (mc *Client) ReadFloat64(addr uint16, regType RegisterType, options ...func(*Client)) (value float64, err error)
ReadFloat64 reads a single 64-bit float register.
func (*Client) ReadFloat64s ¶ added in v1.6.5
func (mc *Client) ReadFloat64s(addr uint16, quantity uint16, regType RegisterType, options ...func(*Client)) (values []float64, err error)
ReadFloat64s reads multiple 64-bit float registers.
func (*Client) ReadRawBytes ¶ added in v1.6.5
func (mc *Client) ReadRawBytes(addr uint16, quantity uint16, regType RegisterType, options ...func(*Client)) (values []byte, err error)
ReadRawBytes reads one or multiple 16-bit registers (function code 03 or 04) as bytes. No byte or word reordering is performed: bytes are returned exactly as they come off the wire, allowing the caller to handle encoding/endianness/word order manually.
func (*Client) ReadRegister ¶ added in v1.6.5
func (mc *Client) ReadRegister(addr uint16, regType RegisterType, options ...func(*Client)) (value uint16, err error)
ReadRegister reads a single 16-bit register (function code 03 or 04).
func (*Client) ReadRegisters ¶ added in v1.6.5
func (mc *Client) ReadRegisters(addr uint16, quantity uint16, regType RegisterType, options ...func(*Client)) (values []uint16, err error)
ReadRegisters reads multiple 16-bit registers (function code 03 or 04).
func (*Client) ReadUint32 ¶ added in v1.6.5
func (mc *Client) ReadUint32(addr uint16, regType RegisterType, options ...func(*Client)) (value uint32, err error)
ReadUint32 reads a single 32-bit register.
func (*Client) ReadUint32s ¶ added in v1.6.5
func (mc *Client) ReadUint32s(addr uint16, quantity uint16, regType RegisterType, options ...func(*Client)) (values []uint32, err error)
ReadUint32s reads multiple 32-bit registers.
func (*Client) ReadUint64 ¶ added in v1.6.5
func (mc *Client) ReadUint64(addr uint16, regType RegisterType, options ...func(*Client)) (value uint64, err error)
ReadUint64 reads a single 64-bit register.
func (*Client) ReadUint64s ¶ added in v1.6.5
func (mc *Client) ReadUint64s(addr uint16, quantity uint16, regType RegisterType, options ...func(*Client)) (values []uint64, err error)
ReadUint64s reads multiple 64-bit registers.
func (*Client) SetEncoding ¶ added in v1.6.5
func (mc *Client) SetEncoding(endianness Endianness, wordOrder WordOrder) (err error)
SetEncoding sets the encoding (endianness and word ordering) of subsequent requests.
func (*Client) WriteBytes ¶ added in v1.6.5
WriteBytes writes the given slice of bytes to 16-bit registers starting at addr. A per-register byteswap is performed if endianness is set to LittleEndian. Odd byte quantities are padded with a null byte to fall on 16-bit register boundaries.
func (*Client) WriteCoils ¶ added in v1.6.5
WriteCoils writes multiple coils (function code 15).
func (*Client) WriteFloat32 ¶ added in v1.6.5
WriteFloat32 writes a single 32-bit float register.
func (*Client) WriteFloat32s ¶ added in v1.6.5
func (mc *Client) WriteFloat32s(addr uint16, values []float32, options ...func(*Client)) (err error)
WriteFloat32s writes multiple 32-bit float registers.
func (*Client) WriteFloat64 ¶ added in v1.6.5
WriteFloat64 writes a single 64-bit float register.
func (*Client) WriteFloat64s ¶ added in v1.6.5
func (mc *Client) WriteFloat64s(addr uint16, values []float64, options ...func(*Client)) (err error)
WriteFloat64s writes multiple 64-bit float registers.
func (*Client) WriteRawBytes ¶ added in v1.6.5
WriteRawBytes writes the given slice of bytes to 16-bit registers starting at addr. No byte or word reordering is performed: bytes are pushed to the wire as-is, allowing the caller to handle encoding/endianness/word order manually. Odd byte quantities are padded with a null byte to fall on 16-bit register boundaries.
func (*Client) WriteRegister ¶ added in v1.6.5
WriteRegister writes a single 16-bit register (function code 06).
func (*Client) WriteRegisters ¶ added in v1.6.5
func (mc *Client) WriteRegisters(addr uint16, values []uint16, options ...func(*Client)) (err error)
WriteRegisters writes multiple 16-bit registers (function code 16).
func (*Client) WriteUint32 ¶ added in v1.6.5
WriteUint32 writes a single 32-bit register.
func (*Client) WriteUint32s ¶ added in v1.6.5
WriteUint32s writes multiple 32-bit registers.
func (*Client) WriteUint64 ¶ added in v1.6.5
WriteUint64 writes a single 64-bit register.
type CoilsRequest ¶
type CoilsRequest struct {
ClientAddr string // the source (client) IP address
ClientRole string // the client role as encoded in the client certificate (tcp+tls only)
UnitID uint8 // the requested unit id (slave id)
Addr uint16 // the base coil address requested
Quantity uint16 // the number of consecutive coils covered by this request
// (first address: Addr, last address: Addr + Quantity - 1)
IsWrite bool // true if the request is a write, false if a read
Args []bool // a slice of bool values of the coils to be set, ordered
}
CoilsRequest is an object passed to the coil handler.
type Configuration ¶ added in v1.6.5
type Configuration struct {
// URL sets the client mode and target location in the form
// <mode>://<serial device or host:port> e.g. tcp://plc:502.
URL string
// Speed sets the serial link speed (in bps, rtu only).
Speed int
// DataBits sets the number of bits per serial character (rtu only).
DataBits int
// Parity sets the serial link parity mode (rtu only).
Parity serial.Parity
// StopBits sets the number of serial stop bits (rtu only).
StopBits serial.StopBits
// Timeout sets the request timeout value.
Timeout time.Duration
// TLSClientCert sets the client-side TLS key pair (tcp+tls only).
TLSClientCert *tls.Certificate
// TLSRootCAs sets the list of CA certificates used to authenticate
// the server (tcp+tls only). Leaf (i.e. server) certificates can also
// be used in case of self-signed certs, or if cert pinning is required.
TLSRootCAs *x509.CertPool
// Logger provides a custom sink for log messages.
// If nil, messages will be written to stdout.
Logger *log.Logger
}
Configuration stores the configuration needed to create a Modbus client.
type DiscreteInputsRequest ¶
type DiscreteInputsRequest struct {
ClientAddr string // the source (client) IP address
ClientRole string // the client role as encoded in the client certificate (tcp+tls only)
UnitID uint8 // the requested unit id (slave id)
Addr uint16 // the base discrete input address requested
Quantity uint16 // the number of consecutive discrete inputs covered by this request
}
DiscreteInputsRequest is an object passed to the discrete input handler.
type Error ¶
type Error string
Error for modbus errors
const ( ErrConfigurationError Error = "configuration error" ErrRequestTimedOut Error = "request timed out" ErrIllegalFunction Error = "illegal function" ErrIllegalDataAddress Error = "illegal data address" ErrIllegalDataValue Error = "illegal data value" ErrServerDeviceFailure Error = "server device failure" ErrAcknowledge Error = "request acknowledged" ErrServerDeviceBusy Error = "server device busy" ErrMemoryParityError Error = "memory parity error" ErrGWTargetFailedToRespond Error = "gateway target device failed to respond" ErrBadCRC Error = "bad crc" ErrShortFrame Error = "short frame" ErrProtocolError Error = "protocol error" ErrBadUnitID Error = "bad unit id" ErrBadTransactionID Error = "bad transaction id" ErrUnknownProtocolID Error = "unknown protocol identifier" ErrUnexpectedParameters Error = "unexpected parameters" ErrTransportIsAlreadyOpen Error = "transport is already open" ErrTransportIsAlreadyClosed Error = "transport is already closed" )
All Modbus errors.
type HoldingRegistersRequest ¶
type HoldingRegistersRequest struct {
ClientAddr string // the source (client) IP address
ClientRole string // the client role as encoded in the client certificate (tcp+tls only)
UnitID uint8 // the requested unit id (slave id)
Addr uint16 // the base register address requested
Quantity uint16 // the number of consecutive registers covered by this request
IsWrite bool // true if the request is a write, false if a read
Args []uint16 // a slice of register values to be set, ordered from
}
HoldingRegistersRequest is an object passed to the holding register handler.
type InputRegistersRequest ¶
type InputRegistersRequest struct {
ClientAddr string // the source (client) IP address
ClientRole string // the client role as encoded in the client certificate (tcp+tls only)
UnitID uint8 // the requested unit id (slave id)
Addr uint16 // the base register address requested
Quantity uint16 // the number of consecutive registers covered by this request
}
InputRegistersRequest is an object passed to the input register handler.
type RegisterType ¶ added in v1.6.5
type RegisterType uint
RegisterType is either HoldingRegister or InputRegister.
type RequestHandler ¶
type RequestHandler interface {
// HandleCoils handles the read coils (0x01), write single coil (0x05)
// and write multiple coils (0x0f) function codes.
// A CoilsRequest object is passed to the handler (see above).
//
// Expected return values:
// - res: a slice of bools containing the coil values to be sent to back
// to the client (only sent for reads),
// - err: either nil if no error occurred, a modbus error (see
// mapErrorToExceptionCode() in modbus.go for a complete list),
// or any other error.
// If nil, a positive modbus response is sent back to the client
// along with the returned data.
// If non-nil, a negative modbus response is sent back, with the
// exception code set depending on the error
// (again, see mapErrorToExceptionCode()).
HandleCoils(req *CoilsRequest) (res []bool, err error)
// HandleDiscreteInputs handles the read discrete inputs (0x02) function code.
// A DiscreteInputsRequest oibject is passed to the handler (see above).
//
// Expected return values:
// - res: a slice of bools containing the discrete input values to be
// sent back to the client,
// - err: either nil if no error occurred, a modbus error (see
// mapErrorToExceptionCode() in modbus.go for a complete list),
// or any other error.
HandleDiscreteInputs(req *DiscreteInputsRequest) (res []bool, err error)
// HandleHoldingRegisters handles the read holding registers (0x03),
// write single register (0x06) and write multiple registers (0x10).
// A HoldingRegistersRequest object is passed to the handler (see above).
//
// Expected return values:
// - res: a slice of uint16 containing the register values to be sent
// to back to the client (only sent for reads),
// - err: either nil if no error occurred, a modbus error (see
// mapErrorToExceptionCode() in modbus.go for a complete list),
// or any other error.
HandleHoldingRegisters(req *HoldingRegistersRequest) (res []uint16, err error)
// HandleInputRegisters handles the read input registers (0x04) function code.
// An InputRegistersRequest object is passed to the handler (see above).
//
// Expected return values:
// - res: a slice of uint16 containing the register values to be sent
// back to the client,
// - err: either nil if no error occurred, a modbus error (see
// mapErrorToExceptionCode() in modbus.go for a complete list),
// or any other error.
HandleInputRegisters(req *InputRegistersRequest) (res []uint16, err error)
}
The RequestHandler interface should be implemented by the handler object passed to NewServer (see reqHandler in NewServer()). After decoding and validating an incoming request, the server will invoke the appropriate handler function, depending on the function code of the request.
type Server ¶ added in v1.6.5
type Server struct {
// contains filtered or unexported fields
}
Server object.
func NewServer ¶
func NewServer(conf *ServerConfiguration, reqHandler RequestHandler) ( ms *Server, err error)
NewServer creates a new modbus server.
type ServerConfiguration ¶
type ServerConfiguration struct {
// URL defines where to listen at e.g. tcp://[::]:502
URL string
// Timeout sets the idle session timeout (client connections will
// be closed if idle for this long)
Timeout time.Duration
// MaxClients sets the maximum number of concurrent client connections
MaxClients uint
// TLSServerCert sets the server-side TLS key pair (tcp+tls only)
TLSServerCert *tls.Certificate
// TLSClientCAs sets the list of CA certificates used to authenticate
// client connections (tcp+tls only). Leaf (i.e. client) certificates can
// also be used in case of self-signed certs, or if cert pinning is required.
TLSClientCAs *x509.CertPool
// Logger provides a custom sink for log messages.
// If nil, messages will be written to stdout.
Logger *log.Logger
}
ServerConfiguration object