client.py
Go to the documentation of this file.
1 
2 import sys
3 import time
4 sys.path.append(".")
5 
6 from IPython import embed
7 import opcua
8 
9 class SubHandler(opcua.SubscriptionHandler):
10  """
11  Client to subcsription. It will receive events from server
12  """
13  def __init__(self, *args):
14  opcua.SubscriptionHandler.__init__(self, *args)
15 
16  def data_change(self, handle, node, val, attr):
17  print("Python: New data change event", handle, node, val, attr)
18 
19  def event(self, handle, event):
20  print("Python: New event", handle, event)
21 
22 
23 if __name__ == "__main__":
24  # create our client object
25  client = opcua.Client(False)
26  client.connect("opc.tcp://localhost:4841/freeopcua/server/")
27  #s.connect("opc.tcp://192.168.56.101:48030")
28  try:
29  # get server namespace. You may want to get all namespaces
30  # with client.get_server_namespaces()
31  uri = "http://examples.freeopcua.github.io"
32  idx = client.get_namespace_index(uri)
33 
34  # read a node from standard opcua address space
35  statenode = client.get_node(opcua.ObjectId.Server_ServerStatus_State)
36  print("Server state is: ", statenode.get_value())
37 
38  # get root node of server and browse it
39  root = client.get_root_node()
40  print("I got root: ", root)
41  print("Childs are: ", root.get_children())
42 
43  # get objects node of server
44  # this is where the interesting data from server should be
45  print("Objects is: ", client.get_objects_node())
46  objects = client.get_objects_node()
47  print("Children of objects are: ", objects.get_children())
48 
49  # get child using browse path
50  myvar = objects.get_child(["{}:NewObject".format(idx), "MyVariable"])
51  print("yvar is: ", myvar)
52 
53  # create a subsription we will use to subscribe to nodes or events
54  sclt = SubHandler()
55  sub = client.create_subscription(100, sclt)
56 
57  # subscribe to a specific node
58  handle = sub.subscribe_data_change(myvar)
59  print("Subscribe handle is: ", handle)
60 
61  # subscribe to events from server
62  evhandle = sub.subscribe_events()
63  print("Subscribe handle is: ", evhandle)
64 
65 
66  embed()
67  finally:
68  client.disconnect()
def __init__(self, args)
Definition: client.py:13
def event(self, handle, event)
Definition: client.py:19
FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args)
Definition: format.cc:873
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3686
def data_change(self, handle, node, val, attr)
Definition: client.py:16


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:04