sf1.0
This commit is contained in:
32
sf_machine_connect/models/py2opcua.py
Normal file
32
sf_machine_connect/models/py2opcua.py
Normal file
@@ -0,0 +1,32 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user