QOpcUaNode Class
QOpcUaNode 支持与 OPC UA 节点进行交互。更多内容...
| 标题: | #include <QOpcUaNode> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS OpcUa) target_link_libraries(mytarget PRIVATE Qt6::OpcUa) |
| qmake: | QT += opcua |
| 继承自: | QObject |
公共类型
公共函数
| QVariant | attribute(QOpcUa::NodeAttribute attribute) const |
| QOpcUa::UaStatusCode | attributeError(QOpcUa::NodeAttribute attribute) const |
| bool | browse(const QOpcUaBrowseRequest &request) |
| bool | browseChildren(QOpcUa::ReferenceTypeId referenceType = QOpcUa::ReferenceTypeId::HierarchicalReferences, QOpcUa::NodeClasses nodeClassMask = QOpcUa::NodeClass::Undefined) |
| bool | callMethod(const QString &methodNodeId, const QList<QOpcUa::TypedVariant> &args = QList<QOpcUa::TypedVariant>()) |
| QOpcUaClient * | client() const |
| bool | disableMonitoring(QOpcUa::NodeAttributes attr) |
| bool | enableMonitoring(QOpcUa::NodeAttributes attr, const QOpcUaMonitoringParameters &settings) |
| bool | modifyDataChangeFilter(QOpcUa::NodeAttribute attr, const QOpcUaMonitoringParameters::DataChangeFilter &filter) |
| bool | modifyEventFilter(const QOpcUaMonitoringParameters::EventFilter &eventFilter) |
| bool | modifyMonitoring(QOpcUa::NodeAttribute attr, QOpcUaMonitoringParameters::Parameter item, const QVariant &value) |
| QOpcUaMonitoringParameters | monitoringStatus(QOpcUa::NodeAttribute attr) |
| QString | nodeId() const |
| bool | readAttributeRange(QOpcUa::NodeAttribute attribute, const QString &indexRange) |
| bool | readAttributes(QOpcUa::NodeAttributes attributes = mandatoryBaseAttributes()) |
(since 6.7) QOpcUaHistoryReadResponse * | readHistoryEvents(const QDateTime &startTime, const QDateTime &endTime, QOpcUaMonitoringParameters::EventFilter &filter, quint32 numValues = 0) |
(since 6.3) QOpcUaHistoryReadResponse * | readHistoryRaw(const QDateTime &startTime, const QDateTime &endTime, quint32 numValues, bool returnBounds) |
(since 6.7) QOpcUaHistoryReadResponse * | readHistoryRaw(const QDateTime &startTime, const QDateTime &endTime, quint32 numValues, bool returnBounds, QOpcUa::TimestampsToReturn timestampsToReturn) |
| bool | readValueAttribute() |
| bool | resolveBrowsePath(const QList<QOpcUaRelativePathElement> &path) |
| QDateTime | serverTimestamp(QOpcUa::NodeAttribute attribute) const |
| QDateTime | sourceTimestamp(QOpcUa::NodeAttribute attribute) const |
| QVariant | valueAttribute() const |
| QOpcUa::UaStatusCode | valueAttributeError() const |
| bool | writeAttribute(QOpcUa::NodeAttribute attribute, const QVariant &value, QOpcUa::Types type = QOpcUa::Types::Undefined) |
| bool | writeAttributeRange(QOpcUa::NodeAttribute attribute, const QVariant &value, const QString &indexRange, QOpcUa::Types type = QOpcUa::Types::Undefined) |
| bool | writeAttributes(const QOpcUaNode::AttributeMap &toWrite, QOpcUa::Types valueAttributeType = QOpcUa::Types::Undefined) |
| bool | writeValueAttribute(const QVariant &value, QOpcUa::Types type = QOpcUa::Types::Undefined) |
信号
| void | attributeRead(QOpcUa::NodeAttributes attributes) |
| void | attributeUpdated(QOpcUa::NodeAttribute attr, QVariant value) |
| void | attributeWritten(QOpcUa::NodeAttribute attribute, QOpcUa::UaStatusCode statusCode) |
| void | browseFinished(QList<QOpcUaReferenceDescription> children, QOpcUa::UaStatusCode statusCode) |
| void | dataChangeOccurred(QOpcUa::NodeAttribute attr, QVariant value) |
| void | disableMonitoringFinished(QOpcUa::NodeAttribute attr, QOpcUa::UaStatusCode statusCode) |
| void | enableMonitoringFinished(QOpcUa::NodeAttribute attr, QOpcUa::UaStatusCode statusCode) |
| void | eventOccurred(QVariantList eventFields) |
| void | methodCallFinished(QString methodNodeId, QVariant result, QOpcUa::UaStatusCode statusCode) |
| void | monitoringStatusChanged(QOpcUa::NodeAttribute attr, QOpcUaMonitoringParameters::Parameters items, QOpcUa::UaStatusCode statusCode) |
| void | resolveBrowsePathFinished(QList<QOpcUaBrowsePathTarget> targets, QList<QOpcUaRelativePathElement> path, QOpcUa::UaStatusCode statusCode) |
(since 6.7) void | valueAttributeUpdated(const QVariant &value) |
静态公共成员
| QOpcUa::NodeAttributes | allBaseAttributes() |
| QOpcUa::NodeAttributes | mandatoryBaseAttributes() |
详细说明
节点是 OPC UA 地址空间的基本构成单元。它具有浏览名、值、相关属性等属性,并可引用地址空间中的其他节点。 节点组织在命名空间中,并具有ID,该ID可以是数字、字符串、命名空间特定格式(不透明)或全局唯一标识符等。 节点通过命名空间 ID 和节点 ID 来标识。该标识符通常以字符串形式给出:位于命名空间 0 且具有数字标识符 42 的节点的标识符为字符串ns=0;i=42 。具有字符串标识符的节点可通过ns=0;s=myStringIdentifier 进行寻址。
此类对象归用户所有,在不再需要时必须予以删除。只要创建它们的QOpcUaClient 仍然存在,这些对象就一直有效。
属性的读写操作
调用readAttributes()或readAttributeRange()时,会从服务器读取节点属性。结果将缓存于本地,并在接收到attributeRead 信号后,可通过attribute()进行检索。
如果用户具备必要的权限,可以使用writeAttribute()、writeAttributes() 和writeAttributeRange() 写入属性。写入操作的成功与否通过attributeWritten 信号进行报告。
attributeError() 包含与该属性最近一次读写操作相关联的状态码。这是 OPC UA 服务返回的底层状态码。可通过调用QOpcUa::errorCategory() 将其转换为QOpcUa::ErrorCategory ,从而简化该状态码。
订阅和监控项
订阅是 OPC UA 中的一个概念,它允许在数据发生变化或出现事件时接收通知,而无需持续轮询节点以检测变化。 被监视项定义了如何监视节点的属性变化。它们被添加到订阅中,由此产生的任何通知都会通过订阅转发给用户。更新间隔以及被监视项和订阅的许多其他选项均可由用户配置。
QOpcUaNode 提供了一种与订阅和受监控项交互的抽象机制。enableMonitoring() 可为一个或多个属性启用数据变更通知。dataChangeOccurred 信号包含新值,且本地缓存会随之更新。disableMonitoring() 可禁用数据变更通知。monitoringStatusChanged 信号用于通知监控状态的变更,例如在手动启用或禁用后,或服务器状态发生变化时。
事件监控使用相同的 API 进行配置和生命周期管理。必须通过EventFilter 监控EventNotifier 属性,该信号会选择所需的事件字段,并根据用户定义的标准过滤报告的事件。事件通过eventOccurred() 信号以QVariantList 的形式报告,其中包含所选事件字段的值。
订阅和被监控项的设置可在运行时通过modifyMonitoring()进行修改。
浏览地址空间
OPC UA 地址空间由通过引用相互连接的节点组成。browseChildren 会沿这些引用向前遍历,并通过 `browseFinished ` 信号返回与 `QOpcUaNode ` 实例后方节点相连的所有节点的属性。browse() 与browseChildren() 类似,但提供了更多用于配置浏览调用的选项。
方法调用
OPC UA 规定了服务器上可供用户调用的方法。QOpcUaNode 通过callMethod 支持此功能,该方法接受参数,并在methodCallFinished 信号中返回调用结果。
解析浏览路径
为了支持基于类型描述的编程,OPC UA 支持解析从特定节点开始的浏览名称路径,以获取目标节点的节点 ID。resolveBrowsePath() 方法会沿着从调用该方法的节点开始的路径进行遍历,并将结果返回至resolveBrowsePathFinished() 信号中。
示例
有关将客户端连接到服务器并获取QOpcUaNode 对象的详细信息,请参阅QOpcUaClient 。
节点成功创建后,从服务器读取根节点的BrowseName:
QOpcUaNode*rootNode;// 此前已创建,请参阅 QOpcUaClient 文档。
// 订阅 attributeRead 信号。也可以使用 QObject 的兼容槽来代替 lambda 表达式。
QObject::connect(rootNode, &QOpcUaNode::attributeRead, [rootNode,client](QOpcUa::NodeAttributes attr) {
qDebug() << "Signal for attributes:" << attr;
if(rootNode->attributeError(QOpcUa::NodeAttribute::BrowseName)!= QOpcUa::UaStatusCode::Good) {
qDebug() << "Failed to read attribute:" << rootNode->attributeError(QOpcUa::NodeAttribute::BrowseName);
client->disconnectFromEndpoint();
}
qDebug() << "Browse name:" << rootNode->attribute(QOpcUa::NodeAttribute::BrowseName).value<QOpcUaQualifiedName>().name();
});
rootNode->readAttributes(QOpcUa::NodeAttribute::BrowseName);// 启动对该节点 BrowseName 属性的读取操作。成员函数文档
[static constexpr] QOpcUa::NodeAttributes QOpcUaNode::allBaseAttributes()
包含 OPC UA 基础节点类的所有属性。
QVariant QOpcUaNode::attribute(QOpcUa::NodeAttribute attribute) const
返回attribute 中指定的属性的值。
该值仅在发出attributeRead 信号后才有效。如果该属性没有缓存值,则返回一个空的QVariant 。
QOpcUa::UaStatusCode QOpcUaNode::attributeError(QOpcUa::NodeAttribute attribute) const
返回attribute 中指定属性的错误代码。
该错误代码仅在发出attributeRead 或attributeWritten 信号后才有效。
如果属性缓存中没有条目,则返回BadNoEntryExists 。
另请参阅 QOpcUa::errorCategory 。
[signal] void QOpcUaNode::attributeRead(QOpcUa::NodeAttributes attributes)
在完成readAttributes() 或readAttributeRange() 操作后,将发出此信号。接收方必须检查状态码,以获取attributes 中包含的属性。
[signal] void QOpcUaNode::attributeUpdated(QOpcUa::NodeAttribute attr, QVariant value)
当属性缓存中的值因来自服务器的数据变更通知、读取操作或写入操作而被更新后,将发出此信号。value 包含节点属性attr 的新值。
另请参阅 attribute()、attributeError()、serverTimestamp()、sourceTimestamp() 以及valueAttributeUpdated()。
[signal] void QOpcUaNode::attributeWritten(QOpcUa::NodeAttribute attribute, QOpcUa::UaStatusCode statusCode)
当writeAttribute()、writeAttributes() 或writeAttributeRange() 操作完成后,会发出此信号。
在发出此信号之前,如果写入成功,将更新属性缓存。对于writeAttributes(),写入调用中的每个属性都会触发一次信号。statusCode 包含attribute 上写入操作的成功信息。
bool QOpcUaNode::browse(const QOpcUaBrowseRequest &request)
从该节点发起一个浏览调用。
如果异步调用已成功分派,则返回true 。
所有符合 `request ` 中指定条件的引用都将通过 `browseFinished()` 信号返回。
例如,可以使用反向浏览调用查找属性节点的父节点:
QOpcUaBrowseRequest request;
request.setBrowseDirection(QOpcUaBrowseRequest::BrowseDirection::Inverse);
request.setReferenceTypeId(QOpcUa::ReferenceTypeId::HasProperty);
propertyNode->browse(request);bool QOpcUaNode::browseChildren(QOpcUa::ReferenceTypeId referenceType = QOpcUa::ReferenceTypeId::HierarchicalReferences, QOpcUa::NodeClasses nodeClassMask = QOpcUa::NodeClass::Undefined)
从调用此方法的节点开始执行向前浏览调用。该浏览操作会收集与该节点相连的子节点信息,并通过browseFinished()信号返回结果。
如果异步调用已成功分发,则返回true 。
若要仅请求通过特定引用类型与该节点连接的子节点,必须将referenceType 设置为该引用类型。例如,可通过在referenceType 中传入HasProperty 来获取节点的所有属性。通过在nodeClassMask 中设置节点类,可过滤结果以仅包含具有特定节点类的节点。
[signal] void QOpcUaNode::browseFinished(QList<QOpcUaReferenceDescription> children, QOpcUa::UaStatusCode statusCode)
该信号在browseChildren() 或browse() 操作完成后触发。
children browseChildren() 包含所有符合 () 中条件的所有节点的信息。 包含浏览操作的服务结果。如果 不是 ,则传入的 向量为空。statusCode statusCode Good children
另请参阅 QOpcUaReferenceDescription 。
bool QOpcUaNode::callMethod(const QString &methodNodeId, const QList<QOpcUa::TypedVariant> &args = QList<QOpcUa::TypedVariant>())
调用OPC UA方法methodNodeId ,并使用通过args 传递的参数。结果将通过methodCallFinished 信号返回。
如果异步调用已成功分发,则返回true 。
QOpcUaClient *QOpcUaNode::client() const
返回指向创建此节点的客户端的指针。
[signal] void QOpcUaNode::dataChangeOccurred(QOpcUa::NodeAttribute attr, QVariant value)
在收到数据变更通知后,将发出此信号。value 包含节点属性attr 的新值。
另请参阅 attribute()、serverTimestamp() 以及sourceTimestamp()。
bool QOpcUaNode::disableMonitoring(QOpcUa::NodeAttributes attr)
此方法将禁用对attr 中指定属性的监听。
如果异步调用已成功分发,则返回true 。
调用完成后,将触发disableMonitoringFinished 信号,且monitoringStatus 将返回一个默认构造的值,其状态码为 BadMonitoredItemIdIinvalid,对应attr 。
[signal] void QOpcUaNode::disableMonitoringFinished(QOpcUa::NodeAttribute attr, QOpcUa::UaStatusCode statusCode)
在异步调用disableMonitoring()完成后,将发出此信号。statusCode 包含该操作生成的状态码。该信号发出后,monitoringStatus 将返回一个默认构造的值,其中attr 的状态码为BadMonitoredItemIdIinvalid。
bool QOpcUaNode::enableMonitoring(QOpcUa::NodeAttributes attr, const QOpcUaMonitoringParameters &settings)
该方法会针对 `attr` 中指定的每个属性创建一个受监控项。在创建受监控项和订阅时,会使用 `settings ` 中的设置。
如果异步调用已成功分发,则返回true 。
调用完成后,将发出enableMonitoringFinished 信号。存在多种会导致生成错误状态码的情况:settings 中指定的订阅 ID 不存在的订阅、服务器上不存在该节点、节点不具有请求的属性,或者服务器已达到监控项的最大数量。
该方法同样适用于启用事件监控。事件是 OPC UA 地址空间中的特殊对象,包含已发生事件的相关信息。若服务器上触发了事件,事件监控项将收集事件对象及其子节点的节点属性中选定的值。 每个具有事件源的节点均可进行事件监控。要对节点进行事件监控,必须使用事件过滤器(EventFilter)监控属性EventNotifier ,该过滤器应包含用户所需的事件字段,并可选地包含用于根据条件过滤事件的where子句(更多详细信息,请参阅QOpcUaMonitoringParameters::EventFilter )。
[signal] void QOpcUaNode::enableMonitoringFinished(QOpcUa::NodeAttribute attr, QOpcUa::UaStatusCode statusCode)
在异步调用enableMonitoring()完成后,将发出此信号。该信号发出后,monitoringStatus()将返回关于attr 的有效信息。statusCode 包含该操作的状态码。
[signal] void QOpcUaNode::eventOccurred(QVariantList eventFields)
在接收到新事件后,将发出此信号。
eventFields 其中包含事件字段的值,其顺序与事件过滤器中select 子句中指定的顺序一致。
[static constexpr] QOpcUa::NodeAttributes QOpcUaNode::mandatoryBaseAttributes()
包含 OPC UA 基础节点类的所有必选属性。
[signal] void QOpcUaNode::methodCallFinished(QString methodNodeId, QVariant result, QOpcUa::UaStatusCode statusCode)
当服务器上对 `methodNodeId ` 的方法调用完成后,会触发此信号。`statusCode ` 包含方法调用的状态码,`result ` 包含方法的输出参数。如果方法没有输出参数,或者 `statusCode ` 不等于 `Good`,则 `result ` 为空。result 变体在仅有一个输出参数时为单个值;若被调用函数返回了多个输出参数,则该变体包含一个变体列表。
if (result.canConvert<QVariantList>()) {
// handle list type
} else {
// handle value type
}bool QOpcUaNode::modifyDataChangeFilter(QOpcUa::NodeAttribute attr, const QOpcUaMonitoringParameters::DataChangeFilter &filter)
修改现有的数据变更监控,使其使用filter 作为数据变更过滤器。
如果过滤器修改请求已成功分发到后端,则返回true 。
monitoringStatusChanged 在操作完成后,会触发针对 `attr ` 的事件。
bool QOpcUaNode::modifyEventFilter(const QOpcUaMonitoringParameters::EventFilter &eventFilter)
修改现有的事件监控,使其使用eventFilter 作为事件过滤器。
如果过滤器修改请求已成功分发至后端,则返回 `true `。
monitoringStatusChanged 操作完成后,将触发针对 `EventNotifier ` 的事件。
bool QOpcUaNode::modifyMonitoring(QOpcUa::NodeAttribute attr, QOpcUaMonitoringParameters::Parameter item, const QVariant &value)
此方法用于修改被监控项或订阅的设置。系统将尝试将被监控项或订阅的item 参数(与attr 相关联)设置为value 。
如果异步调用已成功分发,则返回true 。
调用完成后,将触发monitoringStatusChanged 信号。该信号包含已修改的参数和状态码。如果与所请求属性关联的被监控项不存在、未实现对所请求参数的修改,或者服务器拒绝了所请求的值,则会生成错误状态码。
QOpcUaMonitoringParameters QOpcUaNode::monitoringStatus(QOpcUa::NodeAttribute attr)
返回与属性attr 相关的监控参数。这可用于检查enableMonitoring()的执行结果,或确认参数是否已被修改。只有在attr 触发了enableMonitoringFinished 或monitoringStatusChanged 信号后,返回值才有效。如果在信号触发之前查询状态,QOpcUaMonitoringParameters::statusCode()将返回BadNoEntryExists 。
[signal] void QOpcUaNode::monitoringStatusChanged(QOpcUa::NodeAttribute attr, QOpcUaMonitoringParameters::Parameters items, QOpcUa::UaStatusCode statusCode)
在异步调用modifyMonitoring()完成后,会发出此信号。attr 中返回了请求操作所针对的节点属性。items 包含已被修改的参数。statusCode 包含服务器上修改操作的结果。
QString QOpcUaNode::nodeId() const
返回 OPC UA 节点的 ID。
bool QOpcUaNode::readAttributeRange(QOpcUa::NodeAttribute attribute, const QString &indexRange)
启动针对节点属性attribute 的异步读取操作。indexRange 是一个字符串,可用于选择数组的一部分。该属性在OPC UA 1.05第4部分的7.27节中进行了定义。 数组中的第一个元素为 0,“1” 返回第二个元素,“0:9” 返回前 10 个元素,“0,1” 返回二维数组第一行中的第二个元素。
如果异步调用已成功分发,则返回true 。
只有在发出attributeRead 信号后,属性值才包含有效信息。
bool QOpcUaNode::readAttributes(QOpcUa::NodeAttributes attributes = mandatoryBaseAttributes())
启动对attributes 中节点属性的异步读取操作。
如果异步调用已成功分发,则返回true 。
只有在发出attributeRead 信号后,属性值才会包含有效信息。
[since 6.7] QOpcUaHistoryReadResponse *QOpcUaNode::readHistoryEvents(const QDateTime &startTime, const QDateTime &endTime, QOpcUaMonitoringParameters::EventFilter &filter, quint32 numValues = 0)
使用参数startTime 、endTime 、filter 和numValues ,针对此节点启动读取事件历史记录请求。服务器将使用filter 来确定应返回哪些事件以及这些事件的哪些字段集。
若异步请求已成功分发,则返回一个QOpcUaHistoryReadResponse ,其中包含请求的状态。结果通过QOpcUaHistoryReadResponse::readHistoryEventsFinished (const QList<QOpcUaHistoryEvent> &results, QOpcUa::UaStatusCode serviceResult)信号返回。
以下示例检索过去两天的历史事件。每次最多返回10个事件。只要还有更多符合过滤条件和指定时间范围的事件,hasMoreData() 将保持为true,此时可通过readMoreData()获取更多事件。
QScopedPointer<QOpcUaNode>node(opcuaClient->node("ns=2;s=EventHistorian"));
QVERIFY(node!=nullptr);
QOpcUaMonitoringParameters::EventFilter 过滤器;
过滤器<<QOpcUaSimpleAttributeOperand("Message");
filter<<QOpcUaSimpleAttributeOperand("Time");
const autoresponse= node->readHistoryEvents(QDateTime::currentDateTime().addDays(-2),QDateTime::currentDateTime(),filter, 10);
QObject::connect(response, &QOpcUaHistoryReadResponse::readHistoryEventsFinished, this,
[response](constQList<QOpcUaHistoryEvent> &results, QOpcUa::UaStatusCode serviceResult) {
if(serviceResult!= QOpcUa::UaStatusCode::Good) {
qDebug() << "Service call failed with" << serviceResult;
return;
}
// 输出目前获得的结果
for(const auto &result: response->events()) {
qDebug() << "Results for" << result.nodeId() << result.statusCode();
for(const auto &event: result.events())
qDebug() << " Event:" << event;
}
if(response->hasMoreData())
response->readMoreData();
});该函数在 Qt 6.7 中引入。
[since 6.3] QOpcUaHistoryReadResponse *QOpcUaNode::readHistoryRaw(const QDateTime &startTime, const QDateTime &endTime, quint32 numValues, bool returnBounds)
针对此节点启动读取历史数据请求。这是OPC UA 1.05第4部分5.10.3节中定义的用于读取原始历史数据的OPC UA读取历史(ReadHistory)服务的Qt OPC UA 表示形式。它根据以下参数读取历史数据: startTime 、endTime 、numValues 以及returnBounds 。
如果异步请求已成功分发,则返回一个QOpcUaHistoryReadResponse ,其中包含请求的状态。结果通过QOpcUaHistoryReadResponse::readHistoryDataFinished (const QList<QOpcUaHistoryData> &results, QOpcUa::UaStatusCode serviceResult)信号返回。
在下面的示例中,请求并打印了某个节点过去两天的历史数据。结果限制为每个节点最多十个值。
QOpcUaHistoryReadResponse*response = node->readHistoryRaw(QDateTime::currentDateTime(),
QDateTime::currentDateTime().addDays(-2),
10,
true);
if(response) {
QObject::connect(response123, &QOpcUaHistoryReadResponse::readHistoryDataFinished,
[](QList<QOpcUaHistoryData>results, QOpcUa::UaStatusCode serviceResult) {
if(serviceResult!= QOpcUa::UaStatusCode::Good) {
qWarning() << "Fetching historical data failed with:" << serviceResult;
}else{
for(const auto&result : results) {
qInfo() << "NodeId:" << result.nodeId();
for(const auto &dataValue: result.result())
qInfo() << "Value:" << dataValue.value();
}
}
});
}该函数在 Qt 6.3 中引入。
[since 6.7] QOpcUaHistoryReadResponse *QOpcUaNode::readHistoryRaw(const QDateTime &startTime, const QDateTime &endTime, quint32 numValues, bool returnBounds, QOpcUa::TimestampsToReturn timestampsToReturn)
为该节点发起读取历史记录请求。附加的timestampsToReturn 参数用于确定每个值将返回哪些时间戳。
该函数于 Qt 6.7 中引入。
bool QOpcUaNode::readValueAttribute()
启动针对节点 Value 属性的异步读取操作。
如果异步调用已成功分发,则返回true 。
另请参阅 readAttributes()。
bool QOpcUaNode::resolveBrowsePath(const QList<QOpcUaRelativePathElement> &path)
使用 OPC UA 1.05 第 4 部分 5.8.4 节中指定的 TranslateBrowsePathsToNodeIds 服务,将浏览路径path 解析为从该节点开始的一个或多个节点 ID。
如果异步调用已成功分发,则返回true 。
TranslateBrowsePathsToNodeIds 主要用于针对类型定义进行编程,而非针对 OPC UA 地址空间中的一组具体节点。例如,某台机器模型的类型定义可能包含一个浏览名为“Machine”的起始节点,该节点下有一个浏览名为“Fan”的组件。 该风扇有一个浏览名为“RPM”的组件,这是一个变量节点,用于存储风扇的当前转速(RPM)值。该类型的机器有多台,每台机器在 OPC UA 地址空间中都被映射为该机器类型的对象。 对于每个机器对象,从机器节点到“RPM”节点的路径都是相同的。如果客户端想要读取当前转速值,则需要调用 resolveBrowsePath() 方法,将机器节点作为起始节点,并传入从机器节点到“RPM”节点的浏览路径:
QScopedPointer<QOpcUaNode> node(opcuaClient->node("ns=1;s=machine1"));
QList<QOpcUaRelativePathElement> path;
path.append(QOpcUaRelativePathElement(QOpcUaQualifiedName(1, "Fan"), QOpcUa::ReferenceTypeId::HasComponent));
path.append(QOpcUaRelativePathElement(QOpcUaQualifiedName(1, "RPM"), QOpcUa::ReferenceTypeId::HasComponent));
node->resolveBrowsePath(path);resolveBrowsePathFinished() 返回的结果包含“RPM”节点的节点 ID,可用于访问该节点的属性:
if(!results.size()) {
qWarning() << "Browse path resolution failed";
return;
}
if(results.at(0).isFullyResolved()) {
QOpcUaNode*rpmNode = client->node(results.at(0).targetId());
if(!rpmNode) {
qWarning() << "Failed to create node";
return;
}
// 连接插槽,调用方法
}else{
qWarning() << "Browse path could not be fully resolved, the target node is on another server";
return;
}[signal] void QOpcUaNode::resolveBrowsePathFinished(QList<QOpcUaBrowsePathTarget> targets, QList<QOpcUaRelativePathElement> path, QOpcUa::UaStatusCode statusCode)
该信号在调用 `resolveBrowsePath()` 完成后触发。
QOpcUaBrowsePathTarget targets 其中包含匹配结果, 是该操作的状态码。如果 不为 ,则 为空。浏览路径 是请求中的浏览路径,可用于将结果与请求关联起来。statusCode statusCode Good targets path
QDateTime QOpcUaNode::serverTimestamp(QOpcUa::NodeAttribute attribute) const
返回attribute 自上次读取或数据更改以来的服务器时间戳。在至少发出一个attributeRead 或dataChangeOccurred 信号之前,将返回空的日期时间值。
QDateTime QOpcUaNode::sourceTimestamp(QOpcUa::NodeAttribute attribute) const
返回attribute 的最后一次读取或数据更改的源时间戳。在发出至少一个attributeRead 或dataChangeOccurred 信号之前,将返回一个空的datetime值。
QVariant QOpcUaNode::valueAttribute() const
返回节点 Value 属性的值。
返回值仅在“Value”属性成功读取或写入后,或者监控数据变更更新了属性缓存后才有效。这通过“attributeRead()”或“attributeWritten()”信号(状态码为Good )以及针对“Value”属性的“dataChangeOccurred()”信号来指示。
如果属性缓存中没有值,则返回无效的QVariant 。
另请参阅 readValueAttribute()、writeValueAttribute() 和valueAttributeError()。
QOpcUa::UaStatusCode QOpcUaNode::valueAttributeError() const
返回节点“Value”属性的错误代码。状态码Good 表示valueAttribute()的返回值有效。如果属性缓存中没有条目,则返回BadNoEntryExists 。
[signal, since 6.7] void QOpcUaNode::valueAttributeUpdated(const QVariant &value)
当属性缓存中的 value 属性因服务器发出的数据变更通知、读取操作或写入操作而被更新后,将触发此信号。value 包含 value 属性的新值。
该函数自 Qt 6.7 起引入。
另请参阅 attribute()、attributeError()、serverTimestamp()、sourceTimestamp() 以及attributeUpdated()。
bool QOpcUaNode::writeAttribute(QOpcUa::NodeAttribute attribute, const QVariant &value, QOpcUa::Types type = QOpcUa::Types::Undefined)
使用来自type 的类型信息,将value 写入attribute 中指定的属性。如果异步调用已成功分派,则返回true 。
如果省略了type 参数,后端会尝试查找正确的类型。将采用以下默认类型:
| Qt MetaType | OPC UA 类型 |
|---|---|
| Bool | 布尔 |
| UChar | 字节 |
| Char | SByte |
| UShort | UInt16 |
| Short | Int16 |
| Int | Int32 |
| UInt | UInt32 |
| ULongLong | UInt64 |
| LongLong | Int64 |
| Double | Double |
| Float | Float |
| QString | 字符串 |
| QDateTime | DateTime |
| QByteArray | 字节串 |
| QUuid | GUID |
bool QOpcUaNode::writeAttributeRange(QOpcUa::NodeAttribute attribute, const QVariant &value, const QString &indexRange, QOpcUa::Types type = QOpcUa::Types::Undefined)
使用来自type 的类型信息,将value 写入attribute 中指定的属性。关于indexRange ,请参阅readAttributeRange()。
如果异步调用已成功分发,则返回true 。
bool QOpcUaNode::writeAttributes(const QOpcUaNode::AttributeMap &toWrite, QOpcUa::Types valueAttributeType = QOpcUa::Types::Undefined)
对toWrite 中指定的属性和值执行写入操作。
如果异步调用已成功分发,则返回true 。
valueAttributeType 参数可用于为 value 属性提供类型信息。所有其他属性的类型均为已知类型。
另请参阅 writeAttribute()。
bool QOpcUaNode::writeValueAttribute(const QVariant &value, QOpcUa::Types type = QOpcUa::Types::Undefined)
使用来自type 的类型信息,将value 写入节点的 Value 属性。
如果异步调用已成功分派,则返回true 。
另请参阅 writeAttribute()。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.