【发布时间】:2020-01-01 18:47:10
【问题描述】:
我正在尝试使用Qt 和open62541 将值写入一些OPC UA 节点。为此,我必须知道节点的不同数据类型。每次我尝试读取节点的数据类型时,都会得到Boolean type 而不是int32。它是列表中的正确节点,我可以读取所有节点。有人可以帮帮我吗?
//This is how I add nodes to the server. This is an example with the node DBW0.
//After adding the nodes to the server, each node will be append to the _nodeList.
void OPCConnection::AddNodeToServer()
{
QOpcUaNodeCreationAttributes attributes;
attributes.setDataTypeId(QOpcUa::namespace0Id(QOpcUa::NodeIds::Namespace0::Int16));
attributes.setValueRank(-2); // Scalar or array
attributes.setAccessLevel(QOpcUa::AccessLevelBit::CurrentWrite);
attributes.setUserAccessLevel(QOpcUa::AccessLevelBit::CurrentWrite);
QOpcUaAddNodeItem item;
item.setParentNodeId(QOpcUa::QExpandedNodeId("ns=2;s=PLC1.S7_300.DB120"));
item.setReferenceTypeId(QOpcUa::nodeIdFromReferenceType(QOpcUa::ReferenceTypeId::Organizes));
item.setRequestedNewNodeId(QOpcUa::QExpandedNodeId("ns=2;s=DBW0"));
item.setNodeClass(QOpcUa::NodeClass::Variable);
item.setNodeAttributes(attributes);
_client->addNode(item);
}
//This is how I read the nodes.
void OPCConnection::readNode()
{
if (_client->state() == QOpcUaClient::ClientState::Connected)
{
for (int i = 0; i < _nodeList->count(); i++)
{
_nodeList->at(i)->readAttributes(QOpcUa::NodeAttribute::DataType);
_nodeList->at(i)->readAttributes(QOpcUa::NodeAttribute::Value);
}
}
}
//After reading I want to write.
void OPCConnection::setNodeValue(const QVariant value, const int index)
{
_nodeList->at(index)->writeValueAttribute(value,
_nodeList->at(index)->attribute(QOpcUa::NodeAttribute::DataType).
value<QOpcUa::Types>());
}
我只能将布尔节点写为数据类型,因为每个节点都有一个布尔值作为数据类型。
【问题讨论】:
-
没有任何 mcve (stackoverflow.com/help/minimal-reproducible-example) 我们无法为您提供更详细的帮助。您的代码只是告诉我们,您正在读取的节点是布尔类型。您能否提供一个小代码示例来说明如何重现您的问题?
-
@StefanProfanter 现在您可以查看更多代码来了解我的问题。