- Qt 6.7
- Qt Serial Bus
- C++ Classes
- QModbusDataUnit
- Qt 6.7.0 Reference Documentation

# QModbusDataUnit Class

QModbusDataUnit is a container class representing single bit and `16` bit word entries in the Modbus register. More...

Header: | #include <QModbusDataUnit>
CMake: | find_package(Qt6 REQUIRED COMPONENTS SerialBus) target_link_libraries(mytarget PRIVATE Qt6::SerialBus)
qmake: | QT += serialbus

- List of all members, including inherited members

## Public Types

enum | RegisterType { Invalid, DiscreteInputs, Coils, InputRegisters, HoldingRegisters }

## Public Functions

| QModbusDataUnit()
| QModbusDataUnit(QModbusDataUnit::RegisterType type)
| QModbusDataUnit(QModbusDataUnit::RegisterType type, int address, quint16 size)
| QModbusDataUnit(QModbusDataUnit::RegisterType type, int address, const QList<quint16> &data)
bool | isValid() const
QModbusDataUnit::RegisterType | registerType() const
void | setRegisterType(QModbusDataUnit::RegisterType type)
void | setStartAddress(int address)
void | setValue(qsizetype index, quint16 value)
void | setValueCount(qsizetype newCount)
void | setValues(const QList<quint16> &values)
int | startAddress() const
quint16 | value(qsizetype index) const
qsizetype | valueCount() const
QList<quint16> | values() const

## Related Non-Members

| QModbusDataUnitMap

## Detailed Description

QModbusDataUnit can be used for read and write operations. The entries are addressed via startAddress() and the valueCount() number of contiguous entries. registerType() determines which register is used for the operations. Note that some registers are read-only registers.

The actual values() are either single bit or `16` bit based. QModbusDataUnit::DiscreteInputs and QModbusDataUnit::Coils only accept single bits. Therefore `0` is interpreted as `0` and anything else `1`.

## Member Type Documentation

### enum QModbusDataUnit::RegisterType

This enum describes all supported register types.

Constant | Value | Description
--- | --- | ---
`QModbusDataUnit::Invalid` | `0` | Set by the default constructor, do not use.
`QModbusDataUnit::DiscreteInputs` | `1` | This type of data can be provided by an I/O system.
`QModbusDataUnit::Coils` | `2` | This type of data can be alterable by an application program.
`QModbusDataUnit::InputRegisters` | `3` | This type of data can be provided by an I/O system.
`QModbusDataUnit::HoldingRegisters` | `4` | This type of data can be alterable by an application program.

## Member Function Documentation

### `[constexpr noexcept]` QModbusDataUnit::QModbusDataUnit()

Constructs an empty, invalid QModbusDataUnit. Start address is set to `-1` and the registerType is set to QModbusDataUnit::Invalid.

### `[explicit constexpr noexcept]` QModbusDataUnit::QModbusDataUnit(QModbusDataUnit::RegisterType type)

Constructs a unit of data for register type. Start address is set to `0`, data range and data values are empty.

### QModbusDataUnit::QModbusDataUnit(QModbusDataUnit::RegisterType type, int address, quint16 size)

Constructs a unit of data for registertype. Start address of the data is set to address and the size of the unit to size. The entries of values() are initialized with `0`.

### QModbusDataUnit::QModbusDataUnit(QModbusDataUnit::RegisterType type, int address, const QList<quint16> &data)

Constructs a unit of data for registertype. Start address of the data is set to address and the unit's values to data. The value count is implied by the data size.

### bool QModbusDataUnit::isValid() const

Returns `true` if the `QModbusDataUnit` is valid; otherwise `false`. A `QModbusDataUnit` is considered valid if the registerType() is not QModbusDataUnit::Invalid and the startAddress() is greater than or equal to `0`.

### QModbusDataUnit::RegisterType QModbusDataUnit::registerType() const

Returns the type of the register.

See also setRegisterType() and QModbusDataUnit::RegisterType.

### void QModbusDataUnit::setRegisterType(QModbusDataUnit::RegisterType type)

Sets the register type.

See also registerType() and QModbusDataUnit::RegisterType.

### void QModbusDataUnit::setStartAddress(int address)

Sets the start address of the data unit.

See also startAddress().

### void QModbusDataUnit::setValue(qsizetype index, quint16 value)

Sets the register at position index to value.

See also value().

### void QModbusDataUnit::setValueCount(qsizetype newCount)

Sets the size of the requested register's data block to newCount.

This may be different from values() size as this function is used to indicated the size of a data request. Only once the data request has been processed valueCount() is equal to the size of values().

See also valueCount().

### void QModbusDataUnit::setValues(const QList<quint16> &values)

Sets the values of the data unit. QModbusDataUnit::DiscreteInputs and QModbusDataUnit::Coils tables only accept single bit value, so `0` is interpreted as `0` and anything else as `1`.

See also values().

### int QModbusDataUnit::startAddress() const

Returns the start address of data unit in the register.

See also setStartAddress().

### quint16 QModbusDataUnit::value(qsizetype index) const

Return the value at position index.

See also setValue().

### qsizetype QModbusDataUnit::valueCount() const

Returns the size of the requested register's data block or the size of data read from the device.

This function may not always return a count that equals values() size. Since this class is used to request data from the remote data register, the valueCount() can be used to indicate the size of the requested register's data block. Once the request has been processed, the valueCount() is equal to the size of values().

See also setValueCount().

### QList<quint16> QModbusDataUnit::values() const

Returns the data in the data unit. QModbusDataUnit::DiscreteInputs and QModbusDataUnit::Coils tables only accept single bit value, so `0` is interpreted as `0` and anything else as `1`.

See also setValues().

## Related Non-Members

### QModbusDataUnitMap

Synonym for QMap<QModbusDataUnit::RegisterType, QModbusDataUnit>.