# pip install pythonnet
import time
import clr
clr.AddReference("OPC_Client")
if True: # 防止格式化,跑到上面去了
import OPC_Client
opc = OPC_Client.Startup()
opc.serverip = "localhost" # 远程访问填写ip
opc.servername = "Knight.OPC.Server.Demo"
opc.updaterate = 500 # 更新速率
opc.items = ["a.a.a", "a.a.b", "a.a.c", "a.a.d"] # 变量名列表
opc.OpcConnect()
while True:
time.sleep(0.5)
opc.PyWrite([("a.a.d", 1), ("a.a.c", 1)]) # 写入opc
print(opc.values["a.a.a"]) # 常规取值
for v in opc.values: # 如果需要遍历,如下
print(v.Key, v.Value)
opc.StopOpc() # 断开opc连接