How to get QOpcUaNode type attribute for writing in a client

68 Views Asked by At

How do I read the QOpcUaNode data type attribute correctly?

I would like to do this but am finding this does not work against our server.

opcUaNode->writeValueAttribute(variant);

I have found this to work but some of my variables are UInt16 and others are UInt32 and I'd like to get the node variable type from the node instance but am not sure how.

opcUaNode->writeValueAttribute(variant, QOpcUa::Types::UInt16);

I have found this is not the way to get the node data type.

QVariant dataType = opcUaNode->attribute(QOpcUa::NodeAttribute::DataType);

I believe the QOpcUaNode object should know what data type it is so I should not need to track it separately.

Our runtime is Qt 5.15.2 / 5.15.3

1

There are 1 best solutions below

0
On

This is a workaround IMO but usable for now. Would love to hear of a cleaner approach.

    // use attribute value type to resolve the datatype
    QVariant::Type aValueType = opcUaNode->attribute(QOpcUa::NodeAttribute::Value).type();
    QOpcUa::Types dataType;
    switch (aValueType)
    {
        case QVariant::ULongLong:
            dataType = QOpcUa::Types::UInt64;
        break;
        case QVariant::UInt:
            dataType = QOpcUa::Types::UInt32;
        break;
        default:
            // until support for QVariant::UShort is available
            dataType = QOpcUa::Types::UInt16;
        break;
    }
    QVariant valueVariant(value);
    opcUaNode->writeValueAttribute(valueVariant, dataType);

Derived from: Reading Datatype from Node Attribute