使用opcua写入数组



大家好!

我需要将一个变量作为数组(列表)写入OPC服务器。我使用的是Python和Python OPC-UA。在图片中,您可以看到我试图写入数据的变量的名称和结构。

操作码图像

我尝试使用此代码并获得错误

q = [0]*50
data = client.get_node(f'ns=3;s="OSC_Profile"."THIS"')
dv = ua.Variant(q, ua.VariantType.ExtensionObject)
data.set_value(dv)
KeyError                                  Traceback (most recent call last)
Input In [18], in <cell line: 9>()
20     dv = ua.Variant(q, ua.VariantType.ExtensionObject)
---> 21     data.set_value(dv)
KeyError: 'int'

如果有人能提出正确的方式,我会很高兴

好的,您有一个自定义的数据类型,所以您必须创建相应的类:

q = [ua.OSC(...), ua.OSC(...), ...]
data.set_value(ua.Variant(q, ua.VariantType.ExtensionObject)) 

感谢@Schroeder。我的最终代码看起来像:

from opcua import Client 
from opcua import ua
from opcua.common.type_dictionary_buider import get_ua_class
try:
client.connect()
client.load_type_definitions()
data = client.get_node(f'adress')
osc = get_ua_class('OSC')()
osc.Id= 50.
osc.Age= 60.
osc.Weight= 70.
q = [osc]*50
dv = ua.Variant(q, ua.VariantType.ExtensionObject)
data.set_attribute(13, ua.DataValue(dv)) #13 - id of attribute
finally:
client.disconnect()

最新更新