尧图网络科技YAOTU DIGITAL 获取报价
获取报价
首页 / 资讯中心 / 文章详情

OPC UA设置事件节点

发布时间:2026/9/20 19:13:14

资讯中心
01
ARTICLE

OPC UA设置事件节点

OPC UA设置事件节点
参考链接Question about Python OPCUA session timeout and client.import asyncio, json from asyncua import Client, ua, Node from asyncua.common.events import Event from datetime import datetime #################################################################################### # Globals: #################################################################################### # OPC UA Client server_url opc.tcp://192.168.10.1:4840 nodes_to_subscribe [ #node-id ns4;i1000, ns0;i2267, ns0;i2259, ] events_to_subscribe [ #(eventtype-node-id, event-node-id) ] #################################################################################### # OpcUaClient: #################################################################################### class SubscriptionHandler: The SubscriptionHandler is used to handle the data that is received for the subscription. def datachange_notification(self, node: Node, val, data): Callback for asyncua Subscription. This method will be called when the Client received a data change message from the Server. print(node, val, data) def event_notification(self, event: Event): called for every event notification from server print(event) def status_change_notification(self, status): called for every status change notification from server print(status) async def opcua_client(): -handles connect/disconnect/reconnect/subscribe/unsubscribe -connection-monitoring with cyclic read of the service-level client Client(urlserver_url) handler SubscriptionHandler() subscription None case 0 subscription_handle_list [] while 1: if case 1: #connect print(connecting...) try: await client.connect() await client.load_type_definitions() print(connected!) case 2 except: print(connection error!) case 1 await asyncio.sleep(5) elif case 2: #subscribe all nodes and events print(subscribing nodes and events...) try: subscription await client.create_subscription(50, handler) subscription_handle_list [] if nodes_to_subscribe: for node in nodes_to_subscribe: handle await subscription.subscribe_data_change(client.get_node(node)) subscription_handle_list.append(handle) if events_to_subscribe: for event in events_to_subscribe: handle await subscription.subscribe_events(event[0], event[1]) subscription_handle_list.append(handle) print(subscribed!) case 3 except: print(subscription error) case 4 await asyncio.sleep(0) elif case 3: #running read cyclic the service level if it fails disconnect and unsubscribe wait 5s connect try: service_level await client.get_node(ns0;i2267).get_value() if service_level 200: case 3 else: case 4 await asyncio.sleep(5) except: case 4 elif case 4: #disconnect clean unsubscribe, delete subscription then disconnect print(unsubscribing...) try: if subscription_handle_list: for handle in subscription_handle_list: await subscription.unsubscribe(handle) await subscription.delete() print(unsubscribed!) except: print(unsubscribing error!) subscription None subscription_handle_list [] await asyncio.sleep(0) print(disconnecting...) try: await client.disconnect() except: print(disconnection error!) case 0 else: #wait case 1 await asyncio.sleep(5) #################################################################################### # Run: #################################################################################### if __name__ __main__: asyncio.ensure_future(opcua_client()) asyncio.get_event_loop().run_forever()
02
RELATED NEWS

相关资讯

更多网站建设与数字化升级内容

03
WHY YAOTU

想打造同款高转化官网?

懂行业、懂生意,从建站到增长一站式陪跑

场景化定制

不做模板站,围绕你的业务场景量身设计,小众不撞款。

营销型架构

以转化目标组织内容与路径,让官网真正带来询盘。

全周期服务

设计、开发、运营、运维一体,上线只是开始。

免费获取你的建站方案

留下需求,专属顾问 24 小时内为你输出方案建议。