如何在Python中使用opcua订阅对象节点?

问题描述 投票:0回答:1

我想订阅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)
python object variables opc-ua
1个回答
0
投票

您无法订阅对象以进行数据更改。如果您想获取子变量的所有更改,您可以这样做:

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)
© www.soinside.com 2019 - 2024. All rights reserved.