259 lines
6.3 KiB
C++
259 lines
6.3 KiB
C++
#include "modbustcpmanager.h"
|
|||
|
|
|
||
|
|
#include <QEventLoop>
|
||
|
|
#include <QModbusDataUnit>
|
||
|
|
#include <QModbusReply>
|
||
|
|
#include <QModbusTcpClient>
|
||
|
|
#include <QTimer>
|
||
|
|
#include <QVariant>
|
||
|
|
|
||
|
|
ModbusTcpManager &ModbusTcpManager::instance()
|
||
|
|
{
|
||
|
|
static ModbusTcpManager manager;
|
||
|
|
return manager;
|
||
|
|
}
|
||
|
|
|
||
|
|
ModbusTcpManager::ModbusTcpManager()
|
||
|
|
: m_config(ModbusConfig::defaultConfig()),
|
||
|
|
m_client(nullptr)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
ModbusTcpManager::~ModbusTcpManager()
|
||
|
|
{
|
||
|
|
disconnectDevice();
|
||
|
|
delete m_client;
|
||
|
|
}
|
||
|
|
|
||
|
|
void ModbusTcpManager::setConfig(const ModbusConfig &config)
|
||
|
|
{
|
||
|
|
if (isConnected()) {
|
||
|
|
disconnectDevice();
|
||
|
|
}
|
||
|
|
m_config = config;
|
||
|
|
}
|
||
|
|
|
||
|
|
ModbusConfig ModbusTcpManager::config() const
|
||
|
|
{
|
||
|
|
return m_config;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool ModbusTcpManager::connectDevice(QString *error)
|
||
|
|
{
|
||
|
|
ensureClient();
|
||
|
|
if (isConnected()) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
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);
|
||
|
|
|
||
|
|
if (!m_client->connectDevice()) {
|
||
|
|
setError(m_client->errorString(), error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
QEventLoop loop;
|
||
|
|
QTimer timer;
|
||
|
|
timer.setSingleShot(true);
|
||
|
|
|
||
|
|
QObject::connect(m_client, &QModbusClient::stateChanged, &loop, [&](QModbusDevice::State state) {
|
||
|
|
if (state == QModbusDevice::ConnectedState || state == QModbusDevice::UnconnectedState) {
|
||
|
|
loop.quit();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
QObject::connect(m_client, &QModbusClient::errorOccurred, &loop, [&](QModbusDevice::Error) {
|
||
|
|
loop.quit();
|
||
|
|
});
|
||
|
|
QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
|
||
|
|
|
||
|
|
timer.start(m_config.timeoutMs);
|
||
|
|
loop.exec();
|
||
|
|
|
||
|
|
if (!isConnected()) {
|
||
|
|
QString text = m_client->errorString();
|
||
|
|
if (text.isEmpty()) {
|
||
|
|
text = "Modbus TCP connect timeout";
|
||
|
|
}
|
||
|
|
setError(text, error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
m_lastErrorText.clear();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void ModbusTcpManager::disconnectDevice()
|
||
|
|
{
|
||
|
|
if (m_client) {
|
||
|
|
m_client->disconnectDevice();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool ModbusTcpManager::isConnected() const
|
||
|
|
{
|
||
|
|
return m_client && m_client->state() == QModbusDevice::ConnectedState;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool ModbusTcpManager::readCoil(int address, bool *value, QString *error)
|
||
|
|
{
|
||
|
|
if (!value) {
|
||
|
|
setError("Output value pointer is null", error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
quint16 rawValue = 0;
|
||
|
|
if (!readSingleValue(QModbusDataUnit::Coils, address, &rawValue, error)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
*value = rawValue != 0;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool ModbusTcpManager::readHoldingRegister(int address, quint16 *value, QString *error)
|
||
|
|
{
|
||
|
|
if (!value) {
|
||
|
|
setError("Output value pointer is null", error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
return readSingleValue(QModbusDataUnit::HoldingRegisters, address, value, error);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool ModbusTcpManager::writeCoil(int address, bool value, QString *error)
|
||
|
|
{
|
||
|
|
return writeSingleValue(QModbusDataUnit::Coils, address, value ? 1 : 0, error);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool ModbusTcpManager::writeHoldingRegister(int address, quint16 value, QString *error)
|
||
|
|
{
|
||
|
|
return writeSingleValue(QModbusDataUnit::HoldingRegisters, address, value, error);
|
||
|
|
}
|
||
|
|
|
||
|
|
QString ModbusTcpManager::lastErrorText() const
|
||
|
|
{
|
||
|
|
return m_lastErrorText;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool ModbusTcpManager::readSingleValue(int registerType, int address, quint16 *value, QString *error)
|
||
|
|
{
|
||
|
|
if (address < 0) {
|
||
|
|
setError("Modbus address must be greater than or equal to 0", error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!connectDevice(error)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
QModbusDataUnit unit(static_cast<QModbusDataUnit::RegisterType>(registerType), address, 1);
|
||
|
|
QModbusReply *reply = m_client->sendReadRequest(unit, m_config.serverAddress);
|
||
|
|
if (!reply) {
|
||
|
|
setError(m_client->errorString(), error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
QEventLoop loop;
|
||
|
|
QTimer timer;
|
||
|
|
timer.setSingleShot(true);
|
||
|
|
|
||
|
|
QObject::connect(reply, &QModbusReply::finished, &loop, &QEventLoop::quit);
|
||
|
|
QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
|
||
|
|
|
||
|
|
timer.start(m_config.timeoutMs);
|
||
|
|
loop.exec();
|
||
|
|
|
||
|
|
if (!reply->isFinished()) {
|
||
|
|
reply->deleteLater();
|
||
|
|
setError("Modbus TCP read timeout", error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (reply->error() != QModbusDevice::NoError) {
|
||
|
|
const QString text = reply->errorString();
|
||
|
|
reply->deleteLater();
|
||
|
|
setError(text, error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
const QModbusDataUnit result = reply->result();
|
||
|
|
if (result.valueCount() < 1) {
|
||
|
|
reply->deleteLater();
|
||
|
|
setError("Modbus TCP read returned empty result", error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
*value = result.value(0);
|
||
|
|
reply->deleteLater();
|
||
|
|
m_lastErrorText.clear();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool ModbusTcpManager::writeSingleValue(int registerType, int address, quint16 value, QString *error)
|
||
|
|
{
|
||
|
|
if (address < 0) {
|
||
|
|
setError("Modbus address must be greater than or equal to 0", error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!connectDevice(error)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
QModbusDataUnit unit(static_cast<QModbusDataUnit::RegisterType>(registerType), address, 1);
|
||
|
|
unit.setValue(0, value);
|
||
|
|
|
||
|
|
QModbusReply *reply = m_client->sendWriteRequest(unit, m_config.serverAddress);
|
||
|
|
if (!reply) {
|
||
|
|
setError(m_client->errorString(), error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
QEventLoop loop;
|
||
|
|
QTimer timer;
|
||
|
|
timer.setSingleShot(true);
|
||
|
|
|
||
|
|
QObject::connect(reply, &QModbusReply::finished, &loop, &QEventLoop::quit);
|
||
|
|
QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
|
||
|
|
|
||
|
|
timer.start(m_config.timeoutMs);
|
||
|
|
loop.exec();
|
||
|
|
|
||
|
|
if (!reply->isFinished()) {
|
||
|
|
reply->deleteLater();
|
||
|
|
setError("Modbus TCP write timeout", error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (reply->error() != QModbusDevice::NoError) {
|
||
|
|
const QString text = reply->errorString();
|
||
|
|
reply->deleteLater();
|
||
|
|
setError(text, error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
reply->deleteLater();
|
||
|
|
m_lastErrorText.clear();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void ModbusTcpManager::ensureClient()
|
||
|
|
{
|
||
|
|
if (m_client) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
m_client = new QModbusTcpClient();
|
||
|
|
}
|
||
|
|
|
||
|
|
void ModbusTcpManager::setError(const QString &text, QString *error)
|
||
|
|
{
|
||
|
|
m_lastErrorText = text;
|
||
|
|
if (error) {
|
||
|
|
*error = m_lastErrorText;
|
||
|
|
}
|
||
|
|
}
|