-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathclient-minimal.py
31 lines (22 loc) · 997 Bytes
/
client-minimal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import asyncio
from asyncua import Client
url = "opc.tcp://localhost:4840/freeopcua/server/"
namespace = "http://examples.freeopcua.github.io"
async def main():
print(f"Connecting to {url} ...")
async with Client(url=url) as client:
# Find the namespace index
nsidx = await client.get_namespace_index(namespace)
print(f"Namespace Index for '{namespace}': {nsidx}")
# Get the variable node for read / write
var = await client.nodes.root.get_child(f"0:Objects/{nsidx}:MyObject/{nsidx}:MyVariable")
value = await var.read_value()
print(f"Value of MyVariable ({var}): {value}")
new_value = value - 50
print(f"Setting value of MyVariable to {new_value} ...")
await var.write_value(new_value)
# Calling a method
res = await client.nodes.objects.call_method(f"{nsidx}:ServerMethod", 5)
print(f"Calling ServerMethod returned {res}")
if __name__ == "__main__":
asyncio.run(main())