QModbusPdu Class | Qt Serial Bus | Qt 6.12.0
Description: QModbusPdu is a abstract container class containing the function code and payload that is stored inside a Modbus ADU.
Keywords: class

- Qt 6.12
- Qt Serial Bus
- C++ Classes
- QModbusPdu
- Qt 6.12.0 Reference Documentation

# QModbusPdu Class

QModbusPdu is a abstract container class containing the function code and payload that is stored inside a Modbus ADU. More...

Header: | `#include <QModbusPdu>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS SerialBus)` `target_link_libraries(mytarget PRIVATE Qt6::SerialBus)`
qmake: | `QT += serialbus`
Inherited By: | QModbusRequest and QModbusResponse

- List of all members, including inherited members

## Public Types

enum | ExceptionCode { IllegalFunction, IllegalDataAddress, IllegalDataValue, ServerDeviceFailure, Acknowledge, …, ExtendedException }
enum | FunctionCode { Invalid, ReadCoils, ReadDiscreteInputs, ReadHoldingRegisters, ReadInputRegisters, …, UndefinedFunctionCode }

## Public Functions

| QModbusPdu()
virtual | ~QModbusPdu()
QByteArray | data() const
qint16 | dataSize() const
void | decodeData(Args &&... data) const
void | encodeData(Args... data)
QModbusPdu::ExceptionCode | exceptionCode() const
QModbusPdu::FunctionCode | functionCode() const
bool | isException() const
bool | isValid() const
void | setData(const QByteArray &data)
virtual void | setFunctionCode(QModbusPdu::FunctionCode code)
qint16 | size() const

## Static Public Members

const quint8 | ExceptionByte

## Protected Functions

| QModbusPdu(QModbusPdu::FunctionCode code, const QByteArray &data)
| QModbusPdu(const QModbusPdu &other)
QModbusPdu & | operator=(const QModbusPdu &other)

## Related Non-Members

QDataStream & | operator<<(QDataStream &stream, const QModbusPdu &pdu)
QDebug | operator<<(QDebug debug, const QModbusPdu &pdu)

## Detailed Description

The class provides access to the raw Modbus protocol packets as defined by the Modbus Application Protocol Specification 1.1b.

## Member Type Documentation

### enum QModbusPdu::ExceptionCode

This enum describes all the possible error conditions as defined by Modbus Exception Codes. They are set by the server after checking the appropriate error conditions in the reply to a request and must be decoded by the client to operate on the exception code.

Constant | Value | Description
--- | --- | ---
`QModbusPdu::IllegalFunction` | `0x01` | Function code is not supported by device.
`QModbusPdu::IllegalDataAddress` | `0x02` | The received data address in the query is not an allowable address for the Modbus server.
`QModbusPdu::IllegalDataValue` | `0x03` | The contained value in the request data field is not an allowable value for the Modbus server.
`QModbusPdu::ServerDeviceFailure` | `0x04` | An irrecoverable error occurred while the server was attempting to perform the requested action.
`QModbusPdu::Acknowledge` | `0x05` | Specialized use in conjunction with programming commands.
`QModbusPdu::ServerDeviceBusy` | `0x06` | The server is engaged in processing a long duration program command.
`QModbusPdu::NegativeAcknowledge` | `0x07` | The server cannot perform the program function received in the query. This code is returned for an unsuccessful programming request. The client should request diagnostic or error information from the server.
`QModbusPdu::MemoryParityError` | `0x08` | Indicates that the extended file area failed to pass a consistency check. Used in conjunction with function codes 20 and 21. The exception code does not refer to any parity settings of the transmission line but only to the servers' internal memory of file records.
`QModbusPdu::GatewayPathUnavailable` | `0x0A` | Indicates that the gateway was unable to allocate an internal communication path from the input port to the output port for processing the request.
`QModbusPdu::GatewayTargetDeviceFailedToRespond` | `0x0B` | Indicates that no response was obtained from the target device behind a gateway. Usually this means the target device is not online on the network.
`QModbusPdu::ExtendedException` | `0xFF` | This is an extended exception as per Modbus specification. Generally this code is used to describe an exception that is otherwise further described.

### enum QModbusPdu::FunctionCode

Defines the function code and the implicit type of action required by the server. Not all Modbus devices can handle the same set of function codes.

