FreeOpcUa/opcua-asyncio

How to use browsepath to get the value of node id.

codedebugger1 opened this issue · 8 comments

I am have a question on top of this answer:

you can only use nodeids for direct read!
the only thing you can do use the translate browsepath to nodeid service to get the nodeid and then read.

Originally posted by @AndreasHeine in #1228 (comment)

Basically our need is how we can read and write the values via browse name.
How to use the browse path in that case.

async def translate_browsepaths(self, starting_node: ua.NodeId, relative_paths: Iterable[Union[ua.RelativePath, str]]) -> List[ua.BrowsePathResult]:

https://reference.opcfoundation.org/Core/Part4/v105/docs/A.2

rel path: "/0:Root/0:Objects/0:Server" (you need to use Browsenames!!! <NamespaceIndex>:<Name>)
remember the path is relative to the starting node!

Thanks for your response.

I have tried with below code and couldn't succeded.
I have tried with relative path only.

 async def main():
 client = Client(url=address, timeout=4)

try:
    await client.connect()
    print("Success")
except Exception as e:
    print(e)
    return

paths = await client.translate_browsepaths(starting_node="ns=1;i=100", relative_paths="/0:Root/0:Objects/1:MyServer/1:StatusWord")
print("paths", paths)

try:
    await client.disconnect()
except Exception as e:
    print(e)
    return


 if __name__ == "__main__":
      asyncio.run(main())

We wanted something like this (below is the example of the reading value from node id, we wanted similar with reading value from browsepath ):

    client.connect()
    print("Connect Success")
    node_obj= client.get_node("ns=1;i=100")
    node_value = node_obj.get_value()
    print("node_value", node_value)

Thanks for your response.

I have tried with below code and couldn't succeded. I have tried with relative path only.

 async def main():
 client = Client(url=address, timeout=4)

try:
    await client.connect()
    print("Success")
except Exception as e:
    print(e)
    return

paths = await client.translate_browsepaths(starting_node="ns=1;i=100", relative_paths="/0:Root/0:Objects/1:MyServer/1:StatusWord")
print("paths", paths)

try:
    await client.disconnect()
except Exception as e:
    print(e)
    return


 if __name__ == "__main__":
      asyncio.run(main())

We wanted something like this (below is the example of the reading value from node id, we wanted similar with reading value from browsepath ):

    client.connect()
    print("Connect Success")
    node_obj= client.get_node("ns=1;i=100")
    node_value = node_obj.get_value()
    print("node_value", node_value)

sorry in opc ua its two seperate services means always two seperate requests...

not sure if root and object belongs to a "relative" path!? i used it just to show the syntax...

its usually from the startnode the path to the node you want to get

guess this is your rel path: /1:StatusWord if "ns=1;i=100" is startnode

Yes my start node is "ns=1;i=100"

And I have tried with the relative_path as "/1:StatusWord" as below:

        asyncua_obj = asyncuaClient(address)
        asyncua_obj.connect()
        paths = asyncua_obj.translate_browsepaths(starting_node=node_address, relative_paths="/1:StatusWord")
        print("paths", paths)

Below is the response I got in the paths:

image

are you familiar with asyncio?

        asyncua_obj = asyncuaClient(address)
        await asyncua_obj.connect()
        paths = await asyncua_obj.translate_browsepaths(starting_node=node_address, relative_paths="/1:StatusWord")
        print("paths", paths)

you need to await the connect and the translate method call...

async def translate_browsepaths(self, starting_node: ua.NodeId, relative_paths: Iterable[Union[ua.RelativePath, str]]) -> List[ua.BrowsePathResult]:

as in the type hint its a ua.NodeId and not a string!

node_address = ua.NodeId.from_string("ns=1;i=100")

async def connect():
       asyncua_obj = asyncuaClient(address)
       await asyncua_obj.connect()
       paths = await asyncua_obj.translate_browsepaths(starting_node=node_address, relative_paths="/1:StatusWord")
       print("paths", paths)
       await asyncua_obj.disconnect()

asyncio.run(connect())