client.py
Go to the documentation of this file.
00001 
00002 import sys
00003 import time
00004 sys.path.append(".")
00005 
00006 from IPython import embed
00007 import opcua
00008 
00009 class SubHandler(opcua.SubscriptionHandler):
00010     """
00011     Client to subcsription. It will receive events from server
00012     """
00013     def __init__(self, *args):
00014         opcua.SubscriptionHandler.__init__(self, *args)
00015 
00016     def data_change(self, handle, node, val, attr):
00017         print("Python: New data change event", handle, node, val, attr)
00018 
00019     def event(self, handle, event):
00020         print("Python: New event", handle, event)
00021 
00022 
00023 if __name__ == "__main__":
00024     # create our client object
00025     client = opcua.Client(False)
00026     client.connect("opc.tcp://localhost:4841/freeopcua/server/")
00027     #s.connect("opc.tcp://192.168.56.101:48030")
00028     try:
00029         # get server namespace. You may want to get all namespaces
00030         # with client.get_server_namespaces()
00031         uri = "http://examples.freeopcua.github.io"
00032         idx = client.get_namespace_index(uri)
00033         
00034         # read a node from standard opcua address space
00035         statenode = client.get_node(opcua.ObjectId.Server_ServerStatus_State)
00036         print("Server state is: ", statenode.get_value())
00037 
00038         # get root node of server and browse it
00039         root = client.get_root_node()
00040         print("I got root: ", root)
00041         print("Childs are: ", root.get_children())
00042 
00043         # get objects node of server 
00044         # this is where the interesting data from server should be
00045         print("Objects is: ", client.get_objects_node())
00046         objects = client.get_objects_node()
00047         print("Children of objects are: ", objects.get_children())
00048         
00049         # get child using browse path
00050         myvar = objects.get_child(["{}:NewObject".format(idx), "MyVariable"])
00051         print("yvar is: ", myvar)
00052         
00053         # create a subsription we will use to subscribe to nodes or events
00054         sclt = SubHandler()
00055         sub = client.create_subscription(100, sclt)
00056 
00057         # subscribe to a specific node  
00058         handle = sub.subscribe_data_change(myvar)
00059         print("Subscribe handle is: ", handle)
00060 
00061         # subscribe to events from server  
00062         evhandle = sub.subscribe_events()
00063         print("Subscribe handle is: ", evhandle)
00064 
00065 
00066         embed()
00067     finally:
00068         client.disconnect()


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:40