packet

package
v0.0.0-...-8ec1b3d Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// MaxRegistersInReadResponse is maximum quantity of registers that can be returned by read request (fc03/fc04)
	MaxRegistersInReadResponse = uint16(125)
	// MaxCoilsInReadResponse is maximum quantity of discretes/coils that can be returned by read request (fc01/fc02)
	MaxCoilsInReadResponse = uint16(2000) // 2000/8=250 bytes
)
View Source
const (
	// FunctionReadCoils is function code for Read Coils (FC01)
	FunctionReadCoils = uint8(1) // 0x01
	// FunctionReadDiscreteInputs is function code for Read Discrete Inputs (FC02)
	FunctionReadDiscreteInputs = uint8(2) // 0x02
	// FunctionReadHoldingRegisters is function code for Read Holding Registers (FC03)
	FunctionReadHoldingRegisters = uint8(3) // 0x03
	// FunctionReadInputRegisters is function code for Read Input Registers (FC04)
	FunctionReadInputRegisters = uint8(4) // 0x04
	// FunctionWriteSingleCoil is function code for Write Single Coil (FC05)
	FunctionWriteSingleCoil = uint8(5) // 0x05
	// FunctionWriteSingleRegister is function code for Write Single Register (FC06)
	FunctionWriteSingleRegister = uint8(6) // 0x06
	// FunctionWriteMultipleCoils is function code for Write Multiple Coils (FC15)
	FunctionWriteMultipleCoils = uint8(15) // 0x0f
	// FunctionWriteMultipleRegisters is function code for Write Multiple Registers (FC16)
	FunctionWriteMultipleRegisters = uint8(16) // 0x10
	// FunctionReadServerID is function code for Read Server ID (FC16)
	FunctionReadServerID = uint8(17) // 0x11
	// FunctionReadWriteMultipleRegisters is function code for Read / Write Multiple Registers (FC23)
	FunctionReadWriteMultipleRegisters = uint8(23) // 0x17
)
View Source
const (

	// BigEndian system stores the most significant byte of a word at the smallest memory address and the least
	// significant byte at the largest. By Modbus spec BigEndian is the order how bytes are transferred over the wire.
	BigEndian ByteOrder = 1
	// LittleEndian - little-endian system stores the least-significant byte at the smallest address.
	LittleEndian ByteOrder = 2

	// Double words (word=register) (32bit types) consist of two 16bit words. Different PLCs send double words
	// differently over the wire. So 0xDCBA can be sent low word (0xBA) first 0xBADC or high word (0xDC) first 0xDCBA.
	LowWordFirst ByteOrder = 4

	// HighWordFirst reads data as words/register are ordered from left to right. High word (0xDC) is sent first.
	// Meaning PLCs little endian value 0xABCD is sent as each byte swapped and each 2 byte pair (word/register) is swapped 0xDCBA
	HighWordFirst ByteOrder = 8

	// When bytes for little endian are in 'ABCD' order then Big Endian Low Word First is in 'BADC' order
	// This mean that high word (BA) is first and low word (DC) for double word is last and bytes in words are in big endian order.
	BigEndianLowWordFirst = BigEndian | LowWordFirst // this is default endian+word order we use

	// BigEndianHighWordFirst is big-endian with high word first
	BigEndianHighWordFirst = BigEndian | HighWordFirst

	// LittleEndianLowWordFirst is little-endian with low word first
	LittleEndianLowWordFirst = LittleEndian | LowWordFirst
	// LittleEndianHighWordFirst is little-endian with high word first
	LittleEndianHighWordFirst = LittleEndian | HighWordFirst
)

Data types with Double Word/Register (4 bytes) length can have different byte order when sent over wire depending of PLC vendor Usually data is sent in true big endian format, Big-Endian with Low Word first.

Background info: http://www.digi.com/wiki/developer/index.php/Modbus_Floating_Points (about floats but 32bit int is also double word)

For example, if the number 2923517522 (hex: AE 41 56 52) was to be sent as a 32 bit unsigned integen then bytes that are send over the wire depend on 2 factors - byte order and/or register/word order.

Some devices store the 32bits in 2 registers/words in following order: a) AE41 5652 - higher (leftmost) 16 bits (high word) in the first register and the remaining low word in the second (AE41 before 5652) b) 5652 AE41 - low word in the first register and high word in the second (5652 before AE41)

Ordered in memory (vertical table): | Memory | Big E | Little E | BE Low Word First | LE Low Word First | | byte 0 | AE | 52 | 56 | 41 | | byte 1 | 41 | 56 | 52 | AE | | byte 2 | 56 | 41 | AE | 52 | | byte 3 | 52 | AE | 41 | 56 |

Ordered in memory (horizontal table): | 0 1 2 3 | Byte order | Word order | Name | | AE41 5652 | high byte first | high word first | big endian (high word first) | | 5652 AE41 | high byte first | low word first | big endian (low word first) | | 41AE 5256 | low byte first | high word first | little endian (low word first) | | 5256 41AE | low byte first | low word first | little endian (high word first) |

Example: Our PLC (modbus serving) controller/computer is using little endian

32bit (4 byte) integer 67305985 is in hex 0x01020304 (little endian), most significant byte is 01 and the lowest byte contain hex value 04. Source: http://unixpapa.com/incnote/byteorder.html

32bit (dword) integer is in:

Little Endian (ABCD) = 0x01020304  (0x04 + (0x03 << 8) + (0x02 << 16) + (0x01 << 24))

May be sent over tcp/udp as:

Big Endian (DCBA) = 0x04030201
Big Endian Low Word First (BADC) = 0x02010403 <-- used by WAGO 750-XXX to send modbus packets over tcp/udp

Variables

View Source
var (
	// ErrTCPDataTooShort is returned when received data is still too short to be actual Modbus TCP packet.
	ErrTCPDataTooShort = NewErrorParseTCP(ErrUnknown, "data is too short to be a Modbus TCP packet")
	// ErrIsNotTCPPacket is returned when received data does not look like Modbus TCP packet
	ErrIsNotTCPPacket = NewErrorParseTCP(ErrUnknown, "data does not like Modbus TCP packet")
)
View Source
var ErrInvalidCRC = errors.New("packet cyclic redundancy check does not match Modbus RTU packet bytes")

ErrInvalidCRC is error returned when packet data does not match its CRC value

Functions

func AsRTUErrorPacket

func AsRTUErrorPacket(data []byte) error

AsRTUErrorPacket converts raw packet bytes to Modbus RTU error response if possible

Example packet: 0x0a 0x81 0x02 0xb0 0x53 0x0a - unit id (0) 0x81 - function code + 128 (error bitmask) (1) 0x02 - error code (2) 0xb0 0x53 - CRC (3,4)

func AsTCPErrorPacket

func AsTCPErrorPacket(data []byte) error

AsTCPErrorPacket converts raw packet bytes to Modbus TCP error response if possible

Example packet: 0xda 0x87 0x00 0x00 0x00 0x03 0x01 0x81 0x03 0xda 0x87 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x03 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x01 - unit id (6) 0x81 - function code + 128 (error bitmask) (7) 0x03 - error code (8)

func CRC16

func CRC16(data []byte) uint16

CRC16 calculates 16 bit cyclic redundancy check (CRC) for given bytes Note about the CRC:

Polynomial: x16 + x15 + x2 + 1 (CRC-16-ANSI also known as CRC-16-IBM, normal hexadecimal algebraic polynomial being 8005 and reversed A001). Initial value: 65,535. Example of frame in hexadecimal: 01 04 02 FF FF B8 80 (CRC-16-ANSI calculation from 01 to FF gives 80B8, which is transmitted least significant byte first).

func CoilsToBytes

func CoilsToBytes(coils []bool) []byte

CoilsToBytes converts slice of coil states (as bool values) to byte slice.

func LooksLikeModbusTCP

func LooksLikeModbusTCP(data []byte, allowUnSupportedFunctionCodes bool) (expectedLen int, error error)

LooksLikeModbusTCP checks if given data starts with bytes that could be potentially parsed as Modbus TCP packet.

Types

type ByteOrder

type ByteOrder uint8

ByteOrder determines how bytes are ordered in data

type ErrCode

type ErrCode uint8

ErrCode is enumeration for response error codes

const (
	// ErrUnknown is catchall error code
	ErrUnknown ErrCode = 0
	// ErrIllegalFunction is The function code received in the query is not an allowable action for the server.
	// This may be because the function code is only applicable to newer devices, and was not implemented in the
	// unit selected. It could also indicate that the server is in the wrong state to process a request of this
	// type, for example because it is not configured and is being asked to return register values.
	// Quote from: `MODBUS Application Protocol Specification V1.1b3`, page 48
	ErrIllegalFunction ErrCode = 1
	// ErrIllegalDataAddress is The data address received in the query is not an allowable address for the server.
	// More specifically, the combination of reference number and transfer length is invalid. For a controller with 100
	// registers, the PDU addresses the first register as 0, and the last one as 99. If a request is submitted with a
	// starting register address of 96 and a quantity of registers of 4, then this request will successfully
	// operate (address-wise at least) on registers 96, 97, 98, 99. If a request is submitted with a starting
	// register address of 96 and a quantity of registers of 5, then this request will fail with Exception
	// Code 0x02 “Illegal Data Address” since it attempts to operate on registers 96, 97, 98, 99 and 100, and
	// there is no register with address 100.
	// Quote from: `MODBUS Application Protocol Specification V1.1b3`, page 48
	ErrIllegalDataAddress ErrCode = 2
	// ErrIllegalDataValue is A value contained in the query data field is not an allowable value for server.
	// This indicates a fault in the structure of the remainder of a complex request, such as that the implied length
	// is incorrect. It specifically does NOT mean that a data item submitted for storage in a register has a value
	// outside the expectation of the application program, since the MODBUS protocol is unaware of the significance of
	// any particular value of any particular register.
	// Quote from: `MODBUS Application Protocol Specification V1.1b3`, page 48
	ErrIllegalDataValue ErrCode = 3
	// ErrServerFailure is An unrecoverable error occurred while the server was attempting to perform the requested action.
	// Quote from: `MODBUS Application Protocol Specification V1.1b3`, page 48
	ErrServerFailure ErrCode = 4
	// ErrAcknowledge is Specialized use in conjunction with programming commands. The server has accepted the request
	// and is processing it, but a long duration of time will be required to do so. This response is returned to prevent
	// a timeout error from occurring in the client. The client can next issue a Poll Program Complete message to
	// determine if processing is completed.
	// Quote from: `MODBUS Application Protocol Specification V1.1b3`, page 48
	ErrAcknowledge ErrCode = 5
	// ErrServerBusy is Specialized use in conjunction with programming commands. The server is engaged in processing a
	// long duration program command. The client should retransmit the message later when the server is free.
	// Quote from: `MODBUS Application Protocol Specification V1.1b3`, page 48
	ErrServerBusy ErrCode = 6
	// ErrMemoryParityError is Specialized use in conjunction with function codes 20 and 21 and reference type 6, to
	// indicate that the extended file area failed to pass a consistency check.
	// The server attempted to read record file, but detected a parity error in the memory. The client can retry
	// the request, but service may be required on the server device.
	// Quote from: `MODBUS Application Protocol Specification V1.1b3`, page 48
	ErrMemoryParityError ErrCode = 8
	// ErrGatewayPathUnavailable is Specialized use in conjunction with gateways, indicates that the gateway was unable
	// to allocate an internal communication path from the input port to the output port for processing the request.
	// Usually means that the gateway is misconfigured or overloaded.
	// Quote from: `MODBUS Application Protocol Specification V1.1b3`, page 49
	ErrGatewayPathUnavailable ErrCode = 10
	// ErrGatewayTargetedDeviceResponse is Specialized use in conjunction with gateways, indicates that no response was
	// obtained from the target device. Usually means that the device is not present on the network.
	// Quote from: `MODBUS Application Protocol Specification V1.1b3`, page 49
	ErrGatewayTargetedDeviceResponse ErrCode = 11
)

