如何获取QOpcUaNode类型属性以在客户端进行写入

huangapple go评论65阅读模式
英文:

How to get QOpcUaNode type attribute for writing in a client

问题

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.

我发现这可以工作,但我的一些变量是UInt16,而其他变量是UInt32,我想从节点实例中获取节点变量类型,但不确定如何实现。

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.

我认为QOpcUaNode对象应该知道它的数据类型,因此我不需要单独跟踪它。

Our runtime is Qt 5.15.2 / 5.15.3

我们的运行时环境是Qt 5.15.2 / 5.15.3

英文:

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

得分: 1

这在我看来是一个权宜之计,但现在可以使用。很想听听更清晰的方法。

// 使用属性值类型来解析数据类型
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:
        // 在支持 QVariant::UShort 之前
        dataType = QOpcUa::Types::UInt16;
        break;
}
QVariant valueVariant(value);
opcUaNode->writeValueAttribute(valueVariant, dataType);

派生自:
https://stackoverflow.com/questions/57694122/reading-datatype-from-node-attribute

英文:

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:
https://stackoverflow.com/questions/57694122/reading-datatype-from-node-attribute

huangapple
  • 本文由 发表于 2023年2月16日 05:57:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定