This commit is contained in:
2025-10-20 22:28:37 +08:00
parent eeb5af98eb
commit 7d148d79c9
429 changed files with 13624 additions and 586480 deletions

View File

@@ -61,9 +61,11 @@ bool OpcUaManager::connectToServer(const QString &url, const QString &username,
return false;
}
mClient->requestEndpoints(mServerUrl);
connect(mClient, &QOpcUaClient::readNodeAttributesFinished,this, &OpcUaManager::onBatchReadFinished);
return true;
bool isOK = mClient->requestEndpoints(mServerUrl);
return isOK;
}
void OpcUaManager::disconnectFromServer()
@@ -196,22 +198,25 @@ bool OpcUaManager::writeNodeValue(const QString &nodeId,
int maxRetry /*= 3*/,
int retryIntervalMs /*= 200*/)
{
QString mNodeID = nodeId;
if(!mNodeID.contains("ns=6;s=::AsGlobalPV:"))
mNodeID = "ns=6;s=::AsGlobalPV:"+mNodeID;
if (!mClient || mClient->state() != QOpcUaClient::Connected) {
qWarning().noquote() << "[OPC] 客户端未连接,写入" << nodeId << "失败";
qWarning().noquote() << "[OPC] 客户端未连接,写入" << mNodeID << "失败";
return false;
}
QOpcUaNode *node = mClient->node(nodeId);
QOpcUaNode *node = mClient->node(mNodeID);
if (!node) {
qWarning().noquote() << "[OPC] 无法写入节点:" << nodeId;
qWarning().noquote() << "[OPC] 无法写入节点:" << mNodeID;
return false;
}
for (int attempt = 1; attempt <= maxRetry; ++attempt) {
/* ---------- 1. 先读当前值 ---------- */
QVariant cur = gOPC_NodeValue[nodeId];
QVariant cur = gOPC_NodeValue[mNodeID];
if (!cur.isValid()) {
//qWarning().noquote() << "读到无效值:" << nodeId<<gOPC_NodeValue[nodeId];
//qWarning().noquote() << "读到无效值:" << mNodeID<<gOPC_NodeValue[mNodeID];
}
/* ---------- 2. 类型转换 ---------- */
@@ -247,16 +252,17 @@ bool OpcUaManager::writeNodeValue(const QString &nodeId,
/* ---------- 4. 失败日志 & 重试 ---------- */
// qWarning().noquote() << "[OPC] 第" << attempt << "/" << maxRetry
// << "次写入失败:" << nodeId << " 等待重试 ...";
// << "次写入失败:" << mNodeID << " 等待重试 ...";
if (attempt < maxRetry)
QThread::msleep(retryIntervalMs);
}
qCritical().noquote() << "[OPC] 最终写入失败:" << nodeId;
qCritical().noquote() << "[OPC] 最终写入失败:" << mNodeID;
return false;
}
QOpcUaClient::ClientState OpcUaManager::connectionState() const
{
qDebug()<<"实际状态:"<<mClient->state();
if (mClient) {
return mClient->state();
}