订单流程修改--QThread事件循环导致的逻辑修改
This commit is contained in:
+74
-21
@@ -3,43 +3,96 @@
|
||||
|
||||
#include "modbusconfig.h"
|
||||
|
||||
#include <QQueue>
|
||||
#include <QObject>
|
||||
#include <QModbusDevice>
|
||||
#include <QString>
|
||||
#include <QtGlobal>
|
||||
#include <functional>
|
||||
|
||||
class QModbusTcpClient;
|
||||
class QModbusReply;
|
||||
class QTimer;
|
||||
|
||||
class ModbusTcpManager
|
||||
class ModbusTcpManager : public QObject
|
||||
{
|
||||
public:
|
||||
static ModbusTcpManager &instance();
|
||||
~ModbusTcpManager();
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
using Completion = std::function<void(bool, quint16, const QString &)>;
|
||||
|
||||
explicit ModbusTcpManager(const ModbusConfig &config,
|
||||
QObject *parent = nullptr);
|
||||
~ModbusTcpManager() override;
|
||||
|
||||
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;
|
||||
void readCoil(int address,
|
||||
const std::function<void(bool, bool, const QString &)> &callback);
|
||||
void readHoldingRegister(
|
||||
int address,
|
||||
const std::function<void(bool, quint16, const QString &)> &callback);
|
||||
void writeCoil(int address, bool value,
|
||||
const std::function<void(bool, const QString &)> &callback);
|
||||
void writeHoldingRegister(
|
||||
int address, quint16 value,
|
||||
const std::function<void(bool, const QString &)> &callback);
|
||||
void abortAll(const QString &reason);
|
||||
bool isBusy() const;
|
||||
|
||||
private:
|
||||
ModbusTcpManager();
|
||||
ModbusTcpManager(const ModbusTcpManager &) = delete;
|
||||
ModbusTcpManager &operator=(const ModbusTcpManager &) = delete;
|
||||
struct Request
|
||||
{
|
||||
Request()
|
||||
: registerType(0)
|
||||
, address(-1)
|
||||
, value(0)
|
||||
, write(false)
|
||||
{
|
||||
}
|
||||
Request(int type, int requestAddress, quint16 requestValue,
|
||||
bool requestWrite, const Completion &requestCallback)
|
||||
: registerType(type)
|
||||
, address(requestAddress)
|
||||
, value(requestValue)
|
||||
, write(requestWrite)
|
||||
, callback(requestCallback)
|
||||
{
|
||||
}
|
||||
|
||||
bool readSingleValue(int registerType, int address, quint16 *value, QString *error);
|
||||
bool writeSingleValue(int registerType, int address, quint16 value, QString *error);
|
||||
int registerType;
|
||||
int address;
|
||||
quint16 value;
|
||||
bool write;
|
||||
Completion callback;
|
||||
};
|
||||
|
||||
void enqueue(const Request &request);
|
||||
void startNext();
|
||||
void ensureConnected();
|
||||
void startCurrentRequest();
|
||||
void finishCurrent(bool success, quint16 value, const QString &error);
|
||||
void handleConnectionFailure(const QString &error);
|
||||
void ensureClient();
|
||||
void setError(const QString &text, QString *error);
|
||||
void setError(const QString &text);
|
||||
|
||||
private slots:
|
||||
void onStateChanged(QModbusDevice::State state);
|
||||
void onErrorOccurred(QModbusDevice::Error error);
|
||||
void onReplyFinished();
|
||||
void onConnectTimeout();
|
||||
|
||||
private:
|
||||
ModbusConfig m_config;
|
||||
QModbusTcpClient *m_client;
|
||||
QModbusReply *m_reply;
|
||||
QTimer *m_connectTimer;
|
||||
QQueue<Request> m_requests;
|
||||
Request m_currentRequest;
|
||||
bool m_hasCurrentRequest;
|
||||
bool m_connecting;
|
||||
bool m_aborting;
|
||||
int m_connectAttempts;
|
||||
QString m_lastErrorText;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user