You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python OPC UA Client:通过BrowseName写入变量的语法问题

OPC UA通过BrowseName写入变量时出现BadNodeIdUnknown错误

无法找到通过BrowseName为变量赋值的正确语法,当前以布尔变量flag1做调试测试,目标是实现包括数组在内的所有变量的写入操作。使用索引编号时能正常工作,但调用自定义write_value_bool函数,传入类似ns=4;s="opcData"."flag1"的Node ID写入值时,出现BadNodeIdUnknown错误,提示该节点ID在服务器地址空间中不存在。相关代码及错误信息如下:

测试代码

import pyOPCClient as opc

client = opc.opcConnect('192.168.5.10')

opc.write_value_bool(client, 'ns=4;s="opcData"."flag1"', True)

client.disconnect()

布尔值写入函数

##### Function to WRITE a Boolean into Bool Object Variable - Requires Object Name #####
def write_value_bool(client, node_id, value):
    client_node = client.get_node(node_id)  # get node
    client_node_value = value
    client_node_dv = ua.DataValue(ua.Variant(client_node_value, ua.VariantType.Boolean))
    client_node.set_value(client_node_dv)
    print("Value of : " + str(client_node) + ' : ' + str(client_node_value))

错误信息

PS C:\Users\ALEMAC\Documents\Python Scripts> & C:/ProgramData/Anaconda3/python.exe "c:/Users/ALEMAC/Documents/Python Scripts/opctest.py"
Requested session timeout to be 3600000ms, got 30000ms instead
Traceback (most recent call last):
  File "c:\Users\ALEMAC\Documents\Python Scripts\opctest.py", line 5, in <module>
    opc.write_value_bool(client, 'ns=4;s="opcData"."flag1"', True)
  File "c:\Users\ALEMAC\Documents\Python Scripts\pyOPCClient.py", line 49, in write_value_bool
    client_node.set_value(client_node_dv)
  File "c:\Users\ALEMAC\Documents\Python Scripts\opcua\common\node.py", line 217, in set_value
    self.set_attribute(ua.AttributeIds.Value, datavalue)
  File "c:\Users\ALEMAC\Documents\Python Scripts\opcua\common\node.py", line 263, in set_attribute
    result[0].check()
  File "c:\Users\ALEMAC\Documents\Python Scripts\opcua\ua\uatypes.py", line 218, in check
    raise UaStatusCodeError(self.value)
opcua.ua.uaerrors._auto.BadNodeIdUnknown: "The node id refers to a node that does not exist in the server address space."(BadNodeIdUnknown)

附TIA Portal V17 OPC UA - S7-1212及UA Expert Client截图。

内容的提问来源于stack exchange,提问作者AlexMacabu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 20:01:33