DeaD_EyE

opcua example with asyncua

Oct 27th, 2020
43
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import asyncio
  2. import pprint
  3. from asyncua import Client
  4. from asyncua.ua import DataValue
  5.  
  6.  
  7. def make_node_path(namespace, path):
  8.     parts = ".".join(f'"{part}"' for part in path.split("."))
  9.     return f"ns={namespace};s={parts}"
  10.  
  11.  
  12. async def main(paths):
  13.     values = {}
  14.     async with Client("opc.tcp://192.168.10.10:4840/") as client:
  15.         ns = await client.get_namespace_index("http://www.siemens.com/simatic-s7-opcua")
  16.         for path in paths:
  17.             node_path = make_node_path(ns, path)
  18.             node = client.get_node(node_path)
  19.             values[node_path] = await node.get_value()
  20.     return values
  21.  
  22.  
  23. if __name__ == "__main__":
  24.     nodes = ["DataExchange.operator_panel.S1", "I0", "I1", "I2"]
  25.     coro = main(nodes)
  26.     result = asyncio.run(coro)
  27.     pprint.pprint(result, sort_dicts=False, compact=True, indent=4)
  28.  
RAW Paste Data