Constant | Value | Description
--- | --- | ---
`QModbusPdu::Invalid` | `0x00` | Set by the default constructor, do not use.
`QModbusPdu::ReadCoils` | `0x01` | Requests the status of one or more coils from a device.
`QModbusPdu::ReadDiscreteInputs` | `0x02` | Requests the status of one or more input registers from a device.
`QModbusPdu::ReadHoldingRegisters` | `0x03` | Requests the status of one or more holding register values from a device.
`QModbusPdu::ReadInputRegisters` | `0x04` | Requests the status of one or more input register values from a device.
`QModbusPdu::WriteSingleCoil` | `0x05` | Requests to write a single coil on a device.
`QModbusPdu::WriteSingleRegister` | `0x06` | Requests to write a single holding register on a device.
`QModbusPdu::ReadExceptionStatus` | `0x07` | Requests the status of the eight Exception Status outputs on a device.
`QModbusPdu::Diagnostics` | `0x08` | Used to provide a series of tests for checking the client server communication system, or checking internal
`QModbusPdu::GetCommEventCounter` | `0x0B` | Requests a status word and an event count from the device's communication event counter.
`QModbusPdu::GetCommEventLog` | `0x0C` | Requests a status word, event count, message count, and a field of event bytes from a device.
`QModbusPdu::WriteMultipleCoils` | `0x0F` | Requests to write one or more coils on a device.
`QModbusPdu::WriteMultipleRegisters` | `0x10` | Requests to write one or more holding registers on a device.
`QModbusPdu::ReportServerId` | `0x11` | Requests the description of the type, the current status, and other information specific to a device.
`QModbusPdu::ReadFileRecord` | `0x14` | Requests a file record read.
`QModbusPdu::WriteFileRecord` | `0x15` | Requests a file record write.
`QModbusPdu::MaskWriteRegister` | `0x16` | Requests to modify the contents of a specified holding register using a combination of an AND or OR mask, and the register's current contents.
`QModbusPdu::ReadWriteMultipleRegisters` | `0x17` | Requests the status of one or more holding register and at the same time to write one or more holding registers on a device.
`QModbusPdu::ReadFifoQueue` | `0x18` | Requests to read the contents of a First-In-First-Out (FIFO) queue of register in a remote device.
`QModbusPdu::EncapsulatedInterfaceTransport` | `0x2B` | Please refer to Annex A of the Modbus specification.
`QModbusPdu::UndefinedFunctionCode` | `0x100` | Do not use.

## Member Function Documentation

### `[constexpr noexcept default]` QModbusPdu::QModbusPdu()

Constructs an invalid QModbusPdu.

### `[protected]` QModbusPdu::QModbusPdu(QModbusPdu::FunctionCode code, const QByteArray &data)

Constructs a QModbusPdu with function code set to code and payload set to data. The data is expected to be stored in big-endian byte order already.

### `[noexcept protected default]` QModbusPdu::QModbusPdu(const QModbusPdu &other)

Constructs a QModbusPdu that is a copy of other.

### `[virtual noexcept]` QModbusPdu::~QModbusPdu()

Destroys a QModbusPdu.

### QByteArray QModbusPdu::data() const

Returns the PDU's payload, excluding the function code. The payload is stored in big-endian byte order.

See also setData().

### qint16 QModbusPdu::dataSize() const

Returns the PDU's data size, excluding the function code.

### template <typename... Args> void QModbusPdu::decodeData(Args &&... data) const

Converts the payload into host endianness and reads it into data. Data can be a variable length argument list.

     QModbusResponsePdu response(QModbusPdu::ReportServerId);
     response.encodeData(quint8(0x02), quint8(0x01), quint8(0xff));
     quint8 count, id, run;
     response.decodeData(&count, &id, &run);

Note: Usage is limited `quint8` and `quint16` only. This is because `QDataStream` stream operators will not only append raw data, but also e.g. size, count, etc. for complex types.

### template <typename... Args> void QModbusPdu::encodeData(Args... data)

Sets the payload to data. The data is converted and stored in big-endian byte order.

     QModbusRequestPdu request(QModbusPdu::ReadCoils);
     // starting address and quantity of coils
     request.encodeData(quint16(0x0c), quint16(0x0a));

Note: Usage is limited `quint8` and `quint16` only. This is because `QDataStream` stream operators will not only append raw data, but also e.g. size, count, etc. for complex types.

### QModbusPdu::ExceptionCode QModbusPdu::exceptionCode() const

Returns the response's exception code.

### QModbusPdu::FunctionCode QModbusPdu::functionCode() const

Returns the PDU's function code.

See also setFunctionCode().

### bool QModbusPdu::isException() const

Returns true if the PDU contains an exception code; otherwise false.

### bool QModbusPdu::isValid() const

Returns true if the PDU is valid; otherwise false.

A PDU is considered valid if the message code is in the range of 1 to 255 decimal and the PDU's compound size (function code + data) does not exceed 253 bytes. A default constructed PDU is invalid.

### void QModbusPdu::setData(const QByteArray &data)

Sets the PDU's function payload to data. The data is expected to be stored in big-endian byte order already.

See also data().

### `[virtual]` void QModbusPdu::setFunctionCode(QModbusPdu::FunctionCode code)

Sets the PDU's function code to code.

See also functionCode().

### qint16 QModbusPdu::size() const

Returns the PDU's full size, including function code and data size.

### `[noexcept protected default]` QModbusPdu &QModbusPdu::operator=(const QModbusPdu &other)

Makes a copy of the other and assigns it to this QModbusPdu object.

## Member Variable Documentation

### const quint8 QModbusPdu::ExceptionByte

The variable is initialized to 0x80.

Exceptions are reported in a defined packet format. A function code is returned to the requesting client equal to the original function code, except with its most significant bit set. This is equivalent to adding 0x80 to the value of the original function code.

This field may be used to mask the exception bit in the function field of a raw Modbus packet.

## Related Non-Members

### QDataStream &operator<<(QDataStream &stream, const QModbusPdu &pdu)

Writes a pdu to the stream and returns a reference to the stream.

### QDebug operator<<(QDebug debug, const QModbusPdu &pdu)

Writes the Modbus pdu to the debug stream.