Files
test/sf_machine_connect/models/py2opcua.py
2023-01-11 14:51:59 +08:00

47 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from opcua import ua, Client
class Py2opcua:
"""
将三元检测补偿值写入opcua服务器
"""
def __init__(self, url='opc.tcp://192.168.2.99:4840'):
self.client = Client(url)
# 连接客户端
# def connect(self):
# try:
# self.client.connect()
# print("opcua服务器连接成功可以写入")
# return self.client
# except Exception as e:
# print("opcua服务器连接失败请检查" + str(e))
def write(self, temp_dict):
"""
补偿值写入方法,参数是一个字典,键是节点名,值是补偿值
:param temp_dict:
:return:
"""
# try:
self.client.connect()
# print("opcua服务器连接成功可以写入")
# return self.client
# except Exception as e:
# print("opcua服务器连接失败请检查" + str(e))
# temp_dict = temp_dict
temp_list = list(temp_dict.items())
for i in range(len(temp_list)):
# 寻找节点上的变量
var = self.client.get_node(temp_list[i][0])
# var.set_value(ua.Variant(1.234, ua.VariantType.Float))
# 通过set_value写值
var.set_value(ua.Variant(temp_list[i][1], ua.VariantType.Double))
# print("%s 已写入" % var.get_value())
self.client.disconnect()
# 断开连接
# def disconnect(self):
# self.client.disconnect()