【问题标题】:Reading Datatype from Node Attribute从节点属性读取数据类型
【发布时间】:2020-01-01 18:47:10
【问题描述】:

我正在尝试使用Qtopen62541 将值写入一些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 现在您可以查看更多代码来了解我的问题。

标签: qt c++11 opc-ua open62541


【解决方案1】:

我找到了适合我的情况的解决方案,但我对此并不满意。 因为我无法区分 16 位 Integer 和 32 位 Integer。

void OPCConnection::setNodeValue(const QVariant value, const int index)
{
    _nodeList->at(index)->writeValueAttribute(value, 
    selectType(_nodeList->at(index)->attribute(QOpcUa::NodeAttribute::Value).type()));
}

QOpcUa::Types OPCConnection::selectType(const QVariant::Type type)
{
    switch (type)
    {
        case QVariant::Bool:
            return QOpcUa::Types::Boolean;
        case QVariant::UInt:
            return QOpcUa::Types::UInt32;
        default:
            return QOpcUa::Types::UInt16;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 2017-11-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多