- 在普通 RackPick 前写托盘号、格位并置 M850,收到 M851 后清除两位 - 使用 vision/enabled 控制识别,关闭时仍执行拍照到位流程 - 识别成功且 R60 写入确认后写 M852=1,视觉关闭或失败时写 M852=0 - 确认 M852 写入后启动 RackPick,动作结束保留 M852 - 移除旧 850–854 五信号握手,更新配置和协议文档 - 允许未就绪的视觉服务接收测量,扩展超时以覆盖初始化重试 - 保留 PLC 写入失败阻断、暂停发送门控及迟到结果拒绝 - 持久化未完成事务,支持 PLC 暂停后的视觉维护与人工恢复 验证:MinGW 32/64 位编译通过;生产流程47、IPC14、PLC11项测试通过
139 lines
4.5 KiB
C++
139 lines
4.5 KiB
C++
#ifndef MODBUSTCPMANAGER_H
|
|
#define MODBUSTCPMANAGER_H
|
|
|
|
#include "modbusconfig.h"
|
|
#include "../models/visionresult.h"
|
|
|
|
#include <QQueue>
|
|
#include <QObject>
|
|
#include <QModbusDevice>
|
|
#include <QString>
|
|
#include <QVector>
|
|
#include <QtGlobal>
|
|
#include <functional>
|
|
|
|
class QModbusTcpClient;
|
|
class QModbusReply;
|
|
class QTimer;
|
|
|
|
class ModbusTcpManager : public QObject
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
using Completion = std::function<void(bool, quint16, const QString &)>;
|
|
using VisionWriteCallback = std::function<void(VisionWriteOutcome, const QString &)>;
|
|
|
|
explicit ModbusTcpManager(const ModbusConfig &config,
|
|
QObject *parent = nullptr);
|
|
~ModbusTcpManager() override;
|
|
|
|
ModbusConfig config() const;
|
|
|
|
void readCoil(int address,
|
|
const std::function<void(bool, bool, const QString &)> &callback);
|
|
void readCoils(
|
|
int startAddress, quint16 count,
|
|
const std::function<void(
|
|
bool, const QVector<bool> &, const QString &)> &callback,
|
|
bool visionRequest = false);
|
|
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);
|
|
bool readRReal(
|
|
int rAddress,
|
|
const std::function<void(bool, float, const QString &)> &callback);
|
|
bool writeRReal(
|
|
int rAddress, float value,
|
|
const std::function<void(bool, const QString &)> &callback);
|
|
bool readRReals(int rAddress, int count,
|
|
const std::function<void(bool, const QVector<double> &, const QString &)> &callback,
|
|
bool lowWordFirst = true);
|
|
bool writeRReals(int rAddress, const QVector<double> &values,
|
|
const VisionWriteCallback &callback, bool lowWordFirst = true,
|
|
const std::function<bool()> &beforeSend = {});
|
|
void readVisionCoil(int address,
|
|
const std::function<void(bool, bool, const QString &)> &callback);
|
|
void writeVisionCoil(int address, bool value, const VisionWriteCallback &callback,
|
|
const std::function<bool()> &beforeSend = {});
|
|
void writeVisionHoldingRegister(int address, quint16 value, const VisionWriteCallback &callback,
|
|
const std::function<bool()> &beforeSend);
|
|
void cancelVisionRequests(const QString &reason);
|
|
void abortAll(const QString &reason);
|
|
bool isBusy() const;
|
|
|
|
private:
|
|
using ValuesCompletion = std::function<void(
|
|
bool, const QVector<quint16> &, const QString &)>;
|
|
|
|
struct Request
|
|
{
|
|
Request()
|
|
: registerType(0)
|
|
, address(-1)
|
|
, count(0)
|
|
, write(false)
|
|
{
|
|
}
|
|
Request(int type, int requestAddress, quint16 requestCount,
|
|
const QVector<quint16> &requestValues, bool requestWrite,
|
|
const ValuesCompletion &requestCallback)
|
|
: registerType(type)
|
|
, address(requestAddress)
|
|
, count(requestCount)
|
|
, values(requestValues)
|
|
, write(requestWrite)
|
|
, callback(requestCallback)
|
|
{
|
|
}
|
|
|
|
int registerType;
|
|
int address;
|
|
quint16 count;
|
|
QVector<quint16> values;
|
|
bool write;
|
|
ValuesCompletion callback;
|
|
bool vision = false;
|
|
bool sent = false;
|
|
VisionWriteCallback writeCallback;
|
|
std::function<bool()> beforeSend;
|
|
};
|
|
|
|
void enqueue(const Request &request);
|
|
void startNext();
|
|
void ensureConnected();
|
|
void startCurrentRequest();
|
|
void finishCurrent(bool success, const QVector<quint16> &values,
|
|
const QString &error);
|
|
void handleConnectionFailure(const QString &error);
|
|
void ensureClient();
|
|
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;
|
|
bool m_cancelingVision = false;
|
|
int m_connectAttempts;
|
|
QString m_lastErrorText;
|
|
};
|
|
|
|
#endif // MODBUSTCPMANAGER_H
|