OPC UA调用节点方法报BadNoMatch错误,求解决方案
OPC UA方法调用报错BadNoMatch排查方案
问题描述
调用OPC UA节点方法时失败,多次调整输入仍报错,代码如下:
import asyncio from asyncua import Client url = "opc.tcp://localhost:4840" async def main(): print(f"Connecting to {url} ...") async with Client(url=url) as client: # Calling a method idname = f"ns=1;s=getCurrentAbsTime" var2 = client.get_node(idname) res = await var2.call_method("getCurrentAbsTime",[]) print(res) if __name__ == "__main__": asyncio.run(main())
报错信息:
asyncua.ua.uaerrors._auto.BadNoMatch: The requested operation has no match to return.(BadNoMatch)
排查与解决步骤
1. 核心问题:调用逻辑矛盾
你当前代码存在逻辑错误:已经通过client.get_node(idname)获取了方法节点,却又调用call_method("getCurrentAbsTime",[])——这相当于在该方法节点下查找名为getCurrentAbsTime的子方法,显然不存在,直接触发BadNoMatch报错。
2. 两种正确调用方式
方式一:直接调用方法节点
如果ns=1;s=getCurrentAbsTime是方法本身的NodeId,直接调用该节点的call_method即可,无需额外传入方法名:
import asyncio from asyncua import Client url = "opc.tcp://localhost:4840" async def main(): print(f"Connecting to {url} ...") async with Client(url=url) as client: # 方法的准确NodeId method_node = client.get_node("ns=1;s=getCurrentAbsTime") # 无参数则传空列表/元组,有参数按定义顺序传入 res = await method_node.call_method([]) print(res) if __name__ == "__main__": asyncio.run(main())
方式二:通过父对象节点调用方法
OPC UA方法通常依附于对象节点,若你拿到的是方法所属对象的NodeId,需传入方法的BrowseName进行调用:
import asyncio from asyncua import Client url = "opc.tcp://localhost:4840" async def main(): print(f"Connecting to {url} ...") async with Client(url=url) as client: # 方法所属父对象的NodeId obj_node = client.get_node("ns=1;s=YourParentObjectNode") # 传入方法的BrowseName和参数列表 res = await obj_node.call_method("getCurrentAbsTime", []) print(res) if __name__ == "__main__": asyncio.run(main())
3. 额外验证要点
- 核对NodeId准确性:用UaExpert等OPC UA客户端工具连接服务器,浏览地址空间,确认
getCurrentAbsTime方法的NodeId、命名空间索引(ns=1是否正确)及所属父节点。 - 检查参数要求:确认方法是否需要传入参数,空列表
[]是否符合方法定义;若无需参数,可省略参数或传空元组()。
内容的提问来源于stack exchange,提问作者baixianjie
相关产品推荐
相关产品推荐

