Files
test/sf_machine_connect/models/py2opcua.py
2023-01-09 23:04:53 +08:00

33 lines
973 B
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:
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:
print("opcua服务器连接失败请检查")
def write(self, temp_dict):
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())
def disconnect(self):
# 断开连接
self.client.disconnect()