This is a library for managing Modbus RTU and TCP application data units (ADUs). It is intended to be used with other libraries, but it can be used on its own.
Creating an instance of ModbusADU
creates a structure with essentially the following form:
Field | Length | Description |
---|---|---|
Transaction Identifier | 2 Bytes | Identification of a Modbus request |
Protocol Identifier | 2 Bytes | 0 = Modbus protocol |
Length | 2 Bytes | Number of following bytes, excluding error check |
Unit Itentifier | 1 Byte | Identification of a remote slave/server device |
Function Code | 1 Byte | Function of message: read, write, other |
Data | 1 - 252 Bytes | Contents depend on function code |
Error Check | 2 Bytes | Cyclic redundancy check (CRC) code |
rtu[]
Allows reading from and writing to a
ModbusADU
object as a byte (uint8_t
) array, with index0
corresponding to the unit identifier.
adu.rtu[index]
adu
: aModbusADU
object.
index
: the location to read/write from. Allowed data types:int
.
tcp[]
Allows reading from and writing to a
ModbusADU
object as a byte (uint8_t
) array, with index0
corresponding to the first byte of the transaction identifier.
adu.tcp[index]
adu
: aModbusADU
object.
index
: the location to read/write from, starting from 0. Allowed data types:int
.
pdu[]
Allows reading from and writing to a
ModbusADU
object as a byte (uint8_t
) array, with index0
corresponding to the function code.
adu.pdu[index]
adu
: aModbusADU
object.
index
: the location to read/write from. Allowed data types:int
.
data[]
Allows reading from and writing to a
ModbusADU
object as a byte (uint8_t
) array, with index0
corresponding to the first byte of the data section.
adu.data[index]
adu
: aModbusADU
object.
index
: the location to read/write from. Allowed data types:int
.
setTransactionId()
Sets the transaction identification number of the
ModbusADU
object.
adu.setTransactionId(transactionId)
adu
: aModbusADU
object.
transactionId
: a transaction identifier number for Modbus TCP. Allowed data types:uint16_t
.
setProtocolId()
Sets the protocol identification number of the
ModbusADU
object. For all valid Modbus TCP transactions, this should be set to0
.
adu.setProtocolId(protocolId)
adu
: aModbusADU
object.
protocolId
: a protocol identifier number for Modbus TCP. Allowed data types:uint16_t
.
setLength()
Sets the value in the length field. This value should be the number of bytes in the ADU following the length field, but exluding the error check field. This includes the unit id, function code, and data fields.
adu.setLength(length)
adu
: aModbusADU
object.length
: the value to place in the length field. Allowed data types:uint16_t
.Note the length field can also be manipulated with
setRtuLen()
,setTcpLen()
,setPduLen()
, andsetDataLen()
.
setUnitId()
Sets the unit identification number of the
ModbusADU
object.
adu.setUnitId(unitId)
adu
: aModbusADU
object.
unitId
: the unit identifier number. Allowed data types:uint8_t
.
setFunctionCode()
Sets the function code of the
ModbusADU
object.
adu.setFunctionCode(functionCode)
adu
: aModbusADU
object.
functionCode
: a Modbus function code. Allowed data types:uint8_t
.
setDataRegister()
Sets a 16-bit register in the data portion the
ModbusADU
object.
adu.setDataRegister(index, value)
adu
: aModbusADU
object.index
: the data byte at which to start writing the 16-bit value. Allowed data types:uint8_t
.value
: the 16-bit value to write to the data field of the ADU. Allowed data types:uint16_t
.ModbusADU adu; adu.data[0] = 6; // sets data byte 0 adu.setDataRegister(1, 38229); // sets data bytes 1 and 2 adu.setDataRegister(3, 65535); // sets data bytes 3 and 4 adu.setDataRegister(5, 52428); // sets data bytes 5 and 6Note that the index is per byte, not per 16-bit value. This means that if you are writing multiple registers in a row, you will need to skip an index value between them.
setRtuLen()
Sets the length field assuming that the submitted value is the length of the RTU frame. The RTU frame includes the unit id, function code, data, and error check fields.
adu.setRtuLen(length)
adu
: aModbusADU
object.
length
: the length in bytes of a RTU message frame. Allowed data types:uint16_t
.
setTcpLen()
Sets the length field assuming that the submitted value is the length of the TCP frame. The TCP frame includes the transaction id, protocol id, length, unit id, function code, and data fields.
adu.setTcpLen(length)
adu
: aModbusADU
object.
length
: the length in bytes of the TCP frame. Allowed data types:uint16_t
.
setPduLen()
Sets the length field assuming that the submitted value is the length of the PDU (Protocol Data Unit). The PDU includes the function code, and data fields.
adu.setPduLen(length)
adu
: aModbusADU
object.
length
: the length in bytes of the PDU. Allowed data types:uint16_t
.
setDataLen()
Sets the length field assuming that the submitted value is the length of the data field.
adu.setDataLen(length)
adu
: aModbusADU
object.
length
: the length in bytes of the data field. Allowed data types:uint16_t
.
getTransactionId()
Gets the transaction identification number of the
ModbusADU
object.
adu.getTransactionId()
adu
: aModbusADU
object.Transaction identifier. Data type:
uint16_t
.
getProtocolId()
Gets the protocol identification number of the
ModbusADU
object.
adu.getProtocolId()
adu
: aModbusADU
object.Protocol identifier. Data type:
uint16_t
.
getLength()
Gets the value in the length field. This value should be the number of bytes in the ADU following the length field, but exluding the error check field. This includes the unit id, function code, and data fields.
adu.getLength()
adu
: aModbusADU
object.The value in the length field. Data type:
uint16_t
.
getUnitId()
Gets the unit identification number of the
ModbusADU
object.
adu.getUnitId()
adu
: aModbusADU
object.The unit identifier number. Data type:
uint8_t
.
getFunctionCode()
Gets the function code of the
ModbusADU
object.
adu.getFunctionCode()
adu
: aModbusADU
object.The function code. Data type:
uint8_t
.
getDataRegister()
Gets a 16-bit register from the data portion the
ModbusADU
object.
adu.getDataRegister(index)
adu
: aModbusADU
object.index
: the data byte at which to start reading the 16-bit value. Allowed data types:uint8_t
.The 16-bit value read from the data field of the ADU. Data type:
uint16_t
.Note that the index is per byte, not per 16-bit value.
getRtuLen()
Gets the length of the RTU frame based on the length field. The RTU frame includes the unit id, function code, data, and error check fields.
adu.getRtuLen()
adu
: aModbusADU
object.The length in bytes of the RTU frame. Data type:
uint16_t
.
getTcpLen()
Gets the length of the TCP frame based on the length field. The TCP frame includes the transaction id, protocol id, length, unit id, function code, and data fields.
adu.getTcpLen()
adu
: aModbusADU
object.The length in bytes of the TCP frame. Data type:
uint16_t
.
getPduLen()
Gets the length of the PDU (Protocol Data Unit) based on the length field. The PDU includes the function code, and data fields.
adu.getPduLen()
adu
: aModbusADU
object.The length in bytes of the PDU. Data type:
uint16_t
.
getDataLen()
Gets the length of the data field based on the length field.
adu.getDataLen()
adu
: aModbusADU
object.The length in bytes of the data field. Allowed data types:
uint16_t
.
updateCrc()
Updates the error check field based on the contents of the ADU.
adu.updateCrc()
adu
: aModbusADU
object.
crcGood()
Checks if the error check is correct for the contents of the ADU.
adu.crcGood()
adu
: aModbusADU
object.
true
if the error check is correct for the contents of the ADU.
false
if the error check is incorrect for the contents of the ADU.
prepareExceptionResponse()
Prepares an exception response in the ADU.
adu.prepareExceptionResponse(exceptionCode)
adu
: aModbusADU
object.
exceptionCode
: sets the exception code to be used in the exception response. Typical values are:
1
: Illegal Function2
: Illegal data address3
: Illegal data value4
: Server device failure