1 | import sys |
||
2 | sys.path.insert(0, "..") |
||
3 | import time |
||
4 | import logging |
||
5 | |||
6 | from asyncua.sync import Client, start_thread_loop, stop_thread_loop |
||
7 | from asyncua import ua |
||
8 | |||
9 | |||
10 | class SubHandler(object): |
||
11 | |||
12 | """ |
||
13 | Client to subscription. It will receive events from server |
||
14 | """ |
||
15 | |||
16 | def datachange_notification(self, node, val, data): |
||
17 | print("Python: New data change event", node, val) |
||
18 | |||
19 | def event_notification(self, event): |
||
20 | print("Python: New event", event) |
||
21 | |||
22 | |||
23 | if __name__ == "__main__": |
||
24 | #from IPython import embed |
||
25 | logging.basicConfig(level=logging.DEBUG) |
||
26 | #client = Client("opc.tcp://olivier:olivierpass@localhost:53530/OPCUA/SimulationServer/") |
||
27 | #client.set_security_string("Basic256Sha256,SignAndEncrypt,certificate-example.der,private-key-example.pem") |
||
28 | try: |
||
29 | start_thread_loop() |
||
30 | client = Client("opc.tcp://localhost:53530/OPCUA/SimulationServer/") |
||
31 | client.connect() |
||
32 | root = client.nodes.root |
||
33 | print("Root is", root) |
||
34 | print("childs of root are: ", root.get_children()) |
||
35 | print("name of root is", root.read_browse_name()) |
||
36 | objects = client.nodes.objects |
||
37 | print("childs og objects are: ", objects.get_children()) |
||
38 | myfloat = client.get_node("ns=4;s=Float") |
||
39 | mydouble = client.get_node("ns=4;s=Double") |
||
40 | myint64 = client.get_node("ns=4;s=Int64") |
||
41 | myuint64 = client.get_node("ns=4;s=UInt64") |
||
42 | myint32 = client.get_node("ns=4;s=Int32") |
||
43 | myuint32 = client.get_node("ns=4;s=UInt32") |
||
44 | |||
45 | var = client.get_node(ua.NodeId("Random1", 5)) |
||
46 | print("var is: ", var) |
||
47 | print("value of var is: ", var.read_value()) |
||
48 | var.write_value(ua.Variant([23], ua.VariantType.Double)) |
||
49 | print("setting float value") |
||
50 | myfloat.write_value(ua.Variant(1.234, ua.VariantType.Float)) |
||
51 | print("reading float value: ", myfloat.read_value()) |
||
52 | |||
53 | handler = SubHandler() |
||
54 | sub = client.create_subscription(500, handler) |
||
55 | handle = sub.subscribe_data_change(var) |
||
56 | |||
57 | device = objects.get_child(["2:MyObjects", "2:MyDevice"]) |
||
58 | method = device.get_child("2:MyMethod") |
||
59 | result = device.call_method(method, ua.Variant("sin"), ua.Variant(180, ua.VariantType.Double)) |
||
60 | print("Mehtod result is: ", result) |
||
61 | |||
62 | #embed() |
||
63 | time.sleep(3) |
||
64 | sub.unsubscribe(handle) |
||
65 | sub.delete() |
||
66 | #client.close_session() |
||
67 | finally: |
||
68 | client.disconnect() |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
69 | stop_thread_loop() |
||
70 |