Freeopcua C++ 客户端和 Python Opcua 的组合会在 getChild() 上抛出错误

combination of freeopcua c++ client and python opcua throws error on getChild()

本文关键字:getChild 错误 出错 客户端 C++ Python Opcua 组合 Freeopcua      更新时间:2023-10-16

我在 debian 上使用freeopcua c++(从 2019 年 10 月开始的主分支(和python-opcua/stable 0.98.6-2

当试图通过以下方式获得孩子时:

root.GetChild(std::vector<std::string>{"0:Objects", "2:MyObject", "2:MyVariable"});

server-minimal.py示例中访问服务器时,我收到状态代码0x806f0000 = BadNoMatch错误(见下文(。

如果我这样做:

root.GetChildren()[0].GetChildren()[0].GetChildren()[0]

亲自挑选哪个孩子合适,我就能得到孩子。

这也适用于:

auto node = m_uaclient->GetNode(OpcUa::NodeId(2,2));

我的服务器代码只是python opcua的普通 server-minimal.py:

import sys
sys.path.insert(0, "..")
import time

from opcua import ua, Server

if __name__ == "__main__":
# setup our server
server = Server()
server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
# setup our own namespace, not really necessary but should as spec
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()
try:
count = 0
while True:
time.sleep(1)
count += 0.1
myvar.set_value(count)
finally:
#close connection, remove subcsriptions, etc
server.stop()

客户端如下:

m_uaclient = boost::make_unique<OpcUa::UaClient>();
m_uaclient->Connect("opc.tcp://localhost:4840/freeopcua/server/");
OpcUa::Node root = m_uaclient->GetRootNode();
root.GetChild(std::vector<std::string>{"0:Objects", "2:MyObject", "2:MyVariable"});

有人知道问题可能是什么,或者我如何继续缩小问题范围吗?

希望它能帮助您缩小问题范围。

该错误意味着:

0x806F0000:("BadNoMatch"、">请求的操作没有要返回的匹配项"。

参考此示例代码后,我发现您可能混合或缺少某些步骤。以下是一些建议:

节点
  1. 对象具有读取和写入节点属性以及浏览或填充地址空间的方法。

    print("Children of root are: ", root.get_children())
    
  2. 获取知道其节点 ID 的特定节点

    var = client.get_node(ua.NodeId(1002, 2))
    var = client.get_node("ns=3;i=2002")
    print(var)
    var.get_data_value() # get value of node as a DataValue object
    var.get_value() # get value of node as a python builtin
    var.set_value(ua.Variant([23], ua.VariantType.Int64)) #set node value using 
    explicit data type
    var.set_value(3.9) # set node value using implicit data type
    
  3. 现在使用其浏览路径获取变量节点

    myvar = root.get_child(["0:Objects", "2:MyObject", "2:MyVariable"])
    obj = root.get_child(["0:Objects", "2:MyObject"])
    print("myvar is: ", myvar)
    print("myobj is: ", obj)
    
  4. 堆叠的myvar访问

    print("myvar is: ", root.get_children()[0].get_children()[1].get_variables()[0].get_value())