pump / backend /tools /opcua_test_client.py
Codex
Deploy efficiency estimate fix cf55248
aec7019
Raw History Blame Contribute Delete
2.18 kB
#!/usr/bin/env python3
"""OPC UA 测试客户端:连仿真服务器(或现场标准 PC 的 OPC UA 服务器),
读取泵站点位、写一个频率设定,验证读写通。
python tools/opcua_test_client.py # 默认连本机仿真服务器
python tools/opcua_test_client.py --url opc.tcp://PC_IP:4840/...
"""
import argparse
import asyncio
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from asyncua import Client # noqa: E402
NS_URI = "http://biditech.pump/opcua/"
async def main(url):
async with Client(url=url) as client:
idx = await client.get_namespace_index(NS_URI)
print(f"connected {url} ns={idx}")
# 读站级点
for name in ("Level", "TotalFlow", "TotalPower", "Pressure"):
node = client.get_node(f"ns={idx};s=PumpStation1.{name}")
print(f" PumpStation1.{name} = {await node.read_value()}")
# 读各泵
for pid in range(1, 6):
try:
f = await client.get_node(f"ns={idx};s=PumpStation1.Pump{pid}.Freq").read_value()
p = await client.get_node(f"ns={idx};s=PumpStation1.Pump{pid}.Power").read_value()
s = await client.get_node(f"ns={idx};s=PumpStation1.Pump{pid}.Status").read_value()
print(f" Pump{pid}: Freq={f} Power={p} Status={s}")
except Exception as e: # noqa: BLE001
print(f" Pump{pid}: {e}")
# 写使能 + 频率设定回写测试
from asyncua import ua
we = client.get_node(f"ns={idx};s=PumpStation1.WriteEnable")
await we.write_value(ua.DataValue(ua.Variant(True, ua.VariantType.Boolean)))
fs = client.get_node(f"ns={idx};s=PumpStation1.Pump1.FreqSet")
await fs.write_value(ua.DataValue(ua.Variant(46.5, ua.VariantType.Double)))
print(f" wrote WriteEnable=True, Pump1.FreqSet=46.5 → readback {await fs.read_value()}")
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("--url", default="opc.tcp://127.0.0.1:4840/biditech/server/")
a = ap.parse_args()
asyncio.run(main(a.url))