FreeOpcUa / opcua-asyncio

OPC UA library for python >= 3.7
GNU Lesser General Public License v3.0
1.04k stars 345 forks source link

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

Open codedebugger1 opened 3 days ago

codedebugger1 commented 3 days ago

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 https://github.com/FreeOpcUa/opcua-asyncio/discussions/1228#discussioncomment-5084140

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

AndreasHeine commented 3 days ago

https://github.com/FreeOpcUa/opcua-asyncio/blob/cf27f8cf19e3081317f871550af89c1af28e18df/asyncua/client/client.py#L957

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!

codedebugger1 commented 3 days ago

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)
AndreasHeine commented 3 days ago

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...

AndreasHeine commented 3 days ago

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

codedebugger1 commented 3 days ago

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

AndreasHeine commented 3 days ago

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...

AndreasHeine commented 2 days ago

https://github.com/FreeOpcUa/opcua-asyncio/blob/cf27f8cf19e3081317f871550af89c1af28e18df/asyncua/client/client.py#L957

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())
AndreasHeine commented 2 days ago

https://github.com/FreeOpcUa/opcua-asyncio/blob/master/tests/test_client.py#L163-L175