client-example.py
Go to the documentation of this file.
1 import sys
2 sys.path.insert(0, "..")
3 import logging
4 import time
5 
6 try:
7  from IPython import embed
8 except ImportError:
9  import code
10 
11  def embed():
12  vars = globals()
13  vars.update(locals())
14  shell = code.InteractiveConsole(vars)
15  shell.interact()
16 
17 
18 from opcua import Client
19 from opcua import ua
20 
21 
22 class SubHandler(object):
23 
24  """
25  Subscription Handler. To receive events from server for a subscription
26  data_change and event methods are called directly from receiving thread.
27  Do not do expensive, slow or network operation there. Create another
28  thread if you need to do such a thing
29  """
30 
31  def datachange_notification(self, node, val, data):
32  print("Python: New data change event", node, val)
33 
34  def event_notification(self, event):
35  print("Python: New event", event)
36 
37 
38 if __name__ == "__main__":
39  logging.basicConfig(level=logging.WARN)
40  #logger = logging.getLogger("KeepAlive")
41  #logger.setLevel(logging.DEBUG)
42 
43  client = Client("opc.tcp://localhost:4840/freeopcua/server/")
44  # client = Client("opc.tcp://admin@localhost:4840/freeopcua/server/") #connect using a user
45  try:
46  client.connect()
47 
48  # Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objects
49  root = client.get_root_node()
50  print("Root node is: ", root)
51  objects = client.get_objects_node()
52  print("Objects node is: ", objects)
53 
54  # Node objects have methods to read and write node attributes as well as browse or populate address space
55  print("Children of root are: ", root.get_children())
56 
57  # get a specific node knowing its node id
58  #var = client.get_node(ua.NodeId(1002, 2))
59  #var = client.get_node("ns=3;i=2002")
60  #print(var)
61  #var.get_data_value() # get value of node as a DataValue object
62  #var.get_value() # get value of node as a python builtin
63  #var.set_value(ua.Variant([23], ua.VariantType.Int64)) #set node value using explicit data type
64  #var.set_value(3.9) # set node value using implicit data type
65 
66  # Now getting a variable node using its browse path
67  myvar = root.get_child(["0:Objects", "2:MyObject", "2:MyVariable"])
68  obj = root.get_child(["0:Objects", "2:MyObject"])
69  print("myvar is: ", myvar)
70 
71  # subscribing to a variable node
72  handler = SubHandler()
73  sub = client.create_subscription(500, handler)
74  handle = sub.subscribe_data_change(myvar)
75  time.sleep(0.1)
76 
77  # we can also subscribe to events from server
78  sub.subscribe_events()
79  # sub.unsubscribe(handle)
80  # sub.delete()
81 
82  # calling a method on server
83  res = obj.call_method("2:multiply", 3, "klk")
84  print("method result is: ", res)
85 
86  embed()
87  finally:
88  client.disconnect()
def event_notification(self, event)
def datachange_notification(self, node, val, data)


ros_opcua_impl_python_opcua
Author(s): Denis Štogl , Daniel Draper
autogenerated on Tue Jan 19 2021 03:12:43