我想订阅Python中的对象节点。 我想输出datachange_notification中对象节点的变量节点的值。可以吗?
如果有办法请告诉我。 我尝试如下,但处理程序无法正常工作。
from opcua import Client
client = Client("opc.tcp://1111.1111.1111.1111:1000")
client.connect()
class SubHandler(object):
def datachange_notification(self, node, val, data):
# print client.get_node("ns=2;ABC.T1") # variable node
# print client.get_node("ns=2;ABC.T2") # variable node
# print client.get_node("ns=2;ABC.T3") # variable node
node = client.get_node("ns=2;ABC") # object node
nodes = []
nodes.append(node)
handler = SubHandler()
subscription = client.create_subscription(500, handler)
handle = subscription.subscribe_data_change(nodes)
您无法订阅对象以进行数据更改。如果您想获取子变量的所有更改,您可以这样做:
from opcua import Client
client = Client("opc.tcp://1111.1111.1111.1111:1000")
client.connect()
class SubHandler(object):
def datachange_notification(self, node, val, data):
print(node, val)
node = client.get_node("ns=2;ABC") # object node
nodes = node.get_children(nodeclassmask=ua.NodeClass.Variable) # Collect all child variables
handler = SubHandler()
subscription = client.create_subscription(500, handler)
handle = subscription.subscribe_data_change(nodes)
while 1:
# Stay connected to recive datachange notifications
time.sleep(1.0)