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>
qmake: QT += serialbus
Since: Qt 5.8
Inherited By:

QModbusRequest and QModbusResponse

Public Types

enum ExceptionCode { IllegalFunction, IllegalDataAddress, IllegalDataValue, ServerDeviceFailure, ..., ExtendedException }
enum FunctionCode { Invalid, ReadCoils, ReadDiscreteInputs, ReadHoldingRegisters, ..., 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)
QDebug operator<<(QDebug debug, const QModbusPdu &pdu)
QDataStream &operator<<(QDataStream &stream, const QModbusPdu &pdu)

Detailed Description

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

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.

ConstantValueDescription
QModbusPdu::IllegalFunction0x01Function code is not supported by device.
QModbusPdu::IllegalDataAddress0x02The received data address in the query is not an allowable address for the Modbus server.
QModbusPdu::IllegalDataValue0x03The contained value in the request data field is not an allowable value for the Modbus server.
QModbusPdu::ServerDeviceFailure0x04An irrecoverable error occurred while the server was attempting to perform the requested action.
QModbusPdu::Acknowledge0x05Specialized use in conjunction with programming commands.
QModbusPdu::ServerDeviceBusy0x06The server is engaged in processing a long duration program command.
QModbusPdu::NegativeAcknowledge0x07The 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::MemoryParityError0x08Indicates 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::GatewayPathUnavailable0x0AIndicates that the gateway was unable to allocate an internal communication path from the input port to the output port for processing the request.
QModbusPdu::GatewayTargetDeviceFailedToRespond0x0BIndicates 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::ExtendedException0xFFThis 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.

ConstantValueDescription
QModbusPdu::Invalid0x00Set by the default constructor, do not use.
QModbusPdu::ReadCoils0x01Requests the status of one or more coils from a device.
QModbusPdu::ReadDiscreteInputs0x02Requests the status of one or more input registers from a device.
QModbusPdu::ReadHoldingRegisters0x03Requests the status of one or more holding register values from a device.
QModbusPdu::ReadInputRegisters0x04Requests the status of one or more input register values from a device.
QModbusPdu::WriteSingleCoil0x05Requests to write a single coil on a device.
QModbusPdu::WriteSingleRegister0x06Requests to write a single holding register on a device.
QModbusPdu::ReadExceptionStatus0x07Requests the status of the eight Exception Status outputs on a device.
QModbusPdu::Diagnostics0x08Used to provide a series of tests for checking the client server communication system, or checking internal
QModbusPdu::GetCommEventCounter0x0BRequests a status word and an event count from the device's communication event counter.
QModbusPdu::GetCommEventLog0x0CRequests a status word, event count, message count, and a field of event bytes from a device.
QModbusPdu::WriteMultipleCoils0x0FRequests to write one or more coils on a device.
QModbusPdu::WriteMultipleRegisters0x10Requests to write one or more holding registers on a device.
QModbusPdu::ReportServerId0x11Requests the description of the type, the current status, and other information specific to a device.
QModbusPdu::ReadFileRecord0x14Requests a file record read.
QModbusPdu::WriteFileRecord0x15Requests a file record write.
QModbusPdu::MaskWriteRegister0x16Requests 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::ReadWriteMultipleRegisters0x17Requests the status of one or more holding register and at the same time to write one or more holding registers on a device.
QModbusPdu::ReadFifoQueue0x18Requests to read the contents of a First-In-First-Out (FIFO) queue of register in a remote device.
QModbusPdu::EncapsulatedInterfaceTransport0x2BPlease refer to Annex A of the Modbus specification.
QModbusPdu::UndefinedFunctionCode0x100Do not use.

Member Function Documentation

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.

[protected] QModbusPdu::QModbusPdu(const QModbusPdu &other)

Constructs a QModbusPdu that is a copy of other.

[virtual] 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.

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.

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.

[protected] 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

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

Writes the Modbus pdu to the debug stream.

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

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

© 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.