client-example.py
Go to the documentation of this file.
00001 import sys
00002 sys.path.insert(0, "..")
00003 import logging
00004 import time
00005 
00006 try:
00007     from IPython import embed
00008 except ImportError:
00009     import code
00010 
00011     def embed():
00012         vars = globals()
00013         vars.update(locals())
00014         shell = code.InteractiveConsole(vars)
00015         shell.interact()
00016 
00017 
00018 from opcua import Client
00019 from opcua import ua
00020 
00021 
00022 class SubHandler(object):
00023 
00024     """
00025     Subscription Handler. To receive events from server for a subscription
00026     data_change and event methods are called directly from receiving thread.
00027     Do not do expensive, slow or network operation there. Create another 
00028     thread if you need to do such a thing
00029     """
00030 
00031     def datachange_notification(self, node, val, data):
00032         print("Python: New data change event", node, val)
00033 
00034     def event_notification(self, event):
00035         print("Python: New event", event)
00036 
00037 
00038 if __name__ == "__main__":
00039     logging.basicConfig(level=logging.WARN)
00040     #logger = logging.getLogger("KeepAlive")
00041     #logger.setLevel(logging.DEBUG)
00042 
00043     client = Client("opc.tcp://localhost:4840/freeopcua/server/")
00044     # client = Client("opc.tcp://admin@localhost:4840/freeopcua/server/") #connect using a user
00045     try:
00046         client.connect()
00047 
00048         # Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objects
00049         root = client.get_root_node()
00050         print("Root node is: ", root)
00051         objects = client.get_objects_node()
00052         print("Objects node is: ", objects)
00053 
00054         # Node objects have methods to read and write node attributes as well as browse or populate address space
00055         print("Children of root are: ", root.get_children())
00056 
00057         # get a specific node knowing its node id
00058         #var = client.get_node(ua.NodeId(1002, 2))
00059         #var = client.get_node("ns=3;i=2002")
00060         #print(var)
00061         #var.get_data_value() # get value of node as a DataValue object
00062         #var.get_value() # get value of node as a python builtin
00063         #var.set_value(ua.Variant([23], ua.VariantType.Int64)) #set node value using explicit data type
00064         #var.set_value(3.9) # set node value using implicit data type
00065 
00066         # Now getting a variable node using its browse path
00067         myvar = root.get_child(["0:Objects", "2:MyObject", "2:MyVariable"])
00068         obj = root.get_child(["0:Objects", "2:MyObject"])
00069         print("myvar is: ", myvar)
00070 
00071         # subscribing to a variable node
00072         handler = SubHandler()
00073         sub = client.create_subscription(500, handler)
00074         handle = sub.subscribe_data_change(myvar)
00075         time.sleep(0.1)
00076 
00077         # we can also subscribe to events from server
00078         sub.subscribe_events()
00079         # sub.unsubscribe(handle)
00080         # sub.delete()
00081 
00082         # calling a method on server
00083         res = obj.call_method("2:multiply", 3, "klk")
00084         print("method result is: ", res)
00085 
00086         embed()
00087     finally:
00088         client.disconnect()


ros_opcua_impl_python_opcua
Author(s): Denis Štogl , Daniel Draper
autogenerated on Sat Jun 8 2019 18:26:23