first commit
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
#include "modbusaddress.h"
|
||||
|
||||
#include <QList>
|
||||
#include <QPair>
|
||||
|
||||
namespace {
|
||||
|
||||
bool isValidAddress(int address)
|
||||
{
|
||||
return address >= 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool ModbusAddressMap::isValid(QString *error) const
|
||||
{
|
||||
const QList<QPair<QString, int> > addresses = QList<QPair<QString, int> >()
|
||||
<< qMakePair(QString("startCommand"), startCommand)
|
||||
<< qMakePair(QString("pauseCommand"), pauseCommand)
|
||||
<< qMakePair(QString("resetCommand"), resetCommand)
|
||||
<< qMakePair(QString("clearAlarmCommand"), clearAlarmCommand)
|
||||
<< qMakePair(QString("startFeedback"), startFeedback)
|
||||
<< qMakePair(QString("pauseFeedback"), pauseFeedback)
|
||||
<< qMakePair(QString("resetFeedback"), resetFeedback)
|
||||
<< qMakePair(QString("clearAlarmFeedback"), clearAlarmFeedback)
|
||||
<< qMakePair(QString("plcReady"), plcReady)
|
||||
<< qMakePair(QString("robotReady"), robotReady)
|
||||
<< qMakePair(QString("safetyDoorClosed"), safetyDoorClosed)
|
||||
<< qMakePair(QString("emergencyStop"), emergencyStop)
|
||||
<< qMakePair(QString("currentStep"), currentStep)
|
||||
<< qMakePair(QString("checkResult"), checkResult)
|
||||
<< qMakePair(QString("errorCode"), errorCode);
|
||||
|
||||
for (const QPair<QString, int> &item : addresses) {
|
||||
if (!isValidAddress(item.second)) {
|
||||
if (error) {
|
||||
*error = QString("Modbus address is invalid: %1").arg(item.first);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef MODBUSADDRESS_H
|
||||
#define MODBUSADDRESS_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
struct ModbusAddressMap
|
||||
{
|
||||
int startCommand = 0;
|
||||
int pauseCommand = 1;
|
||||
int resetCommand = 2;
|
||||
int clearAlarmCommand = 3;
|
||||
|
||||
int startFeedback = 10;
|
||||
int pauseFeedback = 11;
|
||||
int resetFeedback = 12;
|
||||
int clearAlarmFeedback = 13;
|
||||
|
||||
int plcReady = 100;
|
||||
int robotReady = 101;
|
||||
int safetyDoorClosed = 102;
|
||||
int emergencyStop = 103;
|
||||
|
||||
int currentStep = 200;
|
||||
int checkResult = 201;
|
||||
int errorCode = 202;
|
||||
|
||||
bool isValid(QString *error = nullptr) const;
|
||||
};
|
||||
|
||||
#endif // MODBUSADDRESS_H
|
||||
@@ -0,0 +1,17 @@
|
||||
#include "modbusconfig.h"
|
||||
|
||||
ModbusConfig ModbusConfig::defaultConfig()
|
||||
{
|
||||
ModbusConfig config;
|
||||
config.host = "127.0.0.1";
|
||||
config.port = 502;
|
||||
config.serverAddress = 1;
|
||||
config.timeoutMs = 1000;
|
||||
config.retryCount = 1;
|
||||
config.pollIntervalMs = 1000;
|
||||
config.commandPulseMs = 100;
|
||||
config.feedbackTimeoutMs = 2000;
|
||||
config.reconnectDelayMs = 200;
|
||||
config.reconnectAttempts = 2;
|
||||
return config;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef MODBUSCONFIG_H
|
||||
#define MODBUSCONFIG_H
|
||||
|
||||
#include "modbusaddress.h"
|
||||
|
||||
#include <QString>
|
||||
|
||||
struct ModbusConfig
|
||||
{
|
||||
QString host;
|
||||
int port;
|
||||
int serverAddress;
|
||||
int timeoutMs;
|
||||
int retryCount;
|
||||
int pollIntervalMs;
|
||||
int commandPulseMs;
|
||||
int feedbackTimeoutMs;
|
||||
int reconnectDelayMs;
|
||||
int reconnectAttempts;
|
||||
ModbusAddressMap addressMap;
|
||||
|
||||
static ModbusConfig defaultConfig();
|
||||
};
|
||||
|
||||
#endif // MODBUSCONFIG_H
|
||||
@@ -0,0 +1,258 @@
|
||||
#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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef MODBUSTCPMANAGER_H
|
||||
#define MODBUSTCPMANAGER_H
|
||||
|
||||
#include "modbusconfig.h"
|
||||
|
||||
#include <QString>
|
||||
#include <QtGlobal>
|
||||
|
||||
class QModbusTcpClient;
|
||||
|
||||
class ModbusTcpManager
|
||||
{
|
||||
public:
|
||||
static ModbusTcpManager &instance();
|
||||
~ModbusTcpManager();
|
||||
|
||||
void setConfig(const ModbusConfig &config);
|
||||
ModbusConfig config() const;
|
||||
|
||||
bool connectDevice(QString *error = nullptr);
|
||||
void disconnectDevice();
|
||||
bool isConnected() const;
|
||||
|
||||
bool readCoil(int address, bool *value, QString *error = nullptr);
|
||||
bool readHoldingRegister(int address, quint16 *value, QString *error = nullptr);
|
||||
bool writeCoil(int address, bool value, QString *error = nullptr);
|
||||
bool writeHoldingRegister(int address, quint16 value, QString *error = nullptr);
|
||||
|
||||
QString lastErrorText() const;
|
||||
|
||||
private:
|
||||
ModbusTcpManager();
|
||||
ModbusTcpManager(const ModbusTcpManager &) = delete;
|
||||
ModbusTcpManager &operator=(const ModbusTcpManager &) = delete;
|
||||
|
||||
bool readSingleValue(int registerType, int address, quint16 *value, QString *error);
|
||||
bool writeSingleValue(int registerType, int address, quint16 value, QString *error);
|
||||
void ensureClient();
|
||||
void setError(const QString &text, QString *error);
|
||||
|
||||
ModbusConfig m_config;
|
||||
QModbusTcpClient *m_client;
|
||||
QString m_lastErrorText;
|
||||
};
|
||||
|
||||
#endif // MODBUSTCPMANAGER_H
|
||||
Reference in New Issue
Block a user