Python-opcua编程(5)-数据变化通知(datachange_notification)

        在OPCUA服务器端,当数据或者变化时,要做一些处理。例如:在聚合服务器或者网关程序中,当客户端修改某一个变量时,需要将修改值传递给底层服务器或者现场总线设备(比如modbus设备。

      这是一种中继方式。SubHanlder 相当于AfterWriteCallback 程序。

演示代码

import sys
sys.path.insert(0, "..")
import time
from opcua import  Server
class SubHandler(object):
   def datachange_notification(self, node, val, data):
       print(node.get_browse_name().Name+" DataChanged")
       pass
if __name__ == "__main__":

    # setup our server
    server = Server()
    server.set_endpoint("opc.tcp://127.0.0.1:48400/freeopcua/server/")
    uri = "http://examples.freeopcua.github.io"
    idx = server.register_namespace(uri)

    # get Objects node, this is where we should put our nodes
    objects = server.get_objects_node()

    # populating our address space
    myobj = objects.add_object(idx, "MyObject")
    myvar = myobj.add_variable(idx, "MyVariable", 6.7)
    myvar.set_writable()    # Set MyVariable to be writable by clients
    
    # starting!
    server.start()
    handler = SubHandler()
    sub = server.create_subscription(100, handler)
    sub.subscribe_data_change(myvar)
    try:
        count = 0
        while True:
            time.sleep(1)
            count += 0.1
            myvar.set_value(count)
    finally:
        #close connection, remove subcsriptions, etc
        server.stop()

在上面的程序中,使用下列函数实现订阅

subscribe_data_change(nodesattr=<AttributeIds.Value: 13>queuesize=0)

nodes可以为一个node 也可以为以节点的数组,例如

sub.subscribe_data_change([myvar1,myvar2])

PythonOPC UApython-opcua)中,如果你想要创建一个订阅并打印接收到的数据,你需要遵循以下步骤: 首先,确保已经安装了`python-opcua`,如果没有,可以使用pip安装: ```bash pip install python-opcua ``` 然后,你可以通过以下代码示例来创建一个订阅并接收数据: ```python import opcua # 创建opcua客户端连接 client = opcua.Client("opc.tcp://your_server_address:4840/") # 如果需要,登录到服务器 if client.connect(): print(f"Connected to {client.server_info.ServerName}") # 导航到你要订阅的节点(例如,一个变量或数据流) node_to_sub = client.get_node("ns=2;i=1") # 用实际的节点路径替换这里的节点ID # 创建订阅 subscription = client.create_subscription(500) # 设置更新周期,单位毫秒 subscription_data_change_notification_handler = opcua.DataChangeNotificationHandler() # 定义事件处理函数,这里只是一个基础示例,将接收到的数据打印出来 def handle_datachange_notification(datachange_notification): for changed_item in datachange_notification.data_changes: if changed_item.is.changed: print(f"{changed_item.node.NodeId} has changed: {changed_item.new_value.Value}") # 添加事件处理器 subscription_data_change_notification_handler.notification += handle_datachange_notification # 开始订阅 subscription.start() try: # 这里阻塞程序直到用户主动停止订阅 input("Press enter to stop the subscription") except KeyboardInterrupt: pass # 停止订阅 subscription.unsubscribe() subscription.delete_session() # 关闭连接 client.disconnect() else: print("Failed to connect.") ``` 在这个例子中,你需要替换`"opc.tcp://your_server_address:4840/"`为你实际的OPC UA服务器地址,并提供正确的节点ID(ns=2;i=1)以订阅。运行此脚本,你会看到接收到的数据变化
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值