- Qt 5.15
- Qt Serial Bus
- C++ Classes
- QModbusRequest
- Qt 5.15.16 Reference Documentation

# QModbusRequest Class

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

Header: | #include <QModbusRequest>
qmake: | QT += serialbus
Since: | Qt 5.8

This class was introduced in Qt 5.8.

- List of all members, including inherited members

## Public Functions

| QModbusRequest(FunctionCode code, Args... data)
| QModbusRequest(FunctionCode code, const int &data = QByteArray())
| QModbusRequest(const QModbusPdu &pdu)
| QModbusRequest()

## Static Public Members

int | calculateDataSize(const QModbusRequest &request)
int | minimumDataSize(const QModbusRequest &request)
int | registerDataSizeCalculator(FunctionCode fc, int calculator)

## Detailed Description

A Modbus request usually consists of a single byte describing the `FunctionCode` and N bytes of payload

A typical Modbus request can looks like this:

     QModbusRequest request(QModbusRequest::WriteMultipleCoils,
         QByteArray::fromHex("0013000a02cd01"));

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 request can be created like this, if the values are known at compile time:

     quint16 startAddress = 19, numberOfCoils = 10;
     quint8 payloadInBytes = 2, outputHigh = 0xcd, outputLow = 0x01;
     QModbusRequest request(QModbusRequest::WriteMultipleCoils, startAddress, numberOfCoils,
         payloadInBytes, outputHigh, outputLow);

## Member Function Documentation

### template <typename Args> QModbusRequest::QModbusRequest(FunctionCode code, Args... data)

Constructs a QModbusRequest 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.

### QModbusRequest::QModbusRequest(FunctionCode code, const int &data = QByteArray())

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.

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

Constructs a copy of pdu.

### QModbusRequest::QModbusRequest()

Constructs an invalid QModbusRequest.

### `[static]` int QModbusRequest::calculateDataSize(const QModbusRequest &request)

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

See also minimumDataSize and registerDataSizeCalculator.

### `[static]` int QModbusRequest::minimumDataSize(const QModbusRequest &request)

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

### `[static]` int QModbusRequest::registerDataSizeCalculator(FunctionCode fc, int calculator)

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

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

     int myCalculateDataSize(const QModbusRequest &pdu);