基于python使用opcua通信

官方文档


 例子:

  Server: https://github.com/FreeOpcUa/python-opcua/blob/master/examples/server-minimal.py

 1 import sys
 2 sys.path.insert(0, "..")
 3 import time
 4 
 5 
 6 from opcua import ua, Server
 7 
 8 
 9 if __name__ == "__main__":
10 
11     # setup our server
12     server = Server()
13     server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
14 
15     # setup our own namespace, not really necessary but should as spec
16     uri = "http://examples.freeopcua.github.io"
17     idx = server.register_namespace(uri)
18 
19     # get Objects node, this is where we should put our nodes
20     objects = server.get_objects_node()
21 
22     # populating our address space
23     myobj = objects.add_object(idx, "MyObject")
24     myvar = myobj.add_variable(idx, "MyVariable", 6.7)
25     myvar.set_writable()    # Set MyVariable to be writable by clients
26 
27     # starting!
28     server.start()
29     
30     try:
31         count = 0
32         while True:
33             time.sleep(1)
34             count += 0.1
35             myvar.set_value(count)
36     finally:
37         #close connection, remove subcsriptions, etc
38         server.stop()

  Client: https://github.com/FreeOpcUa/python-opcua/blob/master/examples/client-minimal.py

 1 import sys
 2 sys.path.insert(0, "..")
 3 
 4 
 5 from opcua import Client
 6 
 7 
 8 if __name__ == "__main__":
 9 
10     client = Client("opc.tcp://localhost:4840/freeopcua/server/")
11     # client = Client("opc.tcp://admin@localhost:4840/freeopcua/server/") #connect using a user
12     try:
13         client.connect()
14 
15         # Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objects
16         root = client.get_root_node()
17         print("Objects node is: ", root)
18 
19         # Node objects have methods to read and write node attributes as well as browse or populate address space
20         print("Children of root are: ", root.get_children())
21 
22         # get a specific node knowing its node id
23         #var = client.get_node(ua.NodeId(1002, 2))
24         #var = client.get_node("ns=3;i=2002")
25         #print(var)
26         #var.get_data_value() # get value of node as a DataValue object
27         #var.get_value() # get value of node as a python builtin
28         #var.set_value(ua.Variant([23], ua.VariantType.Int64)) #set node value using explicit data type
29         #var.set_value(3.9) # set node value using implicit data type
30 
31         # Now getting a variable node using its browse path
32         myvar = root.get_child(["0:Objects", "2:MyObject", "2:MyVariable"])
33         obj = root.get_child(["0:Objects", "2:MyObject"])
34         print("myvar is: ", myvar)
35         print("myobj is: ", obj)
36 
37         # Stacked myvar access
38         # print("myvar is: ", root.get_children()[0].get_children()[1].get_variables()[0].get_value())
39 
40     finally:
41         client.disconnect()

 

posted @ 2021-01-23 23:25  Pio-GD  阅读(3030)  评论(0编辑  收藏  举报