type ErrorParseRTU

type ErrorParseRTU struct {
	Message string
	Packet  ErrorResponseRTU
}

ErrorParseRTU is parsing error that can be sent to the client

func NewErrorParseRTU

func NewErrorParseRTU(code ErrCode, message string) *ErrorParseRTU

NewErrorParseRTU creates new instance of parsing error that can be sent to the client

func (ErrorParseRTU) Bytes

func (e ErrorParseRTU) Bytes() []byte

Bytes returns ErrorParseRTU packet as bytes form

func (ErrorParseRTU) Error

func (e ErrorParseRTU) Error() string

Error translates error code to error message.

type ErrorParseTCP

type ErrorParseTCP struct {
	Message string
	Packet  ErrorResponseTCP
}

ErrorParseTCP is parsing error that can be sent to the client

func NewErrorParseTCP

func NewErrorParseTCP(code ErrCode, message string) *ErrorParseTCP

NewErrorParseTCP creates new instance of parsing error that can be sent to the client

func (ErrorParseTCP) Bytes

func (e ErrorParseTCP) Bytes() []byte

Bytes returns ErrorParseTCP packet as bytes form

func (ErrorParseTCP) Error

func (e ErrorParseTCP) Error() string

Error translates error code to error message.

type ErrorResponseRTU

type ErrorResponseRTU struct {
	UnitID   uint8
	Function uint8
	Code     ErrCode
}

ErrorResponseRTU is RTU error response send by server to client

func (ErrorResponseRTU) Bytes

func (re ErrorResponseRTU) Bytes() []byte

Bytes returns ErrorResponseRTU packet as bytes form

func (ErrorResponseRTU) Error

func (re ErrorResponseRTU) Error() string

Error translates error code to error message.

func (ErrorResponseRTU) ErrorCode

func (re ErrorResponseRTU) ErrorCode() ErrCode

ErrorCode returns error code returned by modbus server

func (ErrorResponseRTU) FunctionCode

func (re ErrorResponseRTU) FunctionCode() uint8

FunctionCode returns function code to which error response originates from / was responded to

type ErrorResponseTCP

type ErrorResponseTCP struct {
	TransactionID uint16
	UnitID        uint8
	Function      uint8
	Code          ErrCode
}

ErrorResponseTCP is TCP error response send by server to client

func (ErrorResponseTCP) Bytes

func (re ErrorResponseTCP) Bytes() []byte

Bytes returns ErrorResponseTCP packet as bytes form

func (ErrorResponseTCP) Error

func (re ErrorResponseTCP) Error() string

Error translates error code to error message.

func (ErrorResponseTCP) ErrorCode

func (re ErrorResponseTCP) ErrorCode() ErrCode

ErrorCode returns error code returned by modbus server

func (ErrorResponseTCP) FunctionCode

func (re ErrorResponseTCP) FunctionCode() uint8

FunctionCode returns function code to which error response originates from / was responded to

type MBAPHeader

type MBAPHeader struct {
	TransactionID uint16
	ProtocolID    uint16
}

MBAPHeader (Modbus Application Header) is header part of modbus TCP packet. NB: this library does pack unitID into header

func ParseMBAPHeader

func ParseMBAPHeader(data []byte) (MBAPHeader, error)

ParseMBAPHeader parses MBAPHeader from given bytes

type ModbusError

type ModbusError interface {
	Error() string
	ErrorCode() ErrCode
}

ModbusError allows distinguishing Modbus error responses (response with error code) from other (i.e. network related or parsing the response) errors when requesting data from modbus server.

type ReadCoilsRequest

type ReadCoilsRequest struct {
	UnitID       uint8
	StartAddress uint16
	Quantity     uint16
}

ReadCoilsRequest is Request for Read Coils function (FC=01)

func (ReadCoilsRequest) Bytes

func (r ReadCoilsRequest) Bytes() []byte

Bytes returns ReadCoilsRequest packet as bytes form

func (ReadCoilsRequest) FunctionCode

func (r ReadCoilsRequest) FunctionCode() uint8

FunctionCode returns function code of this request

type ReadCoilsRequestRTU

type ReadCoilsRequestRTU struct {
	ReadCoilsRequest
}

ReadCoilsRequestRTU is RTU Request for Read Coils function (FC=01)

Example packet: 0x10 0x01 0x00 0x6B 0x00 0x03 0x0e 0x96 0x10 - unit id (0) 0x01 - function code (1) 0x00 0x6B - start address (2,3) 0x00 0x03 - coils quantity to return (4,5) 0x0e 0x96 - CRC16 (6,7)

func NewReadCoilsRequestRTU

func NewReadCoilsRequestRTU(unitID uint8, startAddress uint16, quantity uint16) (*ReadCoilsRequestRTU, error)

NewReadCoilsRequestRTU creates new instance of Read Coils RTU request

func ParseReadCoilsRequestRTU

func ParseReadCoilsRequestRTU(data []byte) (*ReadCoilsRequestRTU, error)

ParseReadCoilsRequestRTU parses given bytes into ReadCoilsRequestRTU Does not check CRC

func (ReadCoilsRequestRTU) Bytes

func (r ReadCoilsRequestRTU) Bytes() []byte

Bytes returns ReadCoilsRequestRTU packet as bytes form

func (ReadCoilsRequestRTU) ExpectedResponseLength

func (r ReadCoilsRequestRTU) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type ReadCoilsRequestTCP

type ReadCoilsRequestTCP struct {
	MBAPHeader
	ReadCoilsRequest
}

ReadCoilsRequestTCP is TCP Request for Read Coils function (FC=01)

Example packet: 0x81 0x80 0x00 0x00 0x00 0x06 0x10 0x01 0x00 0x6B 0x00 0x03 0x81 0x80 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x06 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x10 - unit id (6) 0x01 - function code (7) 0x00 0x6B - start address (8,9) 0x00 0x03 - coils quantity to return (10,11)

func NewReadCoilsRequestTCP

func NewReadCoilsRequestTCP(unitID uint8, startAddress uint16, quantity uint16) (*ReadCoilsRequestTCP, error)

NewReadCoilsRequestTCP creates new instance of Read Coils TCP request

func ParseReadCoilsRequestTCP

func ParseReadCoilsRequestTCP(data []byte) (*ReadCoilsRequestTCP, error)

ParseReadCoilsRequestTCP parses given bytes into ReadCoilsRequestTCP

func (ReadCoilsRequestTCP) Bytes

func (r ReadCoilsRequestTCP) Bytes() []byte

Bytes returns ReadCoilsRequestTCP packet as bytes form

func (ReadCoilsRequestTCP) ExpectedResponseLength

func (r ReadCoilsRequestTCP) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type ReadCoilsResponse

type ReadCoilsResponse struct {
	UnitID          uint8
	CoilsByteLength uint8
	Data            []byte
}

ReadCoilsResponse is Response for Read Coils (FC=01)

func (ReadCoilsResponse) Bytes

func (r ReadCoilsResponse) Bytes() []byte

Bytes returns ReadCoilsResponse packet as bytes form

func (ReadCoilsResponse) FunctionCode

func (r ReadCoilsResponse) FunctionCode() uint8

FunctionCode returns function code of this request

func (ReadCoilsResponse) IsCoilSet

func (r ReadCoilsResponse) IsCoilSet(startAddress uint16, coilAddress uint16) (bool, error)

IsCoilSet checks if N-th coil is set in response data. Coils are counted from `startAddress` (see ReadCoilsRequest) and right to left.

type ReadCoilsResponseRTU

type ReadCoilsResponseRTU struct {
	ReadCoilsResponse
}

ReadCoilsResponseRTU is RTU Response for Read Coils (FC=01)

