modbus

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FCReadCoils              uint8 = 0x01
	FCReadDiscreteInputs     uint8 = 0x02
	FCReadHoldingRegisters   uint8 = 0x03
	FCReadInputRegisters     uint8 = 0x04
	FCWriteSingleCoil        uint8 = 0x05
	FCWriteSingleRegister    uint8 = 0x06
	FCWriteMultipleCoils     uint8 = 0x0F
	FCWriteMultipleRegisters uint8 = 0x10
)

Function Codes

View Source
const (
	ExceptionIllegalFunction                    uint8 = 0x01
	ExceptionIllegalDataAddress                 uint8 = 0x02
	ExceptionIllegalDataValue                   uint8 = 0x03
	ExceptionSlaveDeviceFailure                 uint8 = 0x04
	ExceptionAcknowledge                        uint8 = 0x05
	ExceptionSlaveDeviceBusy                    uint8 = 0x06
	ExceptionMemoryParityError                  uint8 = 0x08
	ExceptionGatewayPathUnavailable             uint8 = 0x0A
	ExceptionGatewayTargetDeviceFailedToRespond uint8 = 0x0B
)

Exception Codes

View Source
const (
	ProtocolIDModbusTCP uint16 = 0x0000
)

Protocol IDs

Variables

This section is empty.

Functions

func AppendCRC16

func AppendCRC16(data []byte) []byte

AppendCRC16 appends CRC16 to the data (little endian: low byte first, then high byte)

func CalculateCRC16

func CalculateCRC16(data []byte) uint16

CalculateCRC16 implements the Modbus RTU CRC16 calculation Uses polynomial 0xA001 (reversed 0x8005) with initial value 0xFFFF

func NewModbusError

func NewModbusError(code uint8) error

func VerifyCRC16

func VerifyCRC16(data []byte) bool

VerifyCRC16 checks if the CRC16 at the end of data matches

Types

type ADU

type ADU interface {
	Pack() []byte
	Unpack([]byte) error
}

ADU interface for different transport types

type Codec

type Codec struct {
	// contains filtered or unexported fields
}

Codec handles reading and writing Modbus frames

func NewCodec

func NewCodec(transportType string) *Codec

NewCodec creates a new Codec

func (*Codec) ReadADU

func (c *Codec) ReadADU(r io.Reader) (ADU, error)

ReadADU reads a Modbus ADU from the reader

type ModbusError

type ModbusError struct {
	ExceptionCode uint8
}

ModbusError represents a Modbus exception

func (*ModbusError) Error

func (e *ModbusError) Error() string

type PDU

type PDU struct {
	FunctionCode uint8
	Data         []byte
}

PDU represents the Protocol Data Unit (Function Code + Data)

type RTUADU

type RTUADU struct {
	UnitID uint8
	PDU    PDU
	CRC    uint16
}

RTUADU represents the Modbus RTU Application Data Unit

func (*RTUADU) Pack

func (adu *RTUADU) Pack() []byte

Pack encodes the RTU ADU into bytes (including CRC)

func (*RTUADU) Unpack

func (adu *RTUADU) Unpack(data []byte) error

Unpack decodes bytes into an RTU ADU

type TCPADU

type TCPADU struct {
	TransactionID uint16
	ProtocolID    uint16
	Length        uint16
	UnitID        uint8
	PDU           PDU
}

TCPADU represents the Modbus TCP Application Data Unit

func (*TCPADU) Pack

func (adu *TCPADU) Pack() []byte

Pack encodes the TCP ADU into bytes

func (*TCPADU) Unpack

func (adu *TCPADU) Unpack(data []byte) error

Unpack decodes bytes into a TCP ADU

Jump to

Keyboard shortcuts

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