QModbusDevice Class▲
-
Header: QModbusDevice
-
Since: Qt 5.8
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS SerialBus)
target_link_libraries(mytarget PRIVATE Qt6::SerialBus)
-
qmake: QT += serialbus
-
Inherits: QObject
-
Inherited By: QModbusClient and QModbusServer
Detailed Description▲
Member Type Documentation▲
enum QModbusDevice::ConnectionParameter▲
This enum describes the possible values that can be set for a Modbus device connection.
The general purpose value (and the associated types) are:
Constant |
Value |
Description |
---|---|---|
QModbusDevice::SerialPortNameParameter |
0 |
This parameter holds the serial port used for device communication, e.g. COM1. QString |
QModbusDevice::SerialParityParameter |
1 |
This parameter holds the parity checking mode. QSerialPort::Parity |
QModbusDevice::SerialBaudRateParameter |
2 |
This parameter holds the data baud rate for the communication. QSerialPort::BaudRate |
QModbusDevice::SerialDataBitsParameter |
3 |
This parameter holds the data bits in a frame. QSerialPort::DataBits |
QModbusDevice::SerialStopBitsParameter |
4 |
This parameter holds the number of stop bits in a frame. QSerialPort::StopBits |
QModbusDevice::NetworkPortParameter |
5 |
This parameter holds the network port. int |
QModbusDevice::NetworkAddressParameter |
6 |
This parameter holds the host address for network communication. QString |
enum QModbusDevice::Error▲
This enum describes all the possible error conditions.
Constant |
Value |
Description |
---|---|---|
QModbusDevice::NoError |
0 |
No errors have occurred. |
QModbusDevice::ReadError |
1 |
An error occurred during a read operation. |
QModbusDevice::WriteError |
2 |
An error occurred during a write operation. |
QModbusDevice::ConnectionError |
3 |
An error occurred when attempting to open the backend. |
QModbusDevice::ConfigurationError |
4 |
An error occurred when attempting to set a configuration parameter. |
QModbusDevice::TimeoutError |
5 |
A timeout occurred during I/O. An I/O operation did not finish within a given time frame. |
QModbusDevice::ProtocolError |
6 |
A Modbus specific protocol error occurred. |
QModbusDevice::ReplyAbortedError |
7 |
The reply was aborted due to a disconnection of the device. |
QModbusDevice::UnknownError |
8 |
An unknown error occurred. |
QModbusDevice::InvalidResponseError (since Qt 6.4) |
9 |
An error occurred while parsing the response, or the FunctionCode is not supported by the current implementation. In the latter case custom Modbus client implementation can override the processResponse() and processPrivateResponse() methods to provide support for needed functions. |
[since 6.0] enum QModbusDevice::IntermediateError▲
This enum describes possible errors that can happen during a full send and receive cycle for a Modbus reply.
Constant |
Value |
Description |
---|---|---|
QModbusDevice::ResponseCrcError |
0 |
A Modbus response with a wrong CRC was received. |
QModbusDevice::ResponseRequestMismatch |
1 |
A Modbus response was received but did not match the open request, probably due to the PDU's function code not matching. |
If any of the above intermediate errors occurred, the frame is likely resent until the maximum number of retries has been reached.
The list of intermediate errors can be inspected from the QModbusReply intermediate errors function.
This enum was introduced or modified in Qt 6.0.
See Also▲
enum QModbusDevice::State▲
This enum describes all possible device states.
Constant |
Value |
Description |
---|---|---|
QModbusDevice::UnconnectedState |
0 |
The device is disconnected. |
QModbusDevice::ConnectingState |
1 |
The device is being connected. |
QModbusDevice::ConnectedState |
2 |
The device is connected to the Modbus network. |
QModbusDevice::ClosingState |
3 |
The device is being closed. |
Member Function Documentation▲
[explicit] QModbusDevice::QModbusDevice(QObject *parent = nullptr)▲
Constructs a Modbus device with the specified parent.
[virtual] QModbusDevice::~QModbusDevice()▲
Destroys the QModbusDevice instance
[pure virtual protected] void QModbusDevice::close()▲
This function is responsible for closing the Modbus connection. The implementation must ensure that the instance's state() is set to QModbusDevice::UnconnectedState.
See Also▲
See also disconnectDevice()
bool QModbusDevice::connectDevice()▲
Connects the device to the Modbus network. Returns true if the connection process was successfully initiated; otherwise false. Final connection success confirmation requires the state() changing to QModbusDevice::ConnectedState.
This function calls open() as part of its implementation.
See Also▲
See also open()
QVariant QModbusDevice::connectionParameter(QModbusDevice::ConnectionParameter parameter) const▲
Returns the value associated with the given connection parameter. The returned value can be empty.
By default the QModbusDevice is initialized with some common values. The serial port settings are even parity, a baud rate of 19200 bits per second, eight data bits and one stop bit. The network settings for the host address is set to local host and port to 502.
For a serial connection to succeed, the SerialPortNameParameter needs to be set to a valid communication port. The information about valid serial ports can be obtained from QSerialPortInfo.
If the device is already connected, the settings are taken into account after reconnecting the device.
See Also▲
See also setConnectionParameter(), ConnectionParameter
[since 5.14] QIODevice *QModbusDevice::device() const▲
Returns the underlying QIODevice used for ModBus communication or nullptr if the device was not yet fully initialized.
Do not store a pointer to the underlying device, because it can be invalidated at any point in time.
This function was introduced in Qt 5.14.
void QModbusDevice::disconnectDevice()▲
Disconnects the device.
This function calls close() as part of its implementation.
QModbusDevice::Error QModbusDevice::error() const▲
void QModbusDevice::errorOccurred(QModbusDevice::Error error)▲
This signal is emitted when an error of the type, error, occurs.
QString QModbusDevice::errorString() const▲
[pure virtual protected] bool QModbusDevice::open()▲
This function is called by connectDevice(). Subclasses must provide an implementation that returns true on successful Modbus connection or connection initiation; otherwise returns false.
The implementation must ensure that the instance's state() is set to QModbusDevice::ConnectingState or QModbusDevice::ConnectedState upon success; otherwise QModbusDevice::UnconnectedState. Typically, QModbusDevice::ConnectingState is used when the connection process reports back asynchronously and QModbusDevice::ConnectedState in case of synchronous connect behavior.
See Also▲
See also connectDevice()
void QModbusDevice::setConnectionParameter(QModbusDevice::ConnectionParameter parameter, const QVariant &value)▲
Sets the value of parameter to value. If the parameter already exists, the previous value is overwritten. A active or running connection is not affected by such parameter changes.
See Also▲
See also ConnectionParameter, connectionParameter()
[protected] void QModbusDevice::setError(const QString &errorText, QModbusDevice::Error error)▲
Sets the error state of the device. ModBus device implementations must use this function in case of an error to set the error type and a descriptive errorText.
See Also▲
See also error(), QModbusDevice::Error
[protected] void QModbusDevice::setState(QModbusDevice::State newState)▲
Sets the state of the device to newState. Modbus device implementations must use this function to update the device state.
See Also▲
See also state()