Dec-09-2020, 08:35 AM
Hi,
first of all I'm new in this forum and also new to python.
I've set up an OPC UA client in Pyhon by using the freeOpcUa library.
https://github.com/FreeOpcUa/python-opcua
The connection to my server works just fine. There is new data at the Server every 20 ms or so.
What I've tried so far is reading the nodes value in a for-loop by using the get_node() and get_value() functions.
Doing it this way, it takes a long time to read for example 1000 values of a variable.
So it tried to set up a subscription by using the SubHandler class. My code is shown below. I want to use a sampling interval of 10 ms.
The subscription works and I'm getting the notification if a value at the server changes. I'm getting the message "Python: New data change event" + the value. For logging purposes I'm only interested in the values. So I deleted the print message from the SubHandler-Class, but if the value change occurs I'm still getting the notification message.
I don't know how to save the changed values to an external variable, to prepare it for logging in a .csv-file.
Any help how to get rid of the notifications and access the values?
Thanks a lot.
Johna
first of all I'm new in this forum and also new to python.
I've set up an OPC UA client in Pyhon by using the freeOpcUa library.
https://github.com/FreeOpcUa/python-opcua
The connection to my server works just fine. There is new data at the Server every 20 ms or so.
What I've tried so far is reading the nodes value in a for-loop by using the get_node() and get_value() functions.
Doing it this way, it takes a long time to read for example 1000 values of a variable.
So it tried to set up a subscription by using the SubHandler class. My code is shown below. I want to use a sampling interval of 10 ms.
The subscription works and I'm getting the notification if a value at the server changes. I'm getting the message "Python: New data change event" + the value. For logging purposes I'm only interested in the values. So I deleted the print message from the SubHandler-Class, but if the value change occurs I'm still getting the notification message.
I don't know how to save the changed values to an external variable, to prepare it for logging in a .csv-file.
Any help how to get rid of the notifications and access the values?
Thanks a lot.
Johna
from opcua import Client class SubHandler(object): """ Subscription Handler. To receive events from server for a subscription data_change and event methods are called directly from receiving thread. Do not do expensive, slow or network operation there. Create another thread if you need to do such a thing """ def datachange_notification(self, node, val, data): print("") def event_notification(self, event): print("") client = Client("opc.tcp://xxx_yyy_zzz") client.connect() X = client.get_node("node_name1") Y = client.get_node("node_name2") Z = client.get_node("node_name3") data = [X,Y,Z] handler = SubHandler() sub = client.create_subscription(10,handler) handle = sub.subscribe_data_change(data)