I'm trying to implement on Python the OPC UA client for equipment, that has 4 measurement channels (pressure sensors) and data readiness flag. The measurement is asynchronous (channels are not synchronized), and fast (5ms sample time). This is why the data is collected in an array: .Machine.Sensor[i].PressureData[j], where i - number of pressure sensor (0...3), j - array index (0...10000). Readiness flag .Machine.Flag is raised by the machine when all measurement is finished. The simplified code of the client is below:
from opcua import Client
import time
def dataread():
client_in = Client(url)
client_in.connect()
press = client_in.get_node("ns=2;i=4")
pressure = press.get_value()
print("data ready: ", press)
client_in.disconnect()
class Ready(object):
def datachange_notification(self, node, val, data):
dataread()
url = "opc.tcp://localhost:4840"
client = Client(url)
client.connect()
print("Client connected")
Readiness = client.get_node("ns=2;i=6")
handlerReady = Ready()
subscribe = client.create_subscription(100,handlerReady)
h = subscribe.subscribe_data_change(Readiness)
while True:
Temp = client.get_node("ns=2;i=3")
Temperature = Temp.get_value()
print(".")
time.sleep(1)
As for now, I subscripted for change of readiness flag, and then in the handler, I call another function where all pressure data is reading (the code is simplified and it is only for one channel in the example). Firstly I have tried to implement all pressure data retrieving inside the handler datachange_notification but received a timeout message. Then I've read in the documentation https://python-opcua.readthedocs.io/en/latest/client.html "Do not do expensive/slow or network operation from these methods since they are called directly from receiving thread. This is a design choice, start another thread if you need to do such a thing.". Then I decide to do it as shown in the code above (additional connection), and it works. In case when I'm using an already established connection I can't retrieve the pressure data. But, the questions are:
- Is it ok, when I will use the additional connection, that will be established by the subscription handler in another connection? As I said it works, but I have some doubts.
- Is there another way to read the data from another OPC UA Node, when the time of retrieving is set by another Node (like a flag of data readiness)? As I said, the data readiness flag is one for all sensors, so on the server, it is implemented as an additional Node.