Documentation
¶
Overview ¶
Package modbus is a thin wrapper around goburrow/modbus that provides a thread-safe TCP client plus FC43 (Read Device Identification) and SunSpec common-model discovery. It's the only protocol code the agent ships with that talks raw to a device over TCP-502.
Index ¶
- func BytesToRegisters(data []byte) []uint16
- func DecodeRawValue(registers []uint16, dataType DataType, endian Endianness) (float64, error)
- func DecodeString(registers []uint16) string
- type Client
- func (c *Client) Close() error
- func (c *Client) DiscoverSunSpec() (*SunSpecInfo, error)
- func (c *Client) ReadDeviceIdentification() (*DeviceIdentification, error)
- func (c *Client) ReadHoldingRegisters(addr, count uint16) ([]byte, error)
- func (c *Client) ReadInputRegisters(addr, count uint16) ([]byte, error)
- func (c *Client) ReadRegisters(addr, count uint16, holding bool) ([]byte, error)
- func (c *Client) SendRawPDU(functionCode byte, data []byte) (respFunctionCode byte, respData []byte, err error)
- func (c *Client) SetSlaveID(id byte)
- func (c *Client) WriteMultipleRegisters(addr uint16, values []uint16) ([]byte, error)
- func (c *Client) WriteSingleRegister(addr, value uint16) ([]byte, error)
- type DataType
- type DeviceIdentification
- type Endianness
- type SunSpecInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesToRegisters ¶
BytesToRegisters converts a byte slice (from Modbus response) to uint16 registers.
func DecodeRawValue ¶
func DecodeRawValue(registers []uint16, dataType DataType, endian Endianness) (float64, error)
DecodeRawValue decodes register words without applying any scale factor. Use this when you only know the data type + endianness (no driver context).
func DecodeString ¶
DecodeString decodes register words into a string (2 bytes per register, big-endian).
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps a Modbus TCP connection with thread-safe read access.
func NewTCPClient ¶
NewTCPClient creates a Modbus TCP client connected to host:port with the given slave ID.
func NewTCPClientWithTimeout ¶
func NewTCPClientWithTimeout(host string, port int, slaveID byte, timeout time.Duration) (*Client, error)
NewTCPClientWithTimeout creates a Modbus TCP client with a custom timeout.
func (*Client) DiscoverSunSpec ¶
func (c *Client) DiscoverSunSpec() (*SunSpecInfo, error)
DiscoverSunSpec probes for SunSpec common model by looking for "SunS" header.
func (*Client) ReadDeviceIdentification ¶
func (c *Client) ReadDeviceIdentification() (*DeviceIdentification, error)
ReadDeviceIdentification sends FC 43 (MEI type 14) to read basic device identification.
func (*Client) ReadHoldingRegisters ¶
ReadHoldingRegisters reads count holding registers starting at addr.
func (*Client) ReadInputRegisters ¶
ReadInputRegisters reads count input registers starting at addr.
func (*Client) ReadRegisters ¶
ReadRegisters reads registers using the appropriate function code.
func (*Client) SendRawPDU ¶
func (c *Client) SendRawPDU(functionCode byte, data []byte) (respFunctionCode byte, respData []byte, err error)
SendRawPDU sends a raw Modbus PDU (function code + data) and returns the response PDU.
func (*Client) SetSlaveID ¶
SetSlaveID changes the slave ID for subsequent requests.
func (*Client) WriteMultipleRegisters ¶
WriteMultipleRegisters writes multiple holding registers starting at addr.
type DataType ¶
type DataType int
DataType represents the type of a Modbus register
func ParseDataType ¶
ParseDataType converts a string to DataType. Accepts both upper-case ("U16", "I32_BE") and the protocol-style lower-case ("u16", "i32_be", "f32_le"). The "_be" / "_le" suffix is purely informational on data types that take an Endianness argument — callers should pass the endianness separately via DecodeRawValue.
type DeviceIdentification ¶
type DeviceIdentification struct {
VendorName string
ProductCode string
Revision string
VendorURL string
ProductName string
ModelName string
UserAppName string
}
DeviceIdentification holds the result of a Modbus FC 43/14 Read Device Identification request.
type Endianness ¶
type Endianness int
Endianness for multi-word registers.
const ( Big Endianness = iota Little )
func EndiannessFromKind ¶
func EndiannessFromKind(kind string) Endianness
EndiannessFromKind picks Big/Little from a "kind" string like "i32_le". Defaults to Big for anything that doesn't end in _le.
func (Endianness) String ¶
func (e Endianness) String() string