QModbusResponse Class | Qt Serial Bus 5.13.2

Qt 5.13 | Qt Serial Bus | C++ Classes | QModbusResponse

Qt 5.13.2 Reference Documentation

# QModbusResponse Class

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

Header: | #include <QModbusResponse>
qmake: | QT += serialbus
Since: | Qt 5.8
Inherits: | QModbusPdu
Inherited By: | QModbusExceptionResponse

This class was introduced in Qt 5.8.

- List of all members, including inherited members

## Public Types

typedef | CalcFuncPtr

## Public Functions

| QModbusResponse(QModbusPdu::FunctionCode code, Args... data)
| QModbusResponse(QModbusPdu::FunctionCode code, const int &data = ...)
| QModbusResponse(const QModbusPdu &pdu)
| QModbusResponse()

## Static Public Members

Q_DECL_IMPORT | calculateDataSize(const QModbusResponse &response)
Q_DECL_IMPORT | minimumDataSize(const QModbusResponse &response)
Q_DECL_IMPORT | registerDataSizeCalculator(QModbusPdu::FunctionCode fc, QModbusResponse::CalcFuncPtr calculator)

## Detailed Description

A typical Modbus response can looks like this:

      QModbusResponse response(QModbusResponse::ReadCoils, QByteArray::fromHex("02cd01"));

Note: When using the constructor taking the `QByteArray`, please make sure to convert the containing data to big-endian byte order before creating the request.

The same response can be created like this, if the values are known at compile time:

      quint8 payloadInBytes = 2, outputHigh = 0xcd, outputLow = 0x01;
      QModbusResponse response(QModbusResponse::ReadCoils, payloadInBytes, outputHigh, outputLow);

## Member Type Documentation

### typedef QModbusResponse::CalcFuncPtr

Typedef for a pointer to a custom calculator function with the same signature as QModbusResponse::calculateDataSize.

## Member Function Documentation

### QModbusResponse::QModbusResponse(QModbusPdu::FunctionCode code, Args... data)

Constructs a QModbusResponse with function code set to code and payload set to data. The data is converted and stored in big-endian byte order.

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.

### QModbusResponse::QModbusResponse(QModbusPdu::FunctionCode code, const int &data = ...)

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

### QModbusResponse::QModbusResponse(const QModbusPdu &pdu)

Constructs a copy of pdu.

### QModbusResponse::QModbusResponse()

Constructs an invalid QModbusResponse.

### `[static]` Q_DECL_IMPORT QModbusResponse::calculateDataSize(const QModbusResponse &response)

Calculates the expected data size for response, based on the response's function code and data. Returns the full size of the response's data part; `-1` if the size could not be properly calculated.

See also minimumDataSize and registerDataSizeCalculator.

### `[static]` Q_DECL_IMPORT QModbusResponse::minimumDataSize(const QModbusResponse &response)

Returns the expected minimum data size for response based on the response's function code; `-1` if the function code is not known.

### `[static]` Q_DECL_IMPORT QModbusResponse::registerDataSizeCalculator(QModbusPdu::FunctionCode fc, QModbusResponse::CalcFuncPtr calculator)

This function registers a user-defined implementation to calculate the response data size for function code fc. It can be used to extend or override the implementation inside QModbusResponse::calculateDataSize().

The `CalcFuncPtr` is a typedef for a pointer to a custom calculator function with the following signature:

      int myCalculateDataSize(const QModbusResponse &pdu);