Documentation ¶
Index ¶
- Constants
- func LoadCertPool(filePath string) (cp *x509.CertPool, err error)
- type ClientConfiguration
- type CoilsRequest
- type DiscreteInputsRequest
- type Endianness
- type Error
- type HoldingRegistersRequest
- type InputRegistersRequest
- type ModbusClient
- func (mc *ModbusClient) Close() (err error)
- func (mc *ModbusClient) Open() (err error)
- func (mc *ModbusClient) ReadCoil(addr uint16) (value bool, err error)
- func (mc *ModbusClient) ReadCoils(addr uint16, quantity uint16) (values []bool, err error)
- func (mc *ModbusClient) ReadDiscreteInput(addr uint16) (value bool, err error)
- func (mc *ModbusClient) ReadDiscreteInputs(addr uint16, quantity uint16) (values []bool, err error)
- func (mc *ModbusClient) ReadFloat32(addr uint16, regType RegType) (value float32, err error)
- func (mc *ModbusClient) ReadFloat32s(addr uint16, quantity uint16, regType RegType) (values []float32, err error)
- func (mc *ModbusClient) ReadFloat64(addr uint16, regType RegType) (value float64, err error)
- func (mc *ModbusClient) ReadFloat64s(addr uint16, quantity uint16, regType RegType) (values []float64, err error)
- func (mc *ModbusClient) ReadRegister(addr uint16, regType RegType) (value uint16, err error)
- func (mc *ModbusClient) ReadRegisters(addr uint16, quantity uint16, regType RegType) (values []uint16, err error)
- func (mc *ModbusClient) ReadUint32(addr uint16, regType RegType) (value uint32, err error)
- func (mc *ModbusClient) ReadUint32s(addr uint16, quantity uint16, regType RegType) (values []uint32, err error)
- func (mc *ModbusClient) ReadUint64(addr uint16, regType RegType) (value uint64, err error)
- func (mc *ModbusClient) ReadUint64s(addr uint16, quantity uint16, regType RegType) (values []uint64, err error)
- func (mc *ModbusClient) SetEncoding(endianness Endianness, wordOrder WordOrder) (err error)
- func (mc *ModbusClient) SetUnitId(id uint8) (err error)
- func (mc *ModbusClient) WriteCoil(addr uint16, value bool) (err error)
- func (mc *ModbusClient) WriteCoils(addr uint16, values []bool) (err error)
- func (mc *ModbusClient) WriteFloat32(addr uint16, value float32) (err error)
- func (mc *ModbusClient) WriteFloat32s(addr uint16, values []float32) (err error)
- func (mc *ModbusClient) WriteFloat64(addr uint16, value float64) (err error)
- func (mc *ModbusClient) WriteFloat64s(addr uint16, values []float64) (err error)
- func (mc *ModbusClient) WriteRegister(addr uint16, value uint16) (err error)
- func (mc *ModbusClient) WriteRegisters(addr uint16, values []uint16) (err error)
- func (mc *ModbusClient) WriteUint32(addr uint16, value uint32) (err error)
- func (mc *ModbusClient) WriteUint32s(addr uint16, values []uint32) (err error)
- func (mc *ModbusClient) WriteUint64(addr uint16, value uint64) (err error)
- func (mc *ModbusClient) WriteUint64s(addr uint16, values []uint64) (err error)
- type ModbusServer
- type RegType
- type RequestHandler
- type ServerConfiguration
- type WordOrder
Constants ¶
const ( PARITY_NONE uint = 0 PARITY_EVEN uint = 1 PARITY_ODD uint = 2 HOLDING_REGISTER RegType = 0 INPUT_REGISTER RegType = 1 // endianness of 16-bit registers BIG_ENDIAN Endianness = 1 LITTLE_ENDIAN Endianness = 2 // word order of 32-bit registers HIGH_WORD_FIRST WordOrder = 1 LOW_WORD_FIRST WordOrder = 2 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ClientConfiguration ¶ added in v1.3.1
type ClientConfiguration 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 uint // DataBits sets the number of bits per serial character (rtu only) DataBits uint // Parity sets the serial link parity mode (rtu only) Parity uint // StopBits sets the number of serial stop bits (rtu only) StopBits uint // 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 }
Modbus client configuration object.
type CoilsRequest ¶ added in v1.3.1
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 }
Request object passed to the coil handler.
type DiscreteInputsRequest ¶ added in v1.3.1
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 }
Request object passed to the discrete input handler.
type Endianness ¶ added in v1.3.1
type Endianness uint
type Error ¶ added in v1.3.1
type Error string
const ( // errors 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" )
type HoldingRegistersRequest ¶ added in v1.3.1
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 }
Request object passed to the holding register handler.
type InputRegistersRequest ¶ added in v1.3.1
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 }
Request object passed to the input register handler.
type ModbusClient ¶ added in v1.3.1
type ModbusClient struct {
// contains filtered or unexported fields
}
Modbus client object.
func NewClient ¶
func NewClient(conf *ClientConfiguration) (mc *ModbusClient, err error)
NewClient creates, configures and returns a modbus client object.
func (*ModbusClient) Close ¶ added in v1.3.1
func (mc *ModbusClient) Close() (err error)
Closes the underlying transport.
func (*ModbusClient) Open ¶ added in v1.3.1
func (mc *ModbusClient) Open() (err error)
Opens the underlying transport (tcp socket or serial line).
func (*ModbusClient) ReadCoil ¶ added in v1.3.1
func (mc *ModbusClient) ReadCoil(addr uint16) (value bool, err error)
Reads a single coil (function code 01).
func (*ModbusClient) ReadCoils ¶ added in v1.3.1
func (mc *ModbusClient) ReadCoils(addr uint16, quantity uint16) (values []bool, err error)
Reads multiple coils (function code 01).
func (*ModbusClient) ReadDiscreteInput ¶ added in v1.3.1
func (mc *ModbusClient) ReadDiscreteInput(addr uint16) (value bool, err error)
Reads a single discrete input (function code 02).
func (*ModbusClient) ReadDiscreteInputs ¶ added in v1.3.1
func (mc *ModbusClient) ReadDiscreteInputs(addr uint16, quantity uint16) (values []bool, err error)
Reads multiple discrete inputs (function code 02).
func (*ModbusClient) ReadFloat32 ¶ added in v1.3.1
func (mc *ModbusClient) ReadFloat32(addr uint16, regType RegType) (value float32, err error)
Reads a single 32-bit float register.
func (*ModbusClient) ReadFloat32s ¶ added in v1.3.1
func (mc *ModbusClient) ReadFloat32s(addr uint16, quantity uint16, regType RegType) (values []float32, err error)
Reads multiple 32-bit float registers.
func (*ModbusClient) ReadFloat64 ¶ added in v1.3.1
func (mc *ModbusClient) ReadFloat64(addr uint16, regType RegType) (value float64, err error)
Reads a single 64-bit float register.
func (*ModbusClient) ReadFloat64s ¶ added in v1.3.1
func (mc *ModbusClient) ReadFloat64s(addr uint16, quantity uint16, regType RegType) (values []float64, err error)
Reads multiple 64-bit float registers.
func (*ModbusClient) ReadRegister ¶ added in v1.3.1
func (mc *ModbusClient) ReadRegister(addr uint16, regType RegType) (value uint16, err error)
Reads a single 16-bit register (function code 03 or 04).
func (*ModbusClient) ReadRegisters ¶ added in v1.3.1
func (mc *ModbusClient) ReadRegisters(addr uint16, quantity uint16, regType RegType) (values []uint16, err error)
Reads multiple 16-bit registers (function code 03 or 04).
func (*ModbusClient) ReadUint32 ¶ added in v1.3.1
func (mc *ModbusClient) ReadUint32(addr uint16, regType RegType) (value uint32, err error)
Reads a single 32-bit register.
func (*ModbusClient) ReadUint32s ¶ added in v1.3.1
func (mc *ModbusClient) ReadUint32s(addr uint16, quantity uint16, regType RegType) (values []uint32, err error)
Reads multiple 32-bit registers.
func (*ModbusClient) ReadUint64 ¶ added in v1.3.1
func (mc *ModbusClient) ReadUint64(addr uint16, regType RegType) (value uint64, err error)
Reads a single 64-bit register.
func (*ModbusClient) ReadUint64s ¶ added in v1.3.1
func (mc *ModbusClient) ReadUint64s(addr uint16, quantity uint16, regType RegType) (values []uint64, err error)
Reads multiple 64-bit registers.
func (*ModbusClient) SetEncoding ¶ added in v1.3.1
func (mc *ModbusClient) SetEncoding(endianness Endianness, wordOrder WordOrder) (err error)
Sets the encoding (endianness and word ordering) of subsequent requests.
func (*ModbusClient) SetUnitId ¶ added in v1.3.1
func (mc *ModbusClient) SetUnitId(id uint8) (err error)
Sets the unit id of subsequent requests.
func (*ModbusClient) WriteCoil ¶ added in v1.3.1
func (mc *ModbusClient) WriteCoil(addr uint16, value bool) (err error)
Writes a single coil (function code 05)
func (*ModbusClient) WriteCoils ¶ added in v1.3.1
func (mc *ModbusClient) WriteCoils(addr uint16, values []bool) (err error)
Writes multiple coils (function code 15)
func (*ModbusClient) WriteFloat32 ¶ added in v1.3.1
func (mc *ModbusClient) WriteFloat32(addr uint16, value float32) (err error)
Writes a single 32-bit float register.
func (*ModbusClient) WriteFloat32s ¶ added in v1.3.1
func (mc *ModbusClient) WriteFloat32s(addr uint16, values []float32) (err error)
Writes multiple 32-bit float registers.
func (*ModbusClient) WriteFloat64 ¶ added in v1.3.1
func (mc *ModbusClient) WriteFloat64(addr uint16, value float64) (err error)
Writes a single 64-bit float register.
func (*ModbusClient) WriteFloat64s ¶ added in v1.3.1
func (mc *ModbusClient) WriteFloat64s(addr uint16, values []float64) (err error)
Writes multiple 64-bit float registers.
func (*ModbusClient) WriteRegister ¶ added in v1.3.1
func (mc *ModbusClient) WriteRegister(addr uint16, value uint16) (err error)
Writes a single 16-bit register (function code 06).
func (*ModbusClient) WriteRegisters ¶ added in v1.3.1
func (mc *ModbusClient) WriteRegisters(addr uint16, values []uint16) (err error)
Writes multiple 16-bit registers (function code 16).
func (*ModbusClient) WriteUint32 ¶ added in v1.3.1
func (mc *ModbusClient) WriteUint32(addr uint16, value uint32) (err error)
Writes a single 32-bit register.
func (*ModbusClient) WriteUint32s ¶ added in v1.3.1
func (mc *ModbusClient) WriteUint32s(addr uint16, values []uint32) (err error)
Writes multiple 32-bit registers.
func (*ModbusClient) WriteUint64 ¶ added in v1.3.1
func (mc *ModbusClient) WriteUint64(addr uint16, value uint64) (err error)
Writes a single 64-bit register.
func (*ModbusClient) WriteUint64s ¶ added in v1.3.1
func (mc *ModbusClient) WriteUint64s(addr uint16, values []uint64) (err error)
Writes multiple 64-bit registers.
type ModbusServer ¶ added in v1.3.1
type ModbusServer struct {
// contains filtered or unexported fields
}
Modbus server object.
func NewServer ¶ added in v1.3.1
func NewServer(conf *ServerConfiguration, reqHandler RequestHandler) ( ms *ModbusServer, err error)
Returns a new modbus server. reqHandler should be a user-provided handler object satisfying the RequestHandler interface.
func (*ModbusServer) Start ¶ added in v1.3.1
func (ms *ModbusServer) Start() (err error)
Starts accepting client connections.
func (*ModbusServer) Stop ¶ added in v1.3.1
func (ms *ModbusServer) Stop() (err error)
Stops accepting new client connections and closes any active session.
type RequestHandler ¶ added in v1.3.1
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 ServerConfiguration ¶ added in v1.3.1
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 }
Server configuration object.