Example packet: 0x03 0x01 0x02 0xCD 0x6B 0xd5 0x43 0x03 - unit id (0) 0x01 - function code (1) 0x02 - coils byte count (2) 0xCD 0x6B - coils data (2 bytes = 2 // 8 coils) (3,4, ...) 0xd5 0x43 - CRC16 (n-2,n-1)

func ParseReadCoilsResponseRTU

func ParseReadCoilsResponseRTU(data []byte) (*ReadCoilsResponseRTU, error)

ParseReadCoilsResponseRTU parses given bytes into ReadCoilsResponseRTU

func (ReadCoilsResponseRTU) Bytes

func (r ReadCoilsResponseRTU) Bytes() []byte

Bytes returns ReadCoilsResponseRTU packet as bytes form

type ReadCoilsResponseTCP

type ReadCoilsResponseTCP struct {
	MBAPHeader
	ReadCoilsResponse
}

ReadCoilsResponseTCP is TCP Response for Read Coils (FC=01)

Example packet: 0x81 0x80 0x00 0x00 0x00 0x05 0x03 0x01 0x02 0xCD 0x6B 0x81 0x80 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x05 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x03 - unit id (6) 0x01 - function code (7) 0x02 - coils byte count (8) 0xCD 0x6B - coils data (2 bytes = 2 // 8 coils) (9,10, ...)

func ParseReadCoilsResponseTCP

func ParseReadCoilsResponseTCP(data []byte) (*ReadCoilsResponseTCP, error)

ParseReadCoilsResponseTCP parses given bytes into ReadCoilsResponseTCP

func (ReadCoilsResponseTCP) Bytes

func (r ReadCoilsResponseTCP) Bytes() []byte

Bytes returns ReadCoilsResponseTCP packet as bytes form

type ReadDiscreteInputsRequest

type ReadDiscreteInputsRequest struct {
	UnitID       uint8
	StartAddress uint16
	Quantity     uint16
}

ReadDiscreteInputsRequest is Request for Read Discrete Inputs (FC=02)

func (ReadDiscreteInputsRequest) Bytes

func (r ReadDiscreteInputsRequest) Bytes() []byte

Bytes returns ReadDiscreteInputsRequest packet as bytes form

func (ReadDiscreteInputsRequest) FunctionCode

func (r ReadDiscreteInputsRequest) FunctionCode() uint8

FunctionCode returns function code of this request

type ReadDiscreteInputsRequestRTU

type ReadDiscreteInputsRequestRTU struct {
	ReadDiscreteInputsRequest
}

ReadDiscreteInputsRequestRTU is RTU Request for Read Discrete Inputs (FC=02)

Example packet: 0x10 0x02 0x00 0x6B 0x00 0x03 0x4a 0x96 0x10 - unit id (0) 0x02 - function code (1) 0x00 0x6B - start address (2,3) 0x00 0x03 - discrete inputs quantity to return (4,5) 0x4a 0x96 - CRC16 (6,7)

func NewReadDiscreteInputsRequestRTU

func NewReadDiscreteInputsRequestRTU(unitID uint8, startAddress uint16, quantity uint16) (*ReadDiscreteInputsRequestRTU, error)

NewReadDiscreteInputsRequestRTU creates new instance of Read Discrete Inputs RTU request

func ParseReadDiscreteInputsRequestRTU

func ParseReadDiscreteInputsRequestRTU(data []byte) (*ReadDiscreteInputsRequestRTU, error)

ParseReadDiscreteInputsRequestRTU parses given bytes into ReadDiscreteInputsRequestRTU

func (ReadDiscreteInputsRequestRTU) Bytes

func (r ReadDiscreteInputsRequestRTU) Bytes() []byte

Bytes returns ReadDiscreteInputsRequestRTU packet as bytes form

func (ReadDiscreteInputsRequestRTU) ExpectedResponseLength

func (r ReadDiscreteInputsRequestRTU) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type ReadDiscreteInputsRequestTCP

type ReadDiscreteInputsRequestTCP struct {
	MBAPHeader
	ReadDiscreteInputsRequest
}

ReadDiscreteInputsRequestTCP is TCP Request for Read Discrete Inputs (FC=02)

Example packet: 0x81 0x80 0x00 0x00 0x00 0x06 0x10 0x02 0x00 0x6B 0x00 0x03 0x81 0x80 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x06 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x10 - unit id (6) 0x02 - function code (7) 0x00 0x6B - start address (8,9) 0x00 0x03 - discrete inputs quantity to return (10,11)

func NewReadDiscreteInputsRequestTCP

func NewReadDiscreteInputsRequestTCP(unitID uint8, startAddress uint16, quantity uint16) (*ReadDiscreteInputsRequestTCP, error)

NewReadDiscreteInputsRequestTCP creates new instance of Read Discrete Inputs TCP request

func ParseReadDiscreteInputsRequestTCP

func ParseReadDiscreteInputsRequestTCP(data []byte) (*ReadDiscreteInputsRequestTCP, error)

ParseReadDiscreteInputsRequestTCP parses given bytes into ReadDiscreteInputsRequestTCP

func (ReadDiscreteInputsRequestTCP) Bytes

func (r ReadDiscreteInputsRequestTCP) Bytes() []byte

Bytes returns ReadDiscreteInputsRequestTCP packet as bytes form

func (ReadDiscreteInputsRequestTCP) ExpectedResponseLength

func (r ReadDiscreteInputsRequestTCP) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type ReadDiscreteInputsResponse

type ReadDiscreteInputsResponse struct {
	UnitID           uint8
	InputsByteLength uint8
	Data             []byte
}

ReadDiscreteInputsResponse is Response for Read Discrete Inputs (FC=02)

func (ReadDiscreteInputsResponse) Bytes

func (r ReadDiscreteInputsResponse) Bytes() []byte

Bytes returns ReadDiscreteInputsResponse packet as bytes form

func (ReadDiscreteInputsResponse) FunctionCode

func (r ReadDiscreteInputsResponse) FunctionCode() uint8

FunctionCode returns function code of this request

func (ReadDiscreteInputsResponse) IsCoilSet

func (r ReadDiscreteInputsResponse) IsCoilSet(startAddress uint16, inputAddress uint16) (bool, error)

IsCoilSet checks if N-th discrete input is set in response data. It is alias to IsInputSet method.

func (ReadDiscreteInputsResponse) IsInputSet

func (r ReadDiscreteInputsResponse) IsInputSet(startAddress uint16, inputAddress uint16) (bool, error)

IsInputSet checks if N-th discrete input is set in response data. Inputs are counted from `startAddress` (see ReadDiscreteInputsRequest) and right to left.

type ReadDiscreteInputsResponseRTU

type ReadDiscreteInputsResponseRTU struct {
	ReadDiscreteInputsResponse
}

ReadDiscreteInputsResponseRTU is RTU Response for Read Discrete Inputs (FC=02)

Example packet: 0x03 0x02 0x02 0xCD 0x6B 0xd5 0x07 0x03 - unit id (0) 0x02 - function code (1) 0x02 - inputs byte count (2) 0xCD 0x6B - inputs data (2 bytes = 2 // 8 inputs) (3,4, ...) 0xd5 0x07 - CRC16 (n-2,n-1)

func ParseReadDiscreteInputsResponseRTU

func ParseReadDiscreteInputsResponseRTU(data []byte) (*ReadDiscreteInputsResponseRTU, error)

ParseReadDiscreteInputsResponseRTU parses given bytes into ReadDiscreteInputsResponseRTU

func (ReadDiscreteInputsResponseRTU) Bytes

func (r ReadDiscreteInputsResponseRTU) Bytes() []byte

Bytes returns ReadDiscreteInputsResponseRTU packet as bytes form

type ReadDiscreteInputsResponseTCP

type ReadDiscreteInputsResponseTCP struct {
	MBAPHeader
	ReadDiscreteInputsResponse
}

ReadDiscreteInputsResponseTCP is TCP Response for Read Discrete Inputs (FC=02)

Example packet: 0x81 0x80 0x00 0x00 0x00 0x05 0x03 0x02 0x02 0xCD 0x6B 0x81 0x80 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x05 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x03 - unit id (6) 0x02 - function code (7) 0x02 - inputs byte count (8) 0xCD 0x6B - inputs discrete data (2 bytes = 2 // 8 inputs) (9, ...)

func ParseReadDiscreteInputsResponseTCP

func ParseReadDiscreteInputsResponseTCP(data []byte) (*ReadDiscreteInputsResponseTCP, error)

ParseReadDiscreteInputsResponseTCP parses given bytes into ReadDiscreteInputsResponseTCP

func (ReadDiscreteInputsResponseTCP) Bytes

func (r ReadDiscreteInputsResponseTCP) Bytes() []byte

Bytes returns ReadDiscreteInputsResponseTCP packet as bytes form

type ReadHoldingRegistersRequest

type ReadHoldingRegistersRequest struct {
	UnitID       uint8
	StartAddress uint16
	Quantity     uint16
}

ReadHoldingRegistersRequest is Request for Read Holding Registers (FC=03)

func (ReadHoldingRegistersRequest) Bytes

func (r ReadHoldingRegistersRequest) Bytes() []byte

Bytes returns ReadHoldingRegistersRequest packet as bytes form

func (ReadHoldingRegistersRequest) FunctionCode

func (r ReadHoldingRegistersRequest) FunctionCode() uint8

FunctionCode returns function code of this request

type ReadHoldingRegistersRequestRTU

type ReadHoldingRegistersRequestRTU struct {
	ReadHoldingRegistersRequest
}

ReadHoldingRegistersRequestRTU is RTU Request for Read Holding Registers (FC=03)

Example packet: 0x01 0x03 0x00 0x6B 0x00 0x01 0xf5 0xd6 0x01 - unit id (0) 0x03 - function code (1) 0x00 0x6B - start address (2,3) 0x00 0x01 - holding registers quantity to return (4,5) 0xf5 0xd6 - CRC16 (6,7)

func NewReadHoldingRegistersRequestRTU

func NewReadHoldingRegistersRequestRTU(unitID uint8, startAddress uint16, quantity uint16) (*ReadHoldingRegistersRequestRTU, error)

NewReadHoldingRegistersRequestRTU creates new instance of Read Holding Registers RTU request

func ParseReadHoldingRegistersRequestRTU

func ParseReadHoldingRegistersRequestRTU(data []byte) (*ReadHoldingRegistersRequestRTU, error)

ParseReadHoldingRegistersRequestRTU parses given bytes into ReadHoldingRegistersRequestRTU

func (ReadHoldingRegistersRequestRTU) Bytes

Bytes returns ReadHoldingRegistersRequestRTU packet as bytes form

func (ReadHoldingRegistersRequestRTU) ExpectedResponseLength

func (r ReadHoldingRegistersRequestRTU) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type ReadHoldingRegistersRequestTCP

type ReadHoldingRegistersRequestTCP struct {
	MBAPHeader
	ReadHoldingRegistersRequest
}

ReadHoldingRegistersRequestTCP is TCP Request for Read Holding Registers (FC=03)

Example packet: 0x00 0x01 0x00 0x00 0x00 0x06 0x01 0x03 0x00 0x6B 0x00 0x01 0x00 0x01 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x06 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x01 - unit id (6) 0x03 - function code (7) 0x00 0x6B - start address (8,9) 0x00 0x01 - holding registers quantity to return (10,11)

func NewReadHoldingRegistersRequestTCP

func NewReadHoldingRegistersRequestTCP(unitID uint8, startAddress uint16, quantity uint16) (*ReadHoldingRegistersRequestTCP, error)

NewReadHoldingRegistersRequestTCP creates new instance of Read Holding Registers TCP request

func ParseReadHoldingRegistersRequestTCP

func ParseReadHoldingRegistersRequestTCP(data []byte) (*ReadHoldingRegistersRequestTCP, error)

ParseReadHoldingRegistersRequestTCP parses given bytes into ReadHoldingRegistersRequestTCP

func (ReadHoldingRegistersRequestTCP) Bytes

Bytes returns ReadHoldingRegistersRequestTCP packet as bytes form

func (ReadHoldingRegistersRequestTCP) ExpectedResponseLength

func (r ReadHoldingRegistersRequestTCP) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type ReadHoldingRegistersResponse

type ReadHoldingRegistersResponse struct {
	UnitID          uint8
	RegisterByteLen uint8
	Data            []byte
}

ReadHoldingRegistersResponse is Request for Read Holding Registers (FC=03)

func (ReadHoldingRegistersResponse) AsRegisters

func (r ReadHoldingRegistersResponse) AsRegisters(requestStartAddress uint16) (*Registers, error)

AsRegisters returns response data as Register to more convenient access

func (ReadHoldingRegistersResponse) Bytes

func (r ReadHoldingRegistersResponse) Bytes() []byte

Bytes returns ReadHoldingRegistersResponse packet as bytes form

func (ReadHoldingRegistersResponse) FunctionCode

func (r ReadHoldingRegistersResponse) FunctionCode() uint8

FunctionCode returns function code of this request

type ReadHoldingRegistersResponseRTU

type ReadHoldingRegistersResponseRTU struct {
	ReadHoldingRegistersResponse
}

ReadHoldingRegistersResponseRTU is RTU Request for Read Holding Registers (FC=03)

Example packet: 0x01 0x03 0x02 0xCD 0x6B 0xad 0x3b 0x01 - unit id (0) 0x03 - function code (1) 0x02 - returned registers byte count (2) 0xCD 0x6B - holding registers data (1 register) (3,4, ... 2 bytes for each register) 0xad 0x3b - CRC16 (n-2,n-1)

func ParseReadHoldingRegistersResponseRTU

func ParseReadHoldingRegistersResponseRTU(data []byte) (*ReadHoldingRegistersResponseRTU, error)

ParseReadHoldingRegistersResponseRTU parses given bytes into ReadHoldingRegistersResponseTCP

func (ReadHoldingRegistersResponseRTU) Bytes

Bytes returns ReadHoldingRegistersResponseRTU packet as bytes form

type ReadHoldingRegistersResponseTCP

type ReadHoldingRegistersResponseTCP struct {
	MBAPHeader
	ReadHoldingRegistersResponse
}

ReadHoldingRegistersResponseTCP is TCP Request for Read Holding Registers (FC=03)

Example packet: 0x81 0x80 0x00 0x00 0x00 0x05 0x01 0x03 0x02 0xCD 0x6B 0x81 0x80 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x05 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x01 - unit id (6) 0x03 - function code (7) 0x02 - returned registers byte count (8) 0xCD 0x6B - holding registers data (1 register) (9,10, ... 2 bytes for each register)

func ParseReadHoldingRegistersResponseTCP

func ParseReadHoldingRegistersResponseTCP(data []byte) (*ReadHoldingRegistersResponseTCP, error)

ParseReadHoldingRegistersResponseTCP parses given bytes into ReadHoldingRegistersResponseTCP

func (ReadHoldingRegistersResponseTCP) Bytes

Bytes returns ReadHoldingRegistersResponseTCP packet as bytes form

type ReadInputRegistersRequest

type ReadInputRegistersRequest struct {
	UnitID       uint8
	StartAddress uint16
	Quantity     uint16
}

ReadInputRegistersRequest is Request for Read Input Registers (FC=04)

func (ReadInputRegistersRequest) Bytes

func (r ReadInputRegistersRequest) Bytes() []byte

Bytes returns ReadInputRegistersRequest packet as bytes form

func (ReadInputRegistersRequest) FunctionCode

func (r ReadInputRegistersRequest) FunctionCode() uint8

FunctionCode returns function code of this request

type ReadInputRegistersRequestRTU

type ReadInputRegistersRequestRTU struct {
	ReadInputRegistersRequest
}

ReadInputRegistersRequestRTU is RTU Request for Read Input Registers (FC=04)

Example packet: 0x01 0x04 0x00 0x6B 0x00 0x01 0x40 0x16 0x01 - unit id (0) 0x04 - function code (1) 0x00 0x6B - start address (2,3) 0x00 0x01 - input registers quantity to return (4,5) 0x40 0x16 - CRC16 (6,7)

func NewReadInputRegistersRequestRTU

func NewReadInputRegistersRequestRTU(unitID uint8, startAddress uint16, quantity uint16) (*ReadInputRegistersRequestRTU, error)

NewReadInputRegistersRequestRTU creates new instance of Read Input Registers RTU request

func ParseReadInputRegistersRequestRTU

func ParseReadInputRegistersRequestRTU(data []byte) (*ReadInputRegistersRequestRTU, error)

ParseReadInputRegistersRequestRTU parses given bytes into ReadInputRegistersRequestRTU

func (ReadInputRegistersRequestRTU) Bytes

func (r ReadInputRegistersRequestRTU) Bytes() []byte

Bytes returns ReadInputRegistersRequestRTU packet as bytes form

func (ReadInputRegistersRequestRTU) ExpectedResponseLength

func (r ReadInputRegistersRequestRTU) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type ReadInputRegistersRequestTCP

type ReadInputRegistersRequestTCP struct {
	MBAPHeader
	ReadInputRegistersRequest
}

ReadInputRegistersRequestTCP is TCP Request for Read Input Registers (FC=04)

Example packet: 0x00 0x01 0x00 0x00 0x00 0x06 0x01 0x04 0x00 0x6B 0x00 0x01 0x00 0x01 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x06 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x01 - unit id (6) 0x04 - function code (7) 0x00 0x6B - start address (8,9) 0x00 0x01 - input registers quantity to return (10,11)

func NewReadInputRegistersRequestTCP

func NewReadInputRegistersRequestTCP(unitID uint8, startAddress uint16, quantity uint16) (*ReadInputRegistersRequestTCP, error)

NewReadInputRegistersRequestTCP creates new instance of Read Input Registers TCP request

func ParseReadInputRegistersRequestTCP

func ParseReadInputRegistersRequestTCP(data []byte) (*ReadInputRegistersRequestTCP, error)

ParseReadInputRegistersRequestTCP parses given bytes into ReadInputRegistersRequestTCP

func (ReadInputRegistersRequestTCP) Bytes

func (r ReadInputRegistersRequestTCP) Bytes() []byte

Bytes returns ReadInputRegistersRequestTCP packet as bytes form

func (ReadInputRegistersRequestTCP) ExpectedResponseLength

func (r ReadInputRegistersRequestTCP) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type ReadInputRegistersResponse

type ReadInputRegistersResponse struct {
	UnitID          uint8
	RegisterByteLen uint8
	Data            []byte
}

ReadInputRegistersResponse is Request for Read Input Registers (FC=04)

func (ReadInputRegistersResponse) AsRegisters

func (r ReadInputRegistersResponse) AsRegisters(requestStartAddress uint16) (*Registers, error)

AsRegisters returns response data as Register to more convenient access

func (ReadInputRegistersResponse) Bytes

func (r ReadInputRegistersResponse) Bytes() []byte

Bytes returns ReadInputRegistersResponse packet as bytes form

func (ReadInputRegistersResponse) FunctionCode

func (r ReadInputRegistersResponse) FunctionCode() uint8

FunctionCode returns function code of this request

type ReadInputRegistersResponseRTU

type ReadInputRegistersResponseRTU struct {
	ReadInputRegistersResponse
}

ReadInputRegistersResponseRTU is RTU Request for Read Input Registers (FC=04)

Example packet: 0x01 0x04 0x02 0xCD 0x6B 0xac 0x4f 0x01 - unit id (0) 0x04 - function code (1) 0x02 - returned registers byte count (2) 0xCD 0x6B - input registers data (1 register) (3,4, ... 2 bytes for each register) 0xac 0x4f - CRC16 (n-2,n-1)

func ParseReadInputRegistersResponseRTU

func ParseReadInputRegistersResponseRTU(data []byte) (*ReadInputRegistersResponseRTU, error)

ParseReadInputRegistersResponseRTU parses given bytes into ParseReadInputRegistersResponseRTU

func (ReadInputRegistersResponseRTU) Bytes

func (r ReadInputRegistersResponseRTU) Bytes() []byte

Bytes returns ReadInputRegistersResponseRTU packet as bytes form

type ReadInputRegistersResponseTCP

type ReadInputRegistersResponseTCP struct {
	MBAPHeader
	ReadInputRegistersResponse
}

ReadInputRegistersResponseTCP is TCP Request for Read Input Registers (FC=04)

Example packet: 0x81 0x80 0x00 0x00 0x00 0x05 0x01 0x04 0x02 0xCD 0x6B 0x81 0x80 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x05 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x01 - unit id (6) 0x04 - function code (7) 0x02 - returned registers byte count (8) 0xCD 0x6B - input registers data (1 register) (9,10, ... 2 bytes for each register)

func ParseReadInputRegistersResponseTCP

func ParseReadInputRegistersResponseTCP(data []byte) (*ReadInputRegistersResponseTCP, error)

ParseReadInputRegistersResponseTCP parses given bytes into ReadInputRegistersResponseTCP

func (ReadInputRegistersResponseTCP) Bytes

func (r ReadInputRegistersResponseTCP) Bytes() []byte

Bytes returns ReadInputRegistersResponseTCP packet as bytes form

type ReadServerIDRequest

type ReadServerIDRequest struct {
	UnitID uint8
}

ReadServerIDRequest is Request for Read Server ID function (FC=17, 0x11)

func (ReadServerIDRequest) Bytes

func (r ReadServerIDRequest) Bytes() []byte

Bytes returns ReadServerIDRequest packet as bytes form

func (ReadServerIDRequest) FunctionCode

func (r ReadServerIDRequest) FunctionCode() uint8

FunctionCode returns function code of this request

type ReadServerIDRequestRTU

type ReadServerIDRequestRTU struct {
	ReadServerIDRequest
}

ReadServerIDRequestRTU is RTU Request for Read Server ID function (FC=17, 0x11)

Example packet: 0x10 0x11 0xcc 0x7c 0x10 - unit id (0) 0x11 - function code (1) 0xcc 0x7c - CRC16 (6,7)

func NewReadServerIDRequestRTU

func NewReadServerIDRequestRTU(unitID uint8) (*ReadServerIDRequestRTU, error)

NewReadServerIDRequestRTU creates new instance of Read Server ID RTU request

func ParseReadServerIDRequestRTU

func ParseReadServerIDRequestRTU(data []byte) (*ReadServerIDRequestRTU, error)

ParseReadServerIDRequestRTU parses given bytes into ReadServerIDRequestRTU Does not check CRC

func (ReadServerIDRequestRTU) Bytes

func (r ReadServerIDRequestRTU) Bytes() []byte

Bytes returns ReadServerIDRequestRTU packet as bytes form

func (ReadServerIDRequestRTU) ExpectedResponseLength

func (r ReadServerIDRequestRTU) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type ReadServerIDRequestTCP

type ReadServerIDRequestTCP struct {
	MBAPHeader
	ReadServerIDRequest
}

ReadServerIDRequestTCP is TCP Request for Read Server ID function (FC=17, 0x11)

Example packet: 0x81 0x80 0x00 0x00 0x00 0x02 0x10 0x11 0x81 0x80 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x02 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x10 - unit id (6) 0x11 - function code (7)

func NewReadServerIDRequestTCP

func NewReadServerIDRequestTCP(unitID uint8) (*ReadServerIDRequestTCP, error)

NewReadServerIDRequestTCP creates new instance of Read Server ID TCP request

func ParseReadServerIDRequestTCP

func ParseReadServerIDRequestTCP(data []byte) (*ReadServerIDRequestTCP, error)

ParseReadServerIDRequestTCP parses given bytes into ReadServerIDRequestTCP

func (ReadServerIDRequestTCP) Bytes

func (r ReadServerIDRequestTCP) Bytes() []byte

Bytes returns ReadServerIDRequestTCP packet as bytes form

func (ReadServerIDRequestTCP) ExpectedResponseLength

func (r ReadServerIDRequestTCP) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type ReadServerIDResponse

type ReadServerIDResponse struct {
	UnitID         uint8
	Status         uint8
	ServerID       []byte
	AdditionalData []byte
}

ReadServerIDResponse is Response for Read Server ID (FC=17) 0x11

func (ReadServerIDResponse) Bytes

func (r ReadServerIDResponse) Bytes() []byte

Bytes returns ReadServerIDResponse packet as bytes form

func (ReadServerIDResponse) FunctionCode

func (r ReadServerIDResponse) FunctionCode() uint8

FunctionCode returns function code of this request

type ReadServerIDResponseRTU

type ReadServerIDResponseRTU struct {
	ReadServerIDResponse
}

ReadServerIDResponseRTU is RTU Response for Read Server ID (FC=17) 0x11

Example packet: 0x10 0x11 0x02 0x01 0x02 0x00 0x01 0x02 0xd5 0x43 0x10 - unit id (0) 0x11 - function code (1) 0x02 - byte count for server id (2) 0x01 0x02 - N bytes for server id (device specific, variable length) (3,4) 0x00 - run status (5) 0x01 0x02 - optional N bytes for additional data (device specific, variable length) (6,7) 0xd5 0x43 - CRC16 (n-2,n-1)

func ParseReadServerIDResponseRTU

func ParseReadServerIDResponseRTU(data []byte) (*ReadServerIDResponseRTU, error)

ParseReadServerIDResponseRTU parses given bytes into ReadServerIDResponseRTU

func (ReadServerIDResponseRTU) Bytes

func (r ReadServerIDResponseRTU) Bytes() []byte

Bytes returns ReadServerIDResponseRTU packet as bytes form

type ReadServerIDResponseTCP

type ReadServerIDResponseTCP struct {
	MBAPHeader
	ReadServerIDResponse
}

ReadServerIDResponseTCP is TCP Response for Read Server ID (FC=17) 0x11

Example packet: 0x81 0x80 0x00 0x00 0x00 0x08 0x10 0x11 0x02 0x01 0x02 0x00 0x01 0x02 0x81 0x80 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x08 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x10 - unit id (6) 0x11 - function code (7) 0x02 - byte count for server id (8) 0x01 0x02 - N bytes for server id (device specific, variable length) (9,10) 0x00 - run status (11) 0x01 0x02 - optional N bytes for additional data (device specific, variable length) (12,13)

func ParseReadServerIDResponseTCP

func ParseReadServerIDResponseTCP(data []byte) (*ReadServerIDResponseTCP, error)

ParseReadServerIDResponseTCP parses given bytes into ReadServerIDResponseTCP

func (ReadServerIDResponseTCP) Bytes

func (r ReadServerIDResponseTCP) Bytes() []byte

Bytes returns ReadServerIDResponseTCP packet as bytes form

type ReadWriteMultipleRegistersRequest

type ReadWriteMultipleRegistersRequest struct {
	UnitID uint8

	ReadStartAddress uint16
	ReadQuantity     uint16

	WriteStartAddress uint16
	WriteQuantity     uint16
	// WriteData must be in BigEndian byte order for server to interpret them correctly. We send them as is.
	WriteData []byte
}

ReadWriteMultipleRegistersRequest is Request for Read / Write Multiple Registers (FC=23)

func (ReadWriteMultipleRegistersRequest) Bytes

Bytes returns ReadWriteMultipleRegistersRequest packet as bytes form

func (ReadWriteMultipleRegistersRequest) FunctionCode

func (r ReadWriteMultipleRegistersRequest) FunctionCode() uint8

FunctionCode returns function code of this request

type ReadWriteMultipleRegistersRequestRTU

type ReadWriteMultipleRegistersRequestRTU struct {
	ReadWriteMultipleRegistersRequest
}

ReadWriteMultipleRegistersRequestRTU is RTU Request for Read / Write Multiple Registers (FC=23)

Example packet: 0x11 0x17 0x04 0x10 0x00 0x01 0x01 0x12 0x00 0x02 0x04 0x00 0xc8 0x00 0x82 0x64 0xe2 0x11 - unit id (0) 0x17 - function code (1) 0x04 0x10 - read registers start address (2,3) 0x00 0x01 - read registers quantity (4,5) 0x01 0x12 - write register start address (6,7) 0x00 0x02 - write quantity (8,9) 0x04 - write bytes count (10) 0x00 0xc8 0x00 0x82 - write registers data (2 registers) (11,12, ...) 0x64 0xe2 - CRC16 (n-2,n-1)

func NewReadWriteMultipleRegistersRequestRTU

func NewReadWriteMultipleRegistersRequestRTU(
	unitID uint8,
	readStartAddress uint16,
	readQuantity uint16,
	writeStartAddress uint16,
	writeData []byte,
) (*ReadWriteMultipleRegistersRequestRTU, error)

NewReadWriteMultipleRegistersRequestRTU creates new instance of Write Multiple Registers RTU request NB: bytes for `data` must be in BigEndian byte order for server to interpret them correctly

func ParseReadWriteMultipleRegistersRequestRTU

func ParseReadWriteMultipleRegistersRequestRTU(data []byte) (*ReadWriteMultipleRegistersRequestRTU, error)

ParseReadWriteMultipleRegistersRequestRTU parses given bytes into ReadWriteMultipleRegistersRequestRTU

func (ReadWriteMultipleRegistersRequestRTU) Bytes

Bytes returns ReadWriteMultipleRegistersRequestRTU packet as bytes form

func (ReadWriteMultipleRegistersRequestRTU) ExpectedResponseLength

func (r ReadWriteMultipleRegistersRequestRTU) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type ReadWriteMultipleRegistersRequestTCP

type ReadWriteMultipleRegistersRequestTCP struct {
	MBAPHeader
	ReadWriteMultipleRegistersRequest
}

ReadWriteMultipleRegistersRequestTCP is TCP Request for Read / Write Multiple Registers (FC=23)

Example packet: 0x01 0x38 0x00 0x00 0x00 0x0f 0x11 0x17 0x04 0x10 0x00 0x01 0x01 0x12 0x00 0x02 0x04 0x00 0xc8 0x00 0x82 0x01 0x38 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x0f - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x11 - unit id (6) 0x17 - function code (7) 0x04 0x10 - read registers start address (8,9) 0x00 0x01 - read registers quantity (10,11) 0x01 0x12 - write register start address (12,13) 0x00 0x02 - write quantity (14,15) 0x04 - write bytes count (16) 0x00 0xc8 0x00 0x82 - write registers data (2 registers) (17,18, ...)

func NewReadWriteMultipleRegistersRequestTCP

func NewReadWriteMultipleRegistersRequestTCP(
	unitID uint8,
	readStartAddress uint16,
	readQuantity uint16,
	writeStartAddress uint16,
	writeData []byte,
) (*ReadWriteMultipleRegistersRequestTCP, error)

NewReadWriteMultipleRegistersRequestTCP creates new instance of Write Multiple Registers TCP request NB: bytes for `data` must be in BigEndian byte order for server to interpret them correctly

func ParseReadWriteMultipleRegistersRequestTCP

func ParseReadWriteMultipleRegistersRequestTCP(data []byte) (*ReadWriteMultipleRegistersRequestTCP, error)

ParseReadWriteMultipleRegistersRequestTCP parses given bytes into ReadWriteMultipleRegistersRequestTCP

func (ReadWriteMultipleRegistersRequestTCP) Bytes

Bytes returns ReadWriteMultipleRegistersRequestTCP packet as bytes form

func (ReadWriteMultipleRegistersRequestTCP) ExpectedResponseLength

func (r ReadWriteMultipleRegistersRequestTCP) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type ReadWriteMultipleRegistersResponse

type ReadWriteMultipleRegistersResponse struct {
	UnitID          uint8
	RegisterByteLen uint8
	Data            []byte
}

ReadWriteMultipleRegistersResponse is Response for Read / Write Multiple Registers request (FC=23)

func (ReadWriteMultipleRegistersResponse) AsRegisters

func (r ReadWriteMultipleRegistersResponse) AsRegisters(requestStartAddress uint16) (*Registers, error)

AsRegisters returns response data as Register to more convenient access

func (ReadWriteMultipleRegistersResponse) Bytes

Bytes returns ReadWriteMultipleRegistersResponse packet as bytes form

func (ReadWriteMultipleRegistersResponse) FunctionCode

func (r ReadWriteMultipleRegistersResponse) FunctionCode() uint8

FunctionCode returns function code of this request

type ReadWriteMultipleRegistersResponseRTU

type ReadWriteMultipleRegistersResponseRTU struct {
	ReadWriteMultipleRegistersResponse
}

ReadWriteMultipleRegistersResponseRTU is RTU Response for Read / Write Multiple Registers request (FC=23)

Example packet: 0x11 0x17 0x02 0xCD 0x6B 0x69 0x08 0x11 - unit id (0) 0x17 - function code (1) 0x02 - registers bytes count (2) 0xCD 0x6B - write registers data (1 registers) (3, 4, ...) 0x69 0x08 - CRC16 (n-2,n-1)

func ParseReadWriteMultipleRegistersResponseRTU

func ParseReadWriteMultipleRegistersResponseRTU(data []byte) (*ReadWriteMultipleRegistersResponseRTU, error)

ParseReadWriteMultipleRegistersResponseRTU parses given bytes into ReadWriteMultipleRegistersResponseRTU

func (ReadWriteMultipleRegistersResponseRTU) Bytes

Bytes returns ReadWriteMultipleRegistersResponseRTU packet as bytes form

type ReadWriteMultipleRegistersResponseTCP

type ReadWriteMultipleRegistersResponseTCP struct {
	MBAPHeader
	ReadWriteMultipleRegistersResponse
}

ReadWriteMultipleRegistersResponseTCP is TCP Response for Read / Write Multiple Registers request (FC=23)

Example packet: 0x01 0x38 0x00 0x00 0x00 0x05 0x11 0x17 0x02 0xCD 0x6B 0x01 0x38 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x05 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x11 - unit id (6) 0x17 - function code (7) 0x02 - registers bytes count (8) 0xCD 0x6B - write registers data (1 registers) (9, 10, ...)

func ParseReadWriteMultipleRegistersResponseTCP

func ParseReadWriteMultipleRegistersResponseTCP(data []byte) (*ReadWriteMultipleRegistersResponseTCP, error)

ParseReadWriteMultipleRegistersResponseTCP parses given bytes into ReadWriteMultipleRegistersResponseTCP

func (ReadWriteMultipleRegistersResponseTCP) Bytes

Bytes returns ReadWriteMultipleRegistersResponseTCP packet as bytes form

type Registers

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

Registers provides more convenient access to data returned by register response

func NewRegisters

func NewRegisters(data []byte, startAddress uint16) (*Registers, error)

NewRegisters creates new instance of Registers

func (Registers) Bit

func (r Registers) Bit(address uint16, bit uint8) (bool, error)

Bit checks if N-th bit is set in register. NB: Bits are counted from 0 and right to left.

func (Registers) Byte

func (r Registers) Byte(address uint16, fromHighByte bool) (byte, error)

Byte returns register data as byte from given address high/low byte. By default High byte is 0th and Low byte is 1th byte.

func (Registers) Bytes

func (r Registers) Bytes(address uint16, length uint8) ([]byte, error)

Bytes returns register data as byte slice starting from given address to given length in bytes.

func (Registers) BytesWithByteOrder

func (r Registers) BytesWithByteOrder(address uint16, length uint8, wantByteOrder ByteOrder) ([]byte, error)

BytesWithByteOrder returns register data as byte slice starting from given address to given length in bytes and byte order.

func (Registers) DoubleRegister

func (r Registers) DoubleRegister(address uint16, byteOrder ByteOrder) ([]byte, error)

DoubleRegister returns two registers data (32bit) from starting from given address using word/register order

func (Registers) Float32

func (r Registers) Float32(address uint16) (float32, error)

Float32 returns register data as float32 from given address. NB: Float32 size is 2 registers (32bits, 4 bytes).

func (Registers) Float32WithByteOrder

func (r Registers) Float32WithByteOrder(address uint16, byteOrder ByteOrder) (float32, error)

Float32WithByteOrder returns register data as float32 from given address with given byte order. NB: float32 size is 2 registers (32bits, 4 bytes).

func (Registers) Float64

func (r Registers) Float64(address uint16) (float64, error)

Float64 returns register data as float64 from given address. NB: Float64 size is 4 registers (64bits, 8 bytes).

func (Registers) Float64WithByteOrder

func (r Registers) Float64WithByteOrder(address uint16, byteOrder ByteOrder) (float64, error)

Float64WithByteOrder returns register data as float64 from given address with given byte order. NB: Float64 size is 4 registers (64bits, 8 bytes).

func (Registers) Int16

func (r Registers) Int16(address uint16) (int16, error)

Int16 returns register data as int16 from given address. NB: Int16 size is 1 register (16bits, 2 bytes).

func (Registers) Int32

func (r Registers) Int32(address uint16) (int32, error)

Int32 returns register data as int32 from given address. NB: Int32 size is 2 registers (32bits, 4 bytes).

func (Registers) Int32WithByteOrder

func (r Registers) Int32WithByteOrder(address uint16, byteOrder ByteOrder) (int32, error)

Int32WithByteOrder returns register data as int32 from given address with given byte order. NB: int32 size is 2 registers (32bits, 4 bytes).

func (Registers) Int64

func (r Registers) Int64(address uint16) (int64, error)

Int64 returns register data as int64 from given address. NB: Int64 size is 4 registers (64bits, 8 bytes).

func (Registers) Int64WithByteOrder

func (r Registers) Int64WithByteOrder(address uint16, byteOrder ByteOrder) (int64, error)

Int64WithByteOrder returns register data as int64 from given address with given byte order. NB: int64 size is 4 registers (64bits, 8 bytes).

func (Registers) Int8

func (r Registers) Int8(address uint16, fromHighByte bool) (int8, error)

Int8 returns register data as int8 from given address high/low byte. By default High byte is 0th and Low byte is 1th byte.

func (Registers) IsEqualBytes

func (r Registers) IsEqualBytes(registerAddress uint16, addressLengthInBytes uint8, bytes []byte) (bool, error)

IsEqualBytes checks if data at given address, to given length, is equal to given bytes Equality check is done against raw data from request which is in Big Endian format

func (Registers) QuadRegister

func (r Registers) QuadRegister(address uint16, byteOrder ByteOrder) ([]byte, error)

QuadRegister returns four registers data (64bit) from starting from given address using word/register order

func (Registers) Register

func (r Registers) Register(address uint16) ([]byte, error)

Register returns single register data (16bit) from given address

func (Registers) String

func (r Registers) String(address uint16, length uint8) (string, error)

String returns register data as string starting from given address to given length. Data is interpreted as ASCII 0x0 (null) terminated string.

func (Registers) StringWithByteOrder

func (r Registers) StringWithByteOrder(address uint16, length uint8, byteOrder ByteOrder) (string, error)

StringWithByteOrder returns register data as string starting from given address to given length and byte order. Data is interpreted as ASCII 0x0 (null) terminated string.

func (Registers) Uint16

func (r Registers) Uint16(address uint16) (uint16, error)

Uint16 returns register data as uint16 from given address. NB: Uint16 size is 1 register (16bits, 2 bytes).

func (Registers) Uint32

func (r Registers) Uint32(address uint16) (uint32, error)

Uint32 returns register data as uint32 from given address. NB: Uint32 size is 2 registers (32bits, 4 bytes).

func (Registers) Uint32WithByteOrder

func (r Registers) Uint32WithByteOrder(address uint16, byteOrder ByteOrder) (uint32, error)

Uint32WithByteOrder returns register data as uint32 from given address with given byte order. NB: uint32 size is 2 registers (32bits, 4 bytes).

func (Registers) Uint64

func (r Registers) Uint64(address uint16) (uint64, error)

Uint64 returns register data as uint64 from given address. NB: Uint64 size is 4 registers (64bits, 8 bytes).

func (Registers) Uint64WithByteOrder

func (r Registers) Uint64WithByteOrder(address uint16, byteOrder ByteOrder) (uint64, error)

Uint64WithByteOrder returns register data as uint64 from given address with given byte order. NB: uint64 size is 4 registers (64bits, 8 bytes).

func (Registers) Uint8

func (r Registers) Uint8(address uint16, fromHighByte bool) (uint8, error)

Uint8 returns register data as uint8 from given address high/low byte. By default High byte is 0th and Low byte is 1th byte.

func (*Registers) WithByteOrder

func (r *Registers) WithByteOrder(byteOrder ByteOrder) *Registers

WithByteOrder sets byte order as default byte order in Registers

type Request

type Request interface {
	// FunctionCode returns function code of this request
	FunctionCode() uint8
	// Bytes returns packet as bytes form
	Bytes() []byte
	// ExpectedResponseLength returns length of bytes that valid response to this request would be
	ExpectedResponseLength() int
}

Request is common interface of modbus request packets

func ParseRTURequest

func ParseRTURequest(data []byte) (Request, error)

ParseRTURequest parses given bytes into modbus RTU request packet or returns error Does not check CRC.

func ParseTCPRequest

func ParseTCPRequest(data []byte) (Request, error)

ParseTCPRequest parses given bytes into modbus TCP request packet or returns error

type Response

type Response interface {
	// FunctionCode returns function code of this request
	FunctionCode() uint8
	// Bytes returns packet as bytes form
	Bytes() []byte
}

Response is common interface of modbus response packets

func ParseRTURequestWithCRC

func ParseRTURequestWithCRC(data []byte) (Response, error)

ParseRTURequestWithCRC checks packet CRC and parses given bytes into modbus RTU request packet or returns error

func ParseRTUResponse

func ParseRTUResponse(data []byte) (Response, error)

ParseRTUResponse parses given bytes into modbus RTU response packet or into ErrorResponseRTU or returns error

func ParseRTUResponseWithCRC

func ParseRTUResponseWithCRC(data []byte) (Response, error)

ParseRTUResponseWithCRC checks packet CRC and parses given bytes into modbus RTU response packet or into ErrorResponseRTU or returns error

func ParseTCPResponse

func ParseTCPResponse(data []byte) (Response, error)

ParseTCPResponse parses given bytes into modbus TCP response packet or into ErrorResponseTCP or returns error

type WriteMultipleCoilsRequest

type WriteMultipleCoilsRequest struct {
	UnitID       uint8
	StartAddress uint16
	CoilCount    uint16
	Data         []byte
}

WriteMultipleCoilsRequest is Request for Write Multiple Coils (FC=15)

func (WriteMultipleCoilsRequest) Bytes

func (r WriteMultipleCoilsRequest) Bytes() []byte

Bytes returns WriteMultipleCoilsRequest packet as bytes form

func (WriteMultipleCoilsRequest) FunctionCode

func (r WriteMultipleCoilsRequest) FunctionCode() uint8

FunctionCode returns function code of this request

type WriteMultipleCoilsRequestRTU

type WriteMultipleCoilsRequestRTU struct {
	WriteMultipleCoilsRequest
}

WriteMultipleCoilsRequestRTU is RTU Request for Write Multiple Coils (FC=15)

Example packet: 0x11 0x0F 0x04 0x10 0x00 0x03 0x01 0x05 0x8e 0x1f 0x11 - unit id (0) 0x0F - function code (1) 0x04 0x10 - start address (2,3) 0x00 0x03 - count of coils to write (4,5) 0x01 - coils byte count (6) 0x05 - coils data (7, ...) 0x8e 0x1f - CRC16 (n-2,n-1)

func NewWriteMultipleCoilsRequestRTU

func NewWriteMultipleCoilsRequestRTU(unitID uint8, startAddress uint16, coils []bool) (*WriteMultipleCoilsRequestRTU, error)

NewWriteMultipleCoilsRequestRTU creates new instance of Write Multiple Coils RTU request

func ParseWriteMultipleCoilsRequestRTU

func ParseWriteMultipleCoilsRequestRTU(data []byte) (*WriteMultipleCoilsRequestRTU, error)

ParseWriteMultipleCoilsRequestRTU parses given bytes into WriteMultipleCoilsRequestRTU

func (WriteMultipleCoilsRequestRTU) Bytes

func (r WriteMultipleCoilsRequestRTU) Bytes() []byte

Bytes returns WriteMultipleCoilsRequestRTU packet as bytes form

func (WriteMultipleCoilsRequestRTU) ExpectedResponseLength

func (r WriteMultipleCoilsRequestRTU) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type WriteMultipleCoilsRequestTCP

type WriteMultipleCoilsRequestTCP struct {
	MBAPHeader
	WriteMultipleCoilsRequest
}

WriteMultipleCoilsRequestTCP is TCP Request for Write Multiple Coils (FC=15)

Example packet: 0x01 0x38 0x00 0x00 0x00 0x08 0x11 0x0F 0x04 0x10 0x00 0x03 0x01 0x05 0x01 0x38 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x08 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x11 - unit id (6) 0x0F - function code (7) 0x04 0x10 - start address (8,9) 0x00 0x03 - count of coils to write (10,11) 0x01 - coils byte count (12) 0x05 - coils data (13, ...)

func NewWriteMultipleCoilsRequestTCP

func NewWriteMultipleCoilsRequestTCP(unitID uint8, startAddress uint16, coils []bool) (*WriteMultipleCoilsRequestTCP, error)

NewWriteMultipleCoilsRequestTCP creates new instance of Write Multiple Coils TCP request

func ParseWriteMultipleCoilsRequestTCP

func ParseWriteMultipleCoilsRequestTCP(data []byte) (*WriteMultipleCoilsRequestTCP, error)

ParseWriteMultipleCoilsRequestTCP parses given bytes into WriteMultipleCoilsRequestTCP

func (WriteMultipleCoilsRequestTCP) Bytes

func (r WriteMultipleCoilsRequestTCP) Bytes() []byte

Bytes returns WriteMultipleCoilsRequestTCP packet as bytes form

func (WriteMultipleCoilsRequestTCP) ExpectedResponseLength

func (r WriteMultipleCoilsRequestTCP) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type WriteMultipleCoilsResponse

type WriteMultipleCoilsResponse struct {
	UnitID       uint8
	StartAddress uint16
	CoilCount    uint16
}

WriteMultipleCoilsResponse is Response for Write Multiple Coils (FC=15)

func (WriteMultipleCoilsResponse) Bytes

func (r WriteMultipleCoilsResponse) Bytes() []byte

Bytes returns WriteMultipleCoilsResponse packet as bytes form

func (WriteMultipleCoilsResponse) FunctionCode

func (r WriteMultipleCoilsResponse) FunctionCode() uint8

FunctionCode returns function code of this request

type WriteMultipleCoilsResponseRTU

type WriteMultipleCoilsResponseRTU struct {
	WriteMultipleCoilsResponse
}

WriteMultipleCoilsResponseRTU is RTU Response for Write Multiple Coils (FC=15)

Example packet: 0x11 0x0F 0x04 0x10 0x00 0x03 0x17 0xaf 0x11 - unit id (0) 0x0F - function code (1) 0x04 0x10 - start address (2,3) 0x00 0x03 - count of coils written (4,5) 0x17 0xaf - CRC16 (6,7)

func ParseWriteMultipleCoilsResponseRTU

func ParseWriteMultipleCoilsResponseRTU(data []byte) (*WriteMultipleCoilsResponseRTU, error)

ParseWriteMultipleCoilsResponseRTU parses given bytes into WriteMultipleCoilsResponseRTU

func (WriteMultipleCoilsResponseRTU) Bytes

func (r WriteMultipleCoilsResponseRTU) Bytes() []byte

Bytes returns WriteMultipleCoilsResponseRTU packet as bytes form

type WriteMultipleCoilsResponseTCP

type WriteMultipleCoilsResponseTCP struct {
	MBAPHeader
	WriteMultipleCoilsResponse
}

WriteMultipleCoilsResponseTCP is TCP Response for Write Multiple Coils (FC=15)

Example packet: 0x01 0x38 0x00 0x00 0x00 0x06 0x11 0x0F 0x04 0x10 0x00 0x03 0x01 0x38 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x06 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x11 - unit id (6) 0x0F - function code (7) 0x04 0x10 - start address (8,9) 0x00 0x03 - count of coils written (10,11)

func ParseWriteMultipleCoilsResponseTCP

func ParseWriteMultipleCoilsResponseTCP(data []byte) (*WriteMultipleCoilsResponseTCP, error)

ParseWriteMultipleCoilsResponseTCP parses given bytes into ParseWriteMultipleCoilsResponseTCP

func (WriteMultipleCoilsResponseTCP) Bytes

func (r WriteMultipleCoilsResponseTCP) Bytes() []byte

Bytes returns WriteMultipleCoilsResponseTCP packet as bytes form

type WriteMultipleRegistersRequest

type WriteMultipleRegistersRequest struct {
	UnitID        uint8
	StartAddress  uint16
	RegisterCount uint16
	// Data must be in BigEndian byte order for server to interpret them correctly. We send them as is.
	Data []byte
}

WriteMultipleRegistersRequest is Request for Write Multiple Registers (FC=16)

func (WriteMultipleRegistersRequest) Bytes

func (r WriteMultipleRegistersRequest) Bytes() []byte

Bytes returns WriteMultipleRegistersRequest packet as bytes form

func (WriteMultipleRegistersRequest) FunctionCode

func (r WriteMultipleRegistersRequest) FunctionCode() uint8

FunctionCode returns function code of this request

type WriteMultipleRegistersRequestRTU

type WriteMultipleRegistersRequestRTU struct {
	WriteMultipleRegistersRequest
}

WriteMultipleRegistersRequestRTU is RTU Request for Write Multiple Registers (FC=16)

Example packet: 0x11 0x10 0x04 0x10 0x00 0x03 0x06 0x00 0xC8 0x00 0x82 0x87 0x01 0x2f 0x7d 0x11 - unit id (0) 0x10 - function code (1) 0x04 0x10 - start address (2,3) 0x00 0x03 - count of register to write (4,5) 0x06 - registers byte count (6) 0x00 0xC8 0x00 0x82 0x87 0x01 - registers data (7,8, ...) 0x2f 0x7d - CRC16 (n-2,n-1)

func NewWriteMultipleRegistersRequestRTU

func NewWriteMultipleRegistersRequestRTU(unitID uint8, startAddress uint16, data []byte) (*WriteMultipleRegistersRequestRTU, error)

NewWriteMultipleRegistersRequestRTU creates new instance of Write Multiple Registers RTU request NB: bytes for `data` must be in BigEndian byte order for server to interpret them correctly

func ParseWriteMultipleRegistersRequestRTU

func ParseWriteMultipleRegistersRequestRTU(data []byte) (*WriteMultipleRegistersRequestRTU, error)

ParseWriteMultipleRegistersRequestRTU parses given bytes into WriteMultipleRegistersRequestRTU

func (WriteMultipleRegistersRequestRTU) Bytes

Bytes returns WriteMultipleRegistersRequestRTU packet as bytes form

func (WriteMultipleRegistersRequestRTU) ExpectedResponseLength

func (r WriteMultipleRegistersRequestRTU) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type WriteMultipleRegistersRequestTCP

type WriteMultipleRegistersRequestTCP struct {
	MBAPHeader
	WriteMultipleRegistersRequest
}

WriteMultipleRegistersRequestTCP is TCP Request for Write Multiple Registers (FC=16)

Example packet: 0x01 0x38 0x00 0x00 0x00 0x0d 0x11 0x10 0x04 0x10 0x00 0x03 0x06 0x00 0xC8 0x00 0x82 0x87 0x01 0x01 0x38 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x0d - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x11 - unit id (6) 0x10 - function code (7) 0x04 0x10 - start address (8,9) 0x00 0x03 - count of register to write (10,11) 0x06 - registers byte count (12) 0x00 0xC8 0x00 0x82 0x87 0x01 - registers data (13, ...)

func NewWriteMultipleRegistersRequestTCP

func NewWriteMultipleRegistersRequestTCP(unitID uint8, startAddress uint16, data []byte) (*WriteMultipleRegistersRequestTCP, error)

NewWriteMultipleRegistersRequestTCP creates new instance of Write Multiple Registers TCP request NB: bytes for `data` must be in BigEndian byte order for server to interpret them correctly

func ParseWriteMultipleRegistersRequestTCP

func ParseWriteMultipleRegistersRequestTCP(data []byte) (*WriteMultipleRegistersRequestTCP, error)

ParseWriteMultipleRegistersRequestTCP parses given bytes into WriteMultipleRegistersRequestTCP

func (WriteMultipleRegistersRequestTCP) Bytes

Bytes returns WriteMultipleRegistersRequestTCP packet as bytes form

func (WriteMultipleRegistersRequestTCP) ExpectedResponseLength

func (r WriteMultipleRegistersRequestTCP) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type WriteMultipleRegistersResponse

type WriteMultipleRegistersResponse struct {
	UnitID        uint8
	StartAddress  uint16
	RegisterCount uint16
}

WriteMultipleRegistersResponse is Response for Write Multiple Registers (FC=16)

func (WriteMultipleRegistersResponse) Bytes

Bytes returns WriteMultipleRegistersResponse packet as bytes form

func (WriteMultipleRegistersResponse) FunctionCode

func (r WriteMultipleRegistersResponse) FunctionCode() uint8

FunctionCode returns function code of this request

type WriteMultipleRegistersResponseRTU

type WriteMultipleRegistersResponseRTU struct {
	WriteMultipleRegistersResponse
}

WriteMultipleRegistersResponseRTU is RTU Response for Write Multiple Registers (FC=16)

Example packet: 0x11 0x10 0x04 0x10 0x00 0x03 0x82 0x6d 0x11 - unit id (0) 0x10 - function code (1) 0x04 0x10 - start address (2,3) 0x00 0x03 - count of registers written (4,5) 0x82 0x6d - CRC16 (6,7)

func ParseWriteMultipleRegistersResponseRTU

func ParseWriteMultipleRegistersResponseRTU(data []byte) (*WriteMultipleRegistersResponseRTU, error)

ParseWriteMultipleRegistersResponseRTU parses given bytes into WriteMultipleRegistersResponseRTU

func (WriteMultipleRegistersResponseRTU) Bytes

Bytes returns WriteMultipleRegistersResponseRTU packet as bytes form

type WriteMultipleRegistersResponseTCP

type WriteMultipleRegistersResponseTCP struct {
	MBAPHeader
	WriteMultipleRegistersResponse
}

WriteMultipleRegistersResponseTCP is TCP Response for Write Multiple Registers (FC=16)

Example packet: 0x01 0x38 0x00 0x00 0x00 0x06 0x11 0x10 0x04 0x10 0x00 0x03 0x01 0x38 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x06 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x11 - unit id (6) 0x10 - function code (7) 0x04 0x10 - start address (8,9) 0x00 0x03 - count of registers written (10,11)

func ParseWriteMultipleRegistersResponseTCP

func ParseWriteMultipleRegistersResponseTCP(data []byte) (*WriteMultipleRegistersResponseTCP, error)

ParseWriteMultipleRegistersResponseTCP parses given bytes into ParseWriteMultipleRegistersResponseTCP

func (WriteMultipleRegistersResponseTCP) Bytes

Bytes returns WriteMultipleRegistersResponseTCP packet as bytes form

type WriteSingleCoilRequest

type WriteSingleCoilRequest struct {
	UnitID    uint8
	Address   uint16
	CoilState bool
}

WriteSingleCoilRequest is Request for Write Single Coil (FC=05)

func (WriteSingleCoilRequest) Bytes

func (r WriteSingleCoilRequest) Bytes() []byte

Bytes returns WriteSingleCoilRequest packet as bytes form

func (WriteSingleCoilRequest) FunctionCode

func (r WriteSingleCoilRequest) FunctionCode() uint8

FunctionCode returns function code of this request

type WriteSingleCoilRequestRTU

type WriteSingleCoilRequestRTU struct {
	WriteSingleCoilRequest
}

WriteSingleCoilRequestRTU is RTU Request for Write Single Coil (FC=05)

Data part of packet is always 4 bytes - 2 byte for address and 2 byte for coil status (FF00 = on, 0000 = off). For example: coil at address 1 is turned on '0x00 0x01 0xFF 0x00' For example: coil at address 10 is turned off '0x00 0x0A 0x00 0x00'

Example packet: 0x11 0x05 0x00 0x6B 0xFF 0x00 0xff 0x76 0x11 - unit id (0) 0x05 - function code (1) 0x00 0x6B - start address (2,3) 0xFF 0x00 - coil data (true) (4,5) 0xff 0x76 - CRC16 (6,7)

func NewWriteSingleCoilRequestRTU

func NewWriteSingleCoilRequestRTU(unitID uint8, address uint16, coilState bool) (*WriteSingleCoilRequestRTU, error)

NewWriteSingleCoilRequestRTU creates new instance of Write Single Coil RTU request

func ParseWriteSingleCoilRequestRTU

func ParseWriteSingleCoilRequestRTU(data []byte) (*WriteSingleCoilRequestRTU, error)

ParseWriteSingleCoilRequestRTU parses given bytes into WriteSingleCoilRequestRTU

func (WriteSingleCoilRequestRTU) Bytes

func (r WriteSingleCoilRequestRTU) Bytes() []byte

Bytes returns WriteSingleCoilRequestRTU packet as bytes form

func (WriteSingleCoilRequestRTU) ExpectedResponseLength

func (r WriteSingleCoilRequestRTU) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type WriteSingleCoilRequestTCP

type WriteSingleCoilRequestTCP struct {
	MBAPHeader
	WriteSingleCoilRequest
}

WriteSingleCoilRequestTCP is TCP Request for Write Single Coil (FC=05)

Data part of packet is always 4 bytes - 2 byte for address and 2 byte for coil status (FF00 = on, 0000 = off). For example: coil at address 1 is turned on '0x00 0x01 0xFF 0x00' For example: coil at address 10 is turned off '0x00 0x0A 0x00 0x00'

Example packet: 0x00 0x01 0x00 0x00 0x00 0x06 0x11 0x05 0x00 0x6B 0xFF 0x00 0x00 0x01 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x06 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x11 - unit id (6) 0x05 - function code (7) 0x00 0x6B - start address (8,9) 0xFF 0x00 - coil data (true) (10,11)

func NewWriteSingleCoilRequestTCP

func NewWriteSingleCoilRequestTCP(unitID uint8, address uint16, coilState bool) (*WriteSingleCoilRequestTCP, error)

NewWriteSingleCoilRequestTCP creates new instance of Write Single Coil TCP request

func ParseWriteSingleCoilRequestTCP

func ParseWriteSingleCoilRequestTCP(data []byte) (*WriteSingleCoilRequestTCP, error)

ParseWriteSingleCoilRequestTCP parses given bytes into WriteSingleCoilRequestTCP

func (WriteSingleCoilRequestTCP) Bytes

func (r WriteSingleCoilRequestTCP) Bytes() []byte

Bytes returns WriteSingleCoilRequestTCP packet as bytes form

func (WriteSingleCoilRequestTCP) ExpectedResponseLength

func (r WriteSingleCoilRequestTCP) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type WriteSingleCoilResponse

type WriteSingleCoilResponse struct {
	UnitID       uint8
	StartAddress uint16
	CoilState    bool
}

WriteSingleCoilResponse is Response for Write Single Coil (FC=05)

func (WriteSingleCoilResponse) Bytes

func (r WriteSingleCoilResponse) Bytes() []byte

Bytes returns WriteSingleCoilResponse packet as bytes form

func (WriteSingleCoilResponse) FunctionCode

func (r WriteSingleCoilResponse) FunctionCode() uint8

FunctionCode returns function code of this request

type WriteSingleCoilResponseRTU

type WriteSingleCoilResponseRTU struct {
	WriteSingleCoilResponse
}

WriteSingleCoilResponseRTU is RTU Response for Write Single Coil (FC=05)

Data part of packet is always 4 bytes - 2 byte for address and 2 byte for coil status (FF00 = on, 0000 = off). For example: coil at address 1 is turned on '0x00 0x01 0xFF 0x00' For example: coil at address 10 is turned off '0x00 0x0A 0x00 0x00'

Example packet: 0x03 0x05 0x00 0x02 0xFF 0x00 0x2c 0x18 0x03 - unit id (0) 0x05 - function code (1) 0x00 0x02 - start address (2,3) 0xFF 0x00 - coil data (true) (4,5) 0x2c 0x18 - CRC16 (6,7)

func ParseWriteSingleCoilResponseRTU

func ParseWriteSingleCoilResponseRTU(data []byte) (*WriteSingleCoilResponseRTU, error)

ParseWriteSingleCoilResponseRTU parses given bytes into WriteSingleCoilResponseRTU

func (WriteSingleCoilResponseRTU) Bytes

func (r WriteSingleCoilResponseRTU) Bytes() []byte

Bytes returns WriteSingleCoilResponseRTU packet as bytes form

type WriteSingleCoilResponseTCP

type WriteSingleCoilResponseTCP struct {
	MBAPHeader
	WriteSingleCoilResponse
}

WriteSingleCoilResponseTCP is TCP Response for Write Single Coil (FC=05)

Data part of packet is always 4 bytes - 2 byte for address and 2 byte for coil status (FF00 = on, 0000 = off). For example: coil at address 1 is turned on '0x00 0x01 0xFF 0x00' For example: coil at address 10 is turned off '0x00 0x0A 0x00 0x00'

Example packet: 0x00 0x01 0x00 0x00 0x00 0x06 0x03 0x05 0x00 0x02 0xFF 0x00 0x00 0x01 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x06 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x03 - unit id (6) 0x05 - function code (7) 0x00 0x02 - start address (8,9) 0xFF 0x00 - coil data (true) (10,11)

func ParseWriteSingleCoilResponseTCP

func ParseWriteSingleCoilResponseTCP(data []byte) (*WriteSingleCoilResponseTCP, error)

ParseWriteSingleCoilResponseTCP parses given bytes into ParseWriteSingleCoilResponseTCP

func (WriteSingleCoilResponseTCP) Bytes

func (r WriteSingleCoilResponseTCP) Bytes() []byte

Bytes returns WriteSingleCoilResponseTCP packet as bytes form

type WriteSingleRegisterRequest

type WriteSingleRegisterRequest struct {
	UnitID  uint8
	Address uint16
	// Data must be in BigEndian byte order for server to interpret them correctly. We send them as is.
	Data [2]byte
}

WriteSingleRegisterRequest is Request for Write Single Register (FC=06)

func (WriteSingleRegisterRequest) Bytes

func (r WriteSingleRegisterRequest) Bytes() []byte

Bytes returns WriteSingleRegisterRequest packet as bytes form

func (WriteSingleRegisterRequest) FunctionCode

func (r WriteSingleRegisterRequest) FunctionCode() uint8

FunctionCode returns function code of this request

type WriteSingleRegisterRequestRTU

type WriteSingleRegisterRequestRTU struct {
	WriteSingleRegisterRequest
}

WriteSingleRegisterRequestRTU is RTU Request for Write Single Register (FC=06)

Example packet: 0x11 0x06 0x00 0x6B 0x01 0x01 0x3a 0xd6 0x11 - unit id (0) 0x06 - function code (1) 0x00 0x6B - start address (2,3) 0x01 0x01 - register data (4,5) 0x3a 0xd6 - CRC16 (6,7)

func NewWriteSingleRegisterRequestRTU

func NewWriteSingleRegisterRequestRTU(unitID uint8, address uint16, data []byte) (*WriteSingleRegisterRequestRTU, error)

NewWriteSingleRegisterRequestRTU creates new instance of Write Single Register RTU request NB: byte slice for `data` must be in BigEndian byte order for server to interpret them correctly

func ParseWriteSingleRegisterRequestRTU

func ParseWriteSingleRegisterRequestRTU(data []byte) (*WriteSingleRegisterRequestRTU, error)

ParseWriteSingleRegisterRequestRTU parses given bytes into WriteSingleRegisterRequestRTU

func (WriteSingleRegisterRequestRTU) Bytes

func (r WriteSingleRegisterRequestRTU) Bytes() []byte

Bytes returns WriteSingleRegisterRequestRTU packet as bytes form

func (WriteSingleRegisterRequestRTU) ExpectedResponseLength

func (r WriteSingleRegisterRequestRTU) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type WriteSingleRegisterRequestTCP

type WriteSingleRegisterRequestTCP struct {
	MBAPHeader
	WriteSingleRegisterRequest
}

WriteSingleRegisterRequestTCP is TCP Request for Write Single Register (FC=06)

Example packet: 0x00 0x01 0x00 0x00 0x00 0x06 0x11 0x06 0x00 0x6B 0x01 0x01 0x00 0x01 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x06 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x11 - unit id (6) 0x06 - function code (7) 0x00 0x6B - start address (8,9) 0x01 0x01 - register data (10,11)

func NewWriteSingleRegisterRequestTCP

func NewWriteSingleRegisterRequestTCP(unitID uint8, address uint16, data []byte) (*WriteSingleRegisterRequestTCP, error)

NewWriteSingleRegisterRequestTCP creates new instance of Write Single Register TCP request NB: byte slice for `data` must be in BigEndian byte order for server to interpret them correctly

func ParseWriteSingleRegisterRequestTCP

func ParseWriteSingleRegisterRequestTCP(data []byte) (*WriteSingleRegisterRequestTCP, error)

ParseWriteSingleRegisterRequestTCP parses given bytes into WriteSingleRegisterRequestTCP

func (WriteSingleRegisterRequestTCP) Bytes

func (r WriteSingleRegisterRequestTCP) Bytes() []byte

Bytes returns WriteSingleRegisterRequestTCP packet as bytes form

func (WriteSingleRegisterRequestTCP) ExpectedResponseLength

func (r WriteSingleRegisterRequestTCP) ExpectedResponseLength() int

ExpectedResponseLength returns length of bytes that valid response to this request would be

type WriteSingleRegisterResponse

type WriteSingleRegisterResponse struct {
	UnitID  uint8
	Address uint16
	Data    [2]byte
}

WriteSingleRegisterResponse is Response for Write Single Register (FC=06)

func (WriteSingleRegisterResponse) AsRegisters

func (r WriteSingleRegisterResponse) AsRegisters(address uint16) (*Registers, error)

AsRegisters returns response data as Register to more convenient access

func (WriteSingleRegisterResponse) Bytes

func (r WriteSingleRegisterResponse) Bytes() []byte

Bytes returns WriteSingleRegisterResponse packet as bytes form

func (WriteSingleRegisterResponse) FunctionCode

func (r WriteSingleRegisterResponse) FunctionCode() uint8

FunctionCode returns function code of this request

type WriteSingleRegisterResponseRTU

type WriteSingleRegisterResponseRTU struct {
	WriteSingleRegisterResponse
}

WriteSingleRegisterResponseRTU is RTU Response for Write Single Register (FC=06)

Example packet: 0x11 0x06 0x00 0x6B 0x01 0x01 0x3a 0xd6 0x11 - unit id (0) 0x06 - function code (1) 0x00 0x6B - start address (2,3) 0x01 0x01 - register data (4,5) 0x3a 0xd6 - CRC16 (6,7)

func ParseWriteSingleRegisterResponseRTU

func ParseWriteSingleRegisterResponseRTU(data []byte) (*WriteSingleRegisterResponseRTU, error)

ParseWriteSingleRegisterResponseRTU parses given bytes into WriteSingleRegisterResponseRTU

func (WriteSingleRegisterResponseRTU) Bytes

Bytes returns WriteSingleRegisterResponseRTU packet as bytes form

type WriteSingleRegisterResponseTCP

type WriteSingleRegisterResponseTCP struct {
	MBAPHeader
	WriteSingleRegisterResponse
}

WriteSingleRegisterResponseTCP is TCP Response for Write Single Register (FC=06)

Example packet: 0x81 0x80 0x00 0x00 0x00 0x06 0x03 0x06 0x00 0x02 0xFF 0x00 0x81 0x80 - transaction id (0,1) 0x00 0x00 - protocol id (2,3) 0x00 0x06 - number of bytes in the message (PDU = ProtocolDataUnit) to follow (4,5) 0x03 - unit id (6) 0x06 - function code (7) 0x00 0x02 - start address (8,9) 0xFF 0x00 - register data (10,11)

func ParseWriteSingleRegisterResponseTCP

func ParseWriteSingleRegisterResponseTCP(data []byte) (*WriteSingleRegisterResponseTCP, error)

ParseWriteSingleRegisterResponseTCP parses given bytes into WriteSingleRegisterResponseTCP

func (WriteSingleRegisterResponseTCP) Bytes

Bytes returns WriteSingleRegisterResponseTCP packet as bytes form

Jump to

Keyboard shortcuts

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