00001 import sys 00002 sys.path.insert(0, "..") 00003 00004 00005 from opcua import Client 00006 00007 00008 if __name__ == "__main__": 00009 00010 client = Client("opc.tcp://localhost:4840/freeopcua/server/") 00011 # client = Client("opc.tcp://admin@localhost:4840/freeopcua/server/") #connect using a user 00012 try: 00013 client.connect() 00014 00015 # Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objects 00016 root = client.get_root_node() 00017 print("Objects node is: ", root) 00018 00019 # Node objects have methods to read and write node attributes as well as browse or populate address space 00020 print("Children of root are: ", root.get_children()) 00021 00022 # get a specific node knowing its node id 00023 #var = client.get_node(ua.NodeId(1002, 2)) 00024 #var = client.get_node("ns=3;i=2002") 00025 #print(var) 00026 #var.get_data_value() # get value of node as a DataValue object 00027 #var.get_value() # get value of node as a python builtin 00028 #var.set_value(ua.Variant([23], ua.VariantType.Int64)) #set node value using explicit data type 00029 #var.set_value(3.9) # set node value using implicit data type 00030 00031 # Now getting a variable node using its browse path 00032 myvar = root.get_child(["0:Objects", "2:MyObject", "2:MyVariable"]) 00033 obj = root.get_child(["0:Objects", "2:MyObject"]) 00034 print("myvar is: ", myvar) 00035 00036 finally: 00037 client.disconnect()