client_to_prosys.py
Go to the documentation of this file.
1 import sys
2 sys.path.insert(0, "..")
3 import time
4 import logging
5 
6 from opcua import Client
7 from opcua import ua
8 
9 
10 class SubHandler(object):
11 
12  """
13  Client to subscription. It will receive events from server
14  """
15 
16  def datachange_notification(self, node, val, data):
17  print("Python: New data change event", node, val)
18 
19  def event_notification(self, event):
20  print("Python: New event", event)
21 
22 
23 if __name__ == "__main__":
24  #from IPython import embed
25  logging.basicConfig(level=logging.DEBUG)
26  client = Client("opc.tcp://localhost:53530/OPCUA/SimulationServer/")
27  #client = Client("opc.tcp://olivier:olivierpass@localhost:53530/OPCUA/SimulationServer/")
28  #client.set_security_string("Basic256,SignAndEncrypt,certificate-example.der,private-key-example.pem")
29  try:
30  client.connect()
31  root = client.get_root_node()
32  print("Root is", root)
33  print("childs of root are: ", root.get_children())
34  print("name of root is", root.get_browse_name())
35  objects = client.get_objects_node()
36  print("childs og objects are: ", objects.get_children())
37  myfloat = client.get_node("ns=4;s=Float")
38  mydouble = client.get_node("ns=4;s=Double")
39  myint64 = client.get_node("ns=4;s=Int64")
40  myuint64 = client.get_node("ns=4;s=UInt64")
41  myint32 = client.get_node("ns=4;s=Int32")
42  myuint32 = client.get_node("ns=4;s=UInt32")
43 
44  var = client.get_node(ua.NodeId("Random1", 5))
45  print("var is: ", var)
46  print("value of var is: ", var.get_value())
47  var.set_value(ua.Variant([23], ua.VariantType.Double))
48  print("setting float value")
49  myfloat.set_value(ua.Variant(1.234, ua.VariantType.Float))
50  print("reading float value: ", myfloat.get_value())
51 
52  handler = SubHandler()
53  sub = client.create_subscription(500, handler)
54  handle = sub.subscribe_data_change(var)
55 
56  device = objects.get_child(["2:MyObjects", "2:MyDevice"])
57  method = device.get_child("2:MyMethod")
58  result = device.call_method(method, ua.Variant("sin"), ua.Variant(180, ua.VariantType.Double))
59  print("Mehtod result is: ", result)
60 
61  #embed()
62  time.sleep(3)
63  sub.unsubscribe(handle)
64  sub.delete()
65  #client.close_session()
66  finally:
67  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