news 2026/8/24 18:01:30

OPC UA设置事件节点

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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 "ns=4;i=1000", "ns=0;i=2267", "ns=0;i=2259", ] 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(url=server_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("ns=0;i=2267").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()
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/24 17:59:01

智能体自我进化新范式:协同进化与经验蒸馏技术解析

1. 项目概述:从静态执行到动态进化的智能体新范式最近在智能体(Agent)领域,一个名为“Mem$^2$Evolve”的概念讨论度很高。简单来说,它探讨的是一种能让智能体像生物一样,在运行过程中不断自我进化、自我完善…

作者头像 李华
网站建设 2026/8/24 17:57:51

LLM智能体长期记忆安全:从攻击面分析到纵深防御实践

1. 从“记忆”到“软肋”:为什么LLM智能体的长期记忆安全成了新战场 最近和几个做AI应用落地的朋友聊天,大家不约而同地提到了一个痛点:那些能记住用户偏好、持续学习的智能助手,用起来是真爽,但心里也是真没底。一个能…

作者头像 李华
网站建设 2026/8/24 17:56:11

时间序列分析实战:从ARIMA到LSTM的核心技术与避坑指南

1. 项目概述:从数据噪声中捕捉时间的脉搏 在数据驱动的决策时代,我们每天都会接触到海量的时序数据:从股票市场的每分钟波动、电商平台的每日销售额、工厂设备的实时传感器读数,到城市每小时的空气质量指数。这些数据点按照时间顺…

作者头像 李华
网站建设 2026/8/24 17:54:36

scrcpy 5分钟把安卓投屏到电脑,免费免装App

scrcpy 5分钟把安卓投屏到电脑,免费免装App 【免费下载链接】scrcpy Display and control your Android device 项目地址: https://gitcode.com/GitHub_Trending/sc/scrcpy scrcpy 是一款免费开源工具,把安卓手机屏幕实时投屏到电脑,并…

作者头像 李华
网站建设 2026/8/24 17:53:55

REPENTOGON 安装实战指南:以撒脚本扩展器一次跑通

REPENTOGON 安装实战指南:以撒脚本扩展器一次跑通 【免费下载链接】REPENTOGON Script extender for The Binding of Isaac: Repentance 项目地址: https://gitcode.com/gh_mirrors/re/REPENTOGON REPENTOGON 是《以撒的结合:忏悔》的 Lua 脚本扩展…

作者头像 李华