1

我正在尝试使用和将值写入某些OPC UA节点。为此,我必须知道节点的不同数据类型。每次我尝试读取节点的数据类型时,我都会得到 a而不是. 它是列表中的正确节点,我可以读取所有节点。有人可以帮帮我吗?Qtopen62541Boolean typeint32

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

我只能将布尔节点写为数据类型,因为每个节点都有一个布尔值作为数据类型。

4

1 回答 1

0

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

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;
    }
}
于 2019-08-29T11:13:44.260 回答