I am working with the opcua Python library to fetch data from an OPC UA server and I am facing issues with reading the “Card Plot” values, which involve two axes: load vs. position. According to the documentation, I need to combine values from two different nodes into a single float value using the struct
library, but my current method is failing.
Please see the documentation of the register I’m trying to fetch the data from
[![Image From Documents][1]][1]
from opcua import Client
import struct
import time
url = "opc.tcp://OPC_SERVER:PORT"
client = Client(url)
try:
client.connect()
print("Client connected to:", url)
# Assuming node_id_1 and node_id_2 are the two parts of the float value
node_id_1 = "ns=2;s=well_OPC_path. 32669"
node_id_2 = "ns=2;s= well_OPC_path. 32669"
node1 = client.get_node(node_id_1)
node2 = client.get_node(node_id_2)
for _ in range 50:
value1 = node1.get_value()
value2 = node2.get_value()
combined_value = struct.pack('HH', value1, value2)
float_value = struct.unpack('f', combined_value)[0]
print("Combined float value from nodes:", float_value)
time.sleep(1)
except Exception as e:
print(f"Error accessing nodes {node_id_1} and {node_id_2}: {str(e)}")
finally:
client.disconnect()
print("Client disconnected")
I receive no errors, but the output values are not as expected. Can someone help me figure out what I might be doing wrong? Are the node IDs correct for combining into a float? Any guidance or pointers to what I might be misunderstanding in the documentation would be greatly appreciated!
Thank you!
I would expect to have around 200 pair of values Load vs Position
[1]: https://i.sstatic.net/BlCn5xzu.jpg
Mohammed Alaamri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.