qtc++11opc-uaopen62541

Reading Datatype from Node Attribute


I'm trying to write values to some OPC UA nodes with Qt and open62541. For that I have to know the different data types of the nodes. Every time I try to read the data type of a node, I get a Boolean type instead of an int32. It is the correct node in the list and I can read all nodes. Can someone please help me?

//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>());
}

I can only write boolean nodes as a datatype, because each node has a boolean value as a datatype.


Solution

  • I found a solution for my case, but I'm not happy with that. Because I can't distinguish a 16 bit Integer and a 32 bit 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;
        }
    }