当前位置: 首页 > news >正文

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()
http://www.cnnetsun.cn/news/4198584.html

相关文章:

  • 智能体自我进化新范式:协同进化与经验蒸馏技术解析
  • Catppuccin Palette API 参考全解析:flavors、colorEntries 与完整类型系统一次看懂
  • LLM智能体长期记忆安全:从攻击面分析到纵深防御实践
  • 时间序列分析实战:从ARIMA到LSTM的核心技术与避坑指南
  • scrcpy 5分钟把安卓投屏到电脑,免费免装App
  • REPENTOGON 安装实战指南:以撒脚本扩展器一次跑通
  • 数学规划模型实战指南:从核心组件到工作流程与排坑
  • 多智能体系统与涌现式学习:Moltbook项目中的AI协作与同伴学习模式
  • 12X速度提升:如何用Quantus批处理指标让Faithfulness指标计算快12倍
  • 美赛D题深度解析:从团队组建到多维度量化建模的实战指南
  • MAA明日方舟助手:全日常一键长草,把重复刷图彻底交给自动化
  • klog 使用教程:Go 层级日志完整指南,三分钟上手
  • OmenSuperHub完整指南:免费为暗影精灵笔记本解锁风扇控制、功耗限制与硬件监控
  • BT 下载总卡在 99%?trackerslist 公共 Tracker 清单配置实录
  • 英雄联盟Akari助手:免费开源,把赛前准备从半小时压到三分钟
  • Unity Hair System 完整指南:从导入到实时渲染的上手路径
  • kons-9动画系统完全指南:ANIMATOR、SHAPE-ANIMATOR与MOTION-GROUP时间轴调度详解
  • 性能提升的秘密:expo-app-template中启用React Compiler的完整指南
  • Kaitai Struct Compiler 表达式语言完全指南:条件、循环与方法调用如何驱动解析逻辑
  • OpenBoardView 安装指南:.brd 查看器 4 个平台 30 分钟跑通
  • 拆解Proton Pass安全中心:如何检测密码复用、弱密码与泄露风险的4步引擎
  • 智能体抽象推理新基准ARC-AGI-3:技术原理、实现路径与实战优化
  • 如何把QQ空间历史说说全部导出成Excel?GetQzonehistory备份完整教程
  • BlueToolkit Recon侦察模块详解:如何采集目标设备的蓝牙版本、厂商与配对能力
  • 具身智能TVA-VLA形态自适应与策略泛化机制
  • 从模板到泛型编程:核心原理、技术价值与实践应用
  • Maka Agent 技能目录预算机制完整解析:2% 上下文窗口如何实现懒加载
  • C++中std::move与std::forward的深度解析:从值类别到完美转发
  • 五分钟在小程序里渲染 HTML 与 Markdown:wxParse 富文本解析完整实战
  • Kaitai Struct Compiler 源码架构全解:Scala 实现的多语言二进制解析器生成器分层设计