Advertisement
DeaD_EyE

Access via asyncua Tags on a sImulated MTP700

May 19th, 2025
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. import asyncio
  2. import typing
  3. from contextlib import asynccontextmanager
  4.  
  5.  
  6. from asyncua import Client
  7.  
  8.  
  9. @asynccontextmanager
  10. async def connector(client: Client):
  11.     client.session_timeout = 600000
  12.     await client.connect()
  13.     yield client
  14.     await client.disconnect()
  15.  
  16.  
  17. async def get_value(hmi_opcua: Client, tag: str) -> typing.Any:
  18.     root = hmi_opcua.get_root_node()
  19.     tags = await root.get_child(["0:Objects", "1:HmiRuntime", "1:HMI_RT_4", "1:Tags"])
  20.     for child in await tags.get_children():
  21.         name = (await child.read_browse_name()).Name.split("::")[1]
  22.         if name == tag:
  23.             return await child.get_value()
  24.  
  25.     raise ValueError(f"Could not find {tag}")
  26.  
  27.  
  28. async def main():
  29.     async with connector(Client(url="opc.tcp://127.0.0.1:4949")) as client:
  30.         for _ in range(10):
  31.             print("Tag in position:", await get_value(client, "Tag_in_pos"))
  32.             await asyncio.sleep(0.1)
  33.  
  34.  
  35. if __name__ == "__main__":
  36.     asyncio.run(main())
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement