modbus

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToRegisters

func BytesToRegisters(data []byte) []uint16

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

func DecodeString(registers []uint16) string

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

func NewTCPClient(host string, port int, slaveID byte) (*Client, error)

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) Close

func (c *Client) Close() error

Close closes the underlying Modbus connection.

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

func (c *Client) ReadHoldingRegisters(addr, count uint16) ([]byte, error)

ReadHoldingRegisters reads count holding registers starting at addr.

func (*Client) ReadInputRegisters

func (c *Client) ReadInputRegisters(addr, count uint16) ([]byte, error)

ReadInputRegisters reads count input registers starting at addr.

func (*Client) ReadRegisters

func (c *Client) ReadRegisters(addr, count uint16, holding bool) ([]byte, error)

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

func (c *Client) SetSlaveID(id byte)

SetSlaveID changes the slave ID for subsequent requests.

func (*Client) WriteMultipleRegisters

func (c *Client) WriteMultipleRegisters(addr uint16, values []uint16) ([]byte, error)

WriteMultipleRegisters writes multiple holding registers starting at addr.

func (*Client) WriteSingleRegister

func (c *Client) WriteSingleRegister(addr, value uint16) ([]byte, error)

WriteSingleRegister writes a single holding register.

type DataType

type DataType int

DataType represents the type of a Modbus register

const (
	U16 DataType = iota
	I16
	U32
	I32
	F32 // IEEE 754 single-precision float
	STR // Multi-word ASCII string
	U64 // Unsigned 64-bit integer (4 words)
)

func ParseDataType

func ParseDataType(s string) (DataType, error)

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.

func (DataType) String

func (d DataType) String() string

func (DataType) WordCount

func (d DataType) WordCount() int

WordCount returns the number of Modbus registers needed for a data type.

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

type SunSpecInfo

type SunSpecInfo struct {
	Manufacturer string
	Model        string
	Serial       string
	Version      string
	BaseAddress  uint16
}

SunSpecInfo holds the result of SunSpec common model discovery.

Jump to

Keyboard shortcuts

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