2026-08-18 09:40:43 +08:00
|
|
|
#include "modbustcpmanager.h"
|
|
|
|
|
|
|
|
|
|
#include <QModbusDataUnit>
|
|
|
|
|
#include <QModbusReply>
|
|
|
|
|
#include <QModbusTcpClient>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
#include <QVariant>
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
ModbusTcpManager::ModbusTcpManager(const ModbusConfig &config,
|
|
|
|
|
QObject *parent)
|
|
|
|
|
: QObject(parent)
|
|
|
|
|
, m_config(config)
|
|
|
|
|
, m_client(nullptr)
|
|
|
|
|
, m_reply(nullptr)
|
|
|
|
|
, m_connectTimer(new QTimer(this))
|
|
|
|
|
, m_hasCurrentRequest(false)
|
|
|
|
|
, m_connecting(false)
|
|
|
|
|
, m_aborting(false)
|
|
|
|
|
, m_connectAttempts(0)
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
2026-08-23 09:41:44 +08:00
|
|
|
m_connectTimer->setSingleShot(true);
|
|
|
|
|
connect(m_connectTimer, &QTimer::timeout,
|
|
|
|
|
this, &ModbusTcpManager::onConnectTimeout);
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
ModbusTcpManager::~ModbusTcpManager()
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
2026-08-23 09:41:44 +08:00
|
|
|
abortAll("Modbus manager is shutting down");
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
ModbusConfig ModbusTcpManager::config() const
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
2026-08-23 09:41:44 +08:00
|
|
|
return m_config;
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ModbusTcpManager::readCoil(
|
|
|
|
|
int address,
|
|
|
|
|
const std::function<void(bool, bool, const QString &)> &callback)
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
2026-08-23 09:41:44 +08:00
|
|
|
enqueue({QModbusDataUnit::Coils, address, 0, false,
|
|
|
|
|
[callback](bool success, quint16 value, const QString &error) {
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback(success, value != 0, error);
|
|
|
|
|
}
|
|
|
|
|
}});
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ModbusTcpManager::readHoldingRegister(
|
|
|
|
|
int address,
|
|
|
|
|
const std::function<void(bool, quint16, const QString &)> &callback)
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
2026-08-23 09:41:44 +08:00
|
|
|
enqueue({QModbusDataUnit::HoldingRegisters, address, 0, false, callback});
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ModbusTcpManager::writeCoil(
|
|
|
|
|
int address, bool value,
|
|
|
|
|
const std::function<void(bool, const QString &)> &callback)
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
2026-08-23 09:41:44 +08:00
|
|
|
enqueue({QModbusDataUnit::Coils, address,
|
|
|
|
|
static_cast<quint16>(value ? 1 : 0), true,
|
|
|
|
|
[callback](bool success, quint16, const QString &error) {
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback(success, error);
|
|
|
|
|
}
|
|
|
|
|
}});
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ModbusTcpManager::writeHoldingRegister(
|
|
|
|
|
int address, quint16 value,
|
|
|
|
|
const std::function<void(bool, const QString &)> &callback)
|
|
|
|
|
{
|
|
|
|
|
enqueue({QModbusDataUnit::HoldingRegisters, address, value, true,
|
|
|
|
|
[callback](bool success, quint16, const QString &error) {
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback(success, error);
|
|
|
|
|
}
|
|
|
|
|
}});
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ModbusTcpManager::abortAll(const QString &reason)
|
|
|
|
|
{
|
|
|
|
|
if (m_aborting) {
|
|
|
|
|
return;
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
const QString text = reason.isEmpty()
|
|
|
|
|
? "Modbus request was canceled" : reason;
|
2026-08-18 09:40:43 +08:00
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
m_aborting = true;
|
|
|
|
|
m_connectTimer->stop();
|
|
|
|
|
m_connecting = false;
|
|
|
|
|
|
|
|
|
|
if (m_reply) {
|
|
|
|
|
QModbusReply *reply = m_reply;
|
|
|
|
|
m_reply = nullptr;
|
|
|
|
|
reply->disconnect(this);
|
|
|
|
|
reply->deleteLater();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_hasCurrentRequest) {
|
|
|
|
|
const Completion callback = m_currentRequest.callback;
|
|
|
|
|
m_currentRequest = Request();
|
|
|
|
|
m_hasCurrentRequest = false;
|
|
|
|
|
setError(text);
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback(false, 0, text);
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
while (!m_requests.isEmpty()) {
|
|
|
|
|
Request request = m_requests.dequeue();
|
|
|
|
|
if (request.callback) {
|
|
|
|
|
request.callback(false, 0, text);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
if (m_client
|
|
|
|
|
&& m_client->state() != QModbusDevice::UnconnectedState) {
|
2026-08-18 09:40:43 +08:00
|
|
|
m_client->disconnectDevice();
|
|
|
|
|
}
|
2026-08-23 09:41:44 +08:00
|
|
|
|
|
|
|
|
m_aborting = false;
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
bool ModbusTcpManager::isBusy() const
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
2026-08-23 09:41:44 +08:00
|
|
|
return m_hasCurrentRequest || !m_requests.isEmpty()
|
|
|
|
|
|| m_connecting || m_reply;
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ModbusTcpManager::enqueue(const Request &request)
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
2026-08-23 09:41:44 +08:00
|
|
|
if (m_aborting) {
|
|
|
|
|
if (request.callback) {
|
|
|
|
|
request.callback(false, 0, "Modbus request was canceled");
|
|
|
|
|
}
|
|
|
|
|
return;
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
if (request.address < 0) {
|
|
|
|
|
if (request.callback) {
|
|
|
|
|
request.callback(false, 0,
|
|
|
|
|
"Modbus address must be greater than or equal to 0");
|
|
|
|
|
}
|
|
|
|
|
return;
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
m_requests.enqueue(request);
|
|
|
|
|
startNext();
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ModbusTcpManager::startNext()
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
2026-08-23 09:41:44 +08:00
|
|
|
if (m_aborting || m_hasCurrentRequest || m_requests.isEmpty()) {
|
|
|
|
|
return;
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
m_currentRequest = m_requests.dequeue();
|
|
|
|
|
m_hasCurrentRequest = true;
|
|
|
|
|
m_connectAttempts = 0;
|
|
|
|
|
ensureConnected();
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ModbusTcpManager::ensureConnected()
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
2026-08-23 09:41:44 +08:00
|
|
|
if (m_aborting || !m_hasCurrentRequest) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
ensureClient();
|
|
|
|
|
if (m_client->state() == QModbusDevice::ConnectedState) {
|
|
|
|
|
startCurrentRequest();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (m_connecting) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
m_client->setConnectionParameter(
|
|
|
|
|
QModbusDevice::NetworkAddressParameter,
|
|
|
|
|
QVariant(m_config.host));
|
|
|
|
|
m_client->setConnectionParameter(
|
|
|
|
|
QModbusDevice::NetworkPortParameter,
|
|
|
|
|
QVariant(m_config.port));
|
|
|
|
|
m_client->setTimeout(m_config.timeoutMs);
|
|
|
|
|
m_client->setNumberOfRetries(m_config.retryCount);
|
|
|
|
|
|
|
|
|
|
++m_connectAttempts;
|
|
|
|
|
m_connecting = true;
|
|
|
|
|
if (!m_client->connectDevice()) {
|
|
|
|
|
handleConnectionFailure(m_client->errorString());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_connectTimer->start(qMax(1, m_config.timeoutMs));
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ModbusTcpManager::startCurrentRequest()
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
2026-08-23 09:41:44 +08:00
|
|
|
if (m_aborting || !m_hasCurrentRequest || m_connecting || m_reply) {
|
|
|
|
|
return;
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
QModbusDataUnit unit(
|
|
|
|
|
static_cast<QModbusDataUnit::RegisterType>(
|
|
|
|
|
m_currentRequest.registerType),
|
|
|
|
|
m_currentRequest.address, 1);
|
|
|
|
|
if (m_currentRequest.write) {
|
|
|
|
|
unit.setValue(0, m_currentRequest.value);
|
|
|
|
|
m_reply = m_client->sendWriteRequest(unit, m_config.serverAddress);
|
|
|
|
|
} else {
|
|
|
|
|
m_reply = m_client->sendReadRequest(unit, m_config.serverAddress);
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
if (!m_reply) {
|
|
|
|
|
finishCurrent(false, 0,
|
|
|
|
|
m_client->errorString().isEmpty()
|
|
|
|
|
? "Failed to create Modbus request"
|
|
|
|
|
: m_client->errorString());
|
|
|
|
|
return;
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
connect(m_reply, &QModbusReply::finished,
|
|
|
|
|
this, &ModbusTcpManager::onReplyFinished);
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ModbusTcpManager::finishCurrent(bool success, quint16 value,
|
|
|
|
|
const QString &error)
|
|
|
|
|
{
|
|
|
|
|
if (!m_hasCurrentRequest) {
|
|
|
|
|
return;
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
const Completion callback = m_currentRequest.callback;
|
|
|
|
|
m_currentRequest = Request();
|
|
|
|
|
m_hasCurrentRequest = false;
|
|
|
|
|
if (!success) {
|
|
|
|
|
setError(error);
|
|
|
|
|
} else {
|
|
|
|
|
m_lastErrorText.clear();
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
if (callback) {
|
|
|
|
|
callback(success, value, error);
|
|
|
|
|
}
|
|
|
|
|
if (!m_aborting) {
|
|
|
|
|
startNext();
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ModbusTcpManager::handleConnectionFailure(const QString &error)
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
2026-08-23 09:41:44 +08:00
|
|
|
if (m_aborting) {
|
|
|
|
|
return;
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
m_connectTimer->stop();
|
|
|
|
|
m_connecting = false;
|
2026-08-18 09:40:43 +08:00
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
if (m_client
|
|
|
|
|
&& m_client->state() != QModbusDevice::UnconnectedState) {
|
|
|
|
|
m_client->disconnectDevice();
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
const int attempts = qMax(1, m_config.reconnectAttempts + 1);
|
|
|
|
|
if (m_connectAttempts < attempts) {
|
|
|
|
|
QTimer::singleShot(
|
|
|
|
|
qMax(0, m_config.reconnectDelayMs),
|
|
|
|
|
this, &ModbusTcpManager::ensureConnected);
|
|
|
|
|
return;
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
finishCurrent(false, 0,
|
|
|
|
|
error.isEmpty() ? "Failed to connect to PLC" : error);
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ModbusTcpManager::ensureClient()
|
|
|
|
|
{
|
|
|
|
|
if (m_client) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
m_client = new QModbusTcpClient(this);
|
|
|
|
|
connect(m_client, &QModbusClient::stateChanged,
|
|
|
|
|
this, &ModbusTcpManager::onStateChanged);
|
|
|
|
|
connect(m_client, &QModbusClient::errorOccurred,
|
|
|
|
|
this, &ModbusTcpManager::onErrorOccurred);
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ModbusTcpManager::setError(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
m_lastErrorText = text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModbusTcpManager::onStateChanged(QModbusDevice::State state)
|
|
|
|
|
{
|
|
|
|
|
if (m_aborting) {
|
|
|
|
|
return;
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
if (state == QModbusDevice::ConnectedState) {
|
|
|
|
|
m_connectTimer->stop();
|
|
|
|
|
m_connecting = false;
|
|
|
|
|
startCurrentRequest();
|
|
|
|
|
return;
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
if (state == QModbusDevice::UnconnectedState && m_connecting) {
|
|
|
|
|
handleConnectionFailure(m_client ? m_client->errorString()
|
|
|
|
|
: QString());
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ModbusTcpManager::onErrorOccurred(QModbusDevice::Error error)
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
2026-08-23 09:41:44 +08:00
|
|
|
Q_UNUSED(error)
|
|
|
|
|
if (m_aborting) {
|
2026-08-18 09:40:43 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
if (m_connecting) {
|
|
|
|
|
handleConnectionFailure(m_client ? m_client->errorString()
|
|
|
|
|
: QString());
|
|
|
|
|
}
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-23 09:41:44 +08:00
|
|
|
void ModbusTcpManager::onReplyFinished()
|
2026-08-18 09:40:43 +08:00
|
|
|
{
|
2026-08-23 09:41:44 +08:00
|
|
|
QModbusReply *reply = qobject_cast<QModbusReply *>(sender());
|
|
|
|
|
if (!reply || reply != m_reply) {
|
|
|
|
|
if (reply) {
|
|
|
|
|
reply->deleteLater();
|
|
|
|
|
}
|
|
|
|
|
return;
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|
2026-08-23 09:41:44 +08:00
|
|
|
|
|
|
|
|
m_reply = nullptr;
|
|
|
|
|
const bool success = reply->error() == QModbusDevice::NoError;
|
|
|
|
|
quint16 value = 0;
|
|
|
|
|
QString error;
|
|
|
|
|
if (!success) {
|
|
|
|
|
error = reply->errorString();
|
|
|
|
|
} else if (!m_currentRequest.write) {
|
|
|
|
|
const QModbusDataUnit result = reply->result();
|
|
|
|
|
if (result.valueCount() < 1) {
|
|
|
|
|
error = "Modbus read returned an empty result";
|
|
|
|
|
} else {
|
|
|
|
|
value = result.value(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reply->deleteLater();
|
|
|
|
|
if (!error.isEmpty()) {
|
|
|
|
|
finishCurrent(false, 0, error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finishCurrent(true, value, QString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModbusTcpManager::onConnectTimeout()
|
|
|
|
|
{
|
|
|
|
|
handleConnectionFailure("Modbus TCP connect timeout");
|
2026-08-18 09:40:43 +08:00
|
|
|
}
|