- config.yaml: 恢复现场 IP(10.50.253.165/.181/.182)、s331_2 端口 502 - config.yaml: S331 全部 32 位量 word_order 改 CDAB,瞬时流量 scale=60 (m3/min -> m3/h) - config.yaml: 11# 电表功率/电能类型改 int32;第二路 RTU 从站 slave_id 改 10 - poller.py: 过滤 S331 无数据哨兵 -9999 与非有限值,避免污染周期基准 - rtu.py: 新增 SafeRTUFramer 支持多点半总线,忽略其它从站请求 - 新增 config.debug.yaml、README.md、wheelhouse 离线依赖 - tests: 适配新配置(ttyS7/address_offset);新增 pytest.ini 限定 testpaths
263 lines
12 KiB
Python
263 lines
12 KiB
Python
"""生成 Modbus Poll / MBS Slave 可直接填写的测试数据表。
|
||
|
||
输出 3 份 CSV + 1 份 Markdown 汇总表:
|
||
- data_mogo_meter.csv (电表,slave=1, port=1502)
|
||
- data_s331_1.csv (S331 #1 气量, slave=10, port=502)
|
||
- data_s331_2.csv (S331 #2 气量, slave=20, port=503)
|
||
- test_data_summary.md (寄存器地址完整对照表)
|
||
|
||
CSV 列含义:
|
||
s4c_addr — Modbus Poll / MBS Slave 里的 S4C 地址(40001 基)
|
||
proto_addr — Modbus 协议地址(0 基,即 config.yaml 里的 address)
|
||
reg_lo — ABCD 字序的低字寄存器值(16-bit,十进制)
|
||
reg_hi — ABCD 字序的高字寄存器值(16-bit,十进制)
|
||
hex_lo — reg_lo 的 16 进制
|
||
hex_hi — reg_hi 的 16 进制
|
||
type — float32 / uint32
|
||
scale — 工程值 = 原始值 × scale
|
||
raw_value — 原始值(写入 Modbus 的值)
|
||
eng_value — 工程值(实际物理量)
|
||
unit — 单位
|
||
note — 说明
|
||
|
||
用法:在 Modbus Poll 的 Connection → Setup → Define 里新建 Connection,
|
||
Slave 地址按 slave_id 填,然后在 Setup → Read/Write Definition
|
||
里按 CSV 的 s4c_addr 和数量建窗口;数据填入 Holding Register。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import csv
|
||
import os
|
||
import struct
|
||
from pathlib import Path
|
||
|
||
# ---------- 数据编码 ----------
|
||
|
||
def enc_float32_abcd(raw: float) -> tuple[int, int]:
|
||
"""float32 按 ABCD 字序拆成两个 16-bit 寄存器(高字在前)。"""
|
||
hi, lo = struct.unpack(">HH", struct.pack(">f", raw))
|
||
return hi, lo # ABCD = [hi, lo] 即 40001=hi, 40002=lo
|
||
|
||
def enc_uint32_abcd(raw: int) -> tuple[int, int]:
|
||
"""uint32 按 ABCD 字序拆成两个 16-bit 寄存器。"""
|
||
if raw < 0 or raw >= 2**32:
|
||
raise ValueError(f"uint32 溢出: {raw}")
|
||
hi, lo = struct.unpack(">HH", struct.pack(">I", raw))
|
||
return hi, lo # ABCD = [hi, lo]
|
||
|
||
# ---------- 点表定义(与 config.yaml 完全对齐)----------
|
||
|
||
# 蘑菇云电表 slave_id=1
|
||
# 功率: float32 ABCD, scale=0.001 → raw = eng / 0.001 = eng * 1000
|
||
# 电能: float32 ABCD, scale=0.1 → raw = eng / 0.1 = eng * 10
|
||
mogo_points = [
|
||
# (comp_id, point_name, proto_addr, type, scale, eng_value, unit)
|
||
(3, "功率", 149, "float32", 0.001, 15.5, "kW"),
|
||
(3, "电能", 151, "float32", 0.1, 12345.67,"kWh"),
|
||
(4, "功率", 157, "float32", 0.001, 12.3, "kW"),
|
||
(4, "电能", 159, "float32", 0.1, 9876.54, "kWh"),
|
||
(5, "功率", 165, "float32", 0.001, 18.0, "kW"),
|
||
(5, "电能", 167, "float32", 0.1, 15000.00, "kWh"),
|
||
(6, "功率", 173, "float32", 0.001, 9.8, "kW"),
|
||
(6, "电能", 175, "float32", 0.1, 7654.32, "kWh"),
|
||
(7, "功率", 181, "float32", 0.001, 20.1, "kW"),
|
||
(7, "电能", 183, "float32", 0.1, 18765.43, "kWh"),
|
||
(8, "功率", 189, "float32", 0.001, 11.2, "kW"),
|
||
(8, "电能", 191, "float32", 0.1, 8765.10, "kWh"),
|
||
(9, "功率", 213, "float32", 0.001, 16.7, "kW"),
|
||
(9, "电能", 215, "float32", 0.1, 13210.20, "kWh"),
|
||
(10, "功率", 205, "float32", 0.001, 13.4, "kW"),
|
||
(10, "电能", 207, "float32", 0.1, 10890.55, "kWh"),
|
||
(11, "功率", 197, "float32", 0.001, 14.8, "kW"),
|
||
(11, "电能", 199, "float32", 0.1, 11555.88, "kWh"),
|
||
]
|
||
|
||
# S331 #1 slave_id=10 负责 3/4/5/11
|
||
# 瞬时流量: float32 ABCD, scale=1 → raw = eng
|
||
# 累计气量: uint32 ABCD, scale=1 → raw = eng
|
||
s331_1_points = [
|
||
(3, "瞬时流量", 112, "float32", 1, 80.5, "m³/h"),
|
||
(3, "累计气量", 114, "uint32", 1, 1234567, "m³"),
|
||
(4, "瞬时流量", 122, "float32", 1, 95.2, "m³/h"),
|
||
(4, "累计气量", 124, "uint32", 1, 1089000, "m³"),
|
||
(5, "瞬时流量", 132, "float32", 1, 70.8, "m³/h"),
|
||
(5, "累计气量", 134, "uint32", 1, 1567800, "m³"),
|
||
(11, "瞬时流量", 142, "float32", 1, 88.1, "m³/h"),
|
||
(11, "累计气量", 144, "uint32", 1, 987654, "m³"),
|
||
]
|
||
|
||
# S331 #2 slave_id=20 负责 6/7/8/9/10
|
||
s331_2_points = [
|
||
(6, "瞬时流量", 118, "float32", 1, 65.0, "m³/h"),
|
||
(6, "累计气量", 120, "uint32", 1, 1678900, "m³"),
|
||
(7, "瞬时流量", 128, "float32", 1, 92.3, "m³/h"),
|
||
(7, "累计气量", 130, "uint32", 1, 1345600, "m³"),
|
||
(8, "瞬时流量", 138, "float32", 1, 78.4, "m³/h"),
|
||
(8, "累计气量", 140, "uint32", 1, 1200000, "m³"),
|
||
(9, "瞬时流量", 148, "float32", 1, 100.0, "m³/h"),
|
||
(9, "累计气量", 150, "uint32", 1, 1800000, "m³"),
|
||
(10, "瞬时流量", 158, "float32", 1, 85.6, "m³/h"),
|
||
(10, "累计气量", 160, "uint32", 1, 1450000, "m³"),
|
||
]
|
||
|
||
# ---------- 编码 & 导出 ----------
|
||
|
||
def build_rows(points: list) -> list[dict]:
|
||
rows = []
|
||
for comp_id, name, proto_addr, dtype, scale, eng_value, unit in points:
|
||
raw_value = eng_value / scale if scale != 0 else eng_value
|
||
if dtype == "float32":
|
||
hi, lo = enc_float32_abcd(float(raw_value))
|
||
elif dtype == "uint32":
|
||
hi, lo = enc_uint32_abcd(int(raw_value))
|
||
else:
|
||
raise ValueError(f"unknown type: {dtype}")
|
||
s4c_addr = proto_addr + 40001 # S4C 显示地址(协议地址 0 → 40001)
|
||
rows.append({
|
||
"comp_id": comp_id,
|
||
"name": f"{comp_id}#{name}",
|
||
"s4c_addr": s4c_addr,
|
||
"proto_addr": proto_addr,
|
||
"reg_hi": hi,
|
||
"reg_lo": lo,
|
||
"hex_hi": f"0x{hi:04X}",
|
||
"hex_lo": f"0x{lo:04X}",
|
||
"type": dtype,
|
||
"scale": scale,
|
||
"raw_value": raw_value,
|
||
"eng_value": eng_value,
|
||
"unit": unit,
|
||
"note": f"工程值={eng_value}{unit}, scale={scale}, 原始值={raw_value}",
|
||
})
|
||
return rows
|
||
|
||
|
||
def write_csv(path: str, rows: list[dict]) -> None:
|
||
fields = ["comp_id", "name", "s4c_addr", "proto_addr",
|
||
"reg_hi", "reg_lo", "hex_hi", "hex_lo",
|
||
"type", "scale", "raw_value", "eng_value", "unit", "note"]
|
||
with open(path, "w", newline="", encoding="utf-8-sig") as f:
|
||
w = csv.DictWriter(f, fieldnames=fields)
|
||
w.writeheader()
|
||
for r in rows:
|
||
w.writerow(r)
|
||
|
||
|
||
def write_markdown(path: str,
|
||
mogo: list[dict],
|
||
s331_1: list[dict],
|
||
s331_2: list[dict]) -> None:
|
||
with open(path, "w", encoding="utf-8") as f:
|
||
f.write("# Modbus Poll / MBS Slave 测试数据对照表\n\n")
|
||
f.write("生成时间:2026-11-17 \n")
|
||
f.write("**重要**:表格中的 `reg_hi` / `reg_lo` 和 `hex_hi` / `hex_lo` 是**原始值**,\n")
|
||
f.write("直接填入 Modbus Poll / MBS Slave 的 Holding Register 即可。\n")
|
||
f.write("`scale` 表示:网关内部工程值 = 原始值 × scale。\n\n")
|
||
|
||
# ---------- 设备 1:蘑菇云电表 ----------
|
||
f.write("---\n\n")
|
||
f.write("## 设备 1:蘑菇云电表 Modbus TCP Server\n\n")
|
||
f.write("| 项目 | 值 |\n|---|---|\n")
|
||
f.write("| Connection | Modbus TCP |\n")
|
||
f.write("| Host | 192.168.1.253(测试)/ 10.50.253.165(现场) |\n")
|
||
f.write("| Port | 1502 |\n")
|
||
f.write("| Slave ID | 1 |\n")
|
||
f.write("| Function Code | 03 (Read Holding Registers) |\n")
|
||
f.write("| 寄存器窗口 | 40149 ~ 40216(连续一段) |\n\n")
|
||
|
||
f.write("### 数据值\n\n")
|
||
f.write("| 空压机 | 点 | 协议地址(0基) | S4C地址 | reg_hi | reg_lo | hex_hi | hex_lo | 类型 | scale | 工程值 | 单位 |\n")
|
||
f.write("|---|---|---|---|---|---|---|---|---|---|---|---|\n")
|
||
for r in mogo:
|
||
f.write(f"| {r['comp_id']}# | {r['name'].split('#',1)[1]} | {r['proto_addr']} | {r['s4c_addr']} | "
|
||
f"{r['reg_hi']} | {r['reg_lo']} | {r['hex_hi']} | {r['hex_lo']} | "
|
||
f"{r['type']} | {r['scale']} | {r['eng_value']} | {r['unit']} |\n")
|
||
|
||
# ---------- 设备 2:S331 #1 ----------
|
||
f.write("\n---\n\n")
|
||
f.write("## 设备 2:希尔思 S331 #1 Modbus TCP Server(负责 3/4/5/11)\n\n")
|
||
f.write("| 项目 | 值 |\n|---|---|\n")
|
||
f.write("| Connection | Modbus TCP |\n")
|
||
f.write("| Host | 192.168.1.253(测试)/ 10.50.253.181(现场) |\n")
|
||
f.write("| Port | 502 |\n")
|
||
f.write("| Slave ID | 10 |\n")
|
||
f.write("| Function Code | 03 |\n")
|
||
f.write("| 寄存器窗口 | 40113 ~ 40145(连续一段) |\n\n")
|
||
|
||
f.write("### 数据值\n\n")
|
||
f.write("| 空压机 | 点 | 协议地址(0基) | S4C地址 | reg_hi | reg_lo | hex_hi | hex_lo | 类型 | scale | 工程值 | 单位 |\n")
|
||
f.write("|---|---|---|---|---|---|---|---|---|---|---|---|\n")
|
||
for r in s331_1:
|
||
f.write(f"| {r['comp_id']}# | {r['name'].split('#',1)[1]} | {r['proto_addr']} | {r['s4c_addr']} | "
|
||
f"{r['reg_hi']} | {r['reg_lo']} | {r['hex_hi']} | {r['hex_lo']} | "
|
||
f"{r['type']} | {r['scale']} | {r['eng_value']} | {r['unit']} |\n")
|
||
|
||
# ---------- 设备 3:S331 #2 ----------
|
||
f.write("\n---\n\n")
|
||
f.write("## 设备 3:希尔思 S331 #2 Modbus TCP Server(负责 6/7/8/9/10)\n\n")
|
||
f.write("| 项目 | 值 |\n|---|---|\n")
|
||
f.write("| Connection | Modbus TCP |\n")
|
||
f.write("| Host | 192.168.1.253(测试)/ 10.50.253.182(现场) |\n")
|
||
f.write("| Port | 503(测试)/ 502(现场) |\n")
|
||
f.write("| Slave ID | 20 |\n")
|
||
f.write("| Function Code | 03 |\n")
|
||
f.write("| 寄存器窗口 | 40119 ~ 40161(连续一段) |\n\n")
|
||
|
||
f.write("### 数据值\n\n")
|
||
f.write("| 空压机 | 点 | 协议地址(0基) | S4C地址 | reg_hi | reg_lo | hex_hi | hex_lo | 类型 | scale | 工程值 | 单位 |\n")
|
||
f.write("|---|---|---|---|---|---|---|---|---|---|---|---|\n")
|
||
for r in s331_2:
|
||
f.write(f"| {r['comp_id']}# | {r['name'].split('#',1)[1]} | {r['proto_addr']} | {r['s4c_addr']} | "
|
||
f"{r['reg_hi']} | {r['reg_lo']} | {r['hex_hi']} | {r['hex_lo']} | "
|
||
f"{r['type']} | {r['scale']} | {r['eng_value']} | {r['unit']} |\n")
|
||
|
||
# ---------- 快速校验 ----------
|
||
f.write("\n---\n\n")
|
||
f.write("## 快速校验样例\n\n")
|
||
f.write("如果网关采集正常,`journalctl -u hx-gateway -n 30` 应该看到类似:\n\n")
|
||
f.write("```\n")
|
||
f.write("状态 3#空压机 瞬时=0.1925 时=0.0000 日=0.0000 月=0.0000 | 瞬时流量=80.5 累计气量=1234567.0 瞬时功率=15.5 累计电量=12345.7 | 数据延迟=1s\n")
|
||
f.write("```\n\n")
|
||
f.write("气电比计算:\n\n")
|
||
f.write("```\n")
|
||
f.write("瞬时气电比 = 功率(kW) / 流量(m³/h)\n")
|
||
f.write("3#: 15.5 / 80.5 = 0.1925\n")
|
||
f.write("```\n")
|
||
|
||
|
||
def main():
|
||
out = Path(r"c:\Users\admin\Desktop\海信视像数采\chengxu\tools\test_data")
|
||
out.mkdir(parents=True, exist_ok=True)
|
||
|
||
mogo = build_rows(mogo_points)
|
||
s331_1 = build_rows(s331_1_points)
|
||
s331_2 = build_rows(s331_2_points)
|
||
|
||
write_csv(str(out / "data_mogo_meter.csv"), mogo)
|
||
write_csv(str(out / "data_s331_1.csv"), s331_1)
|
||
write_csv(str(out / "data_s331_2.csv"), s331_2)
|
||
write_markdown(str(out / "test_data_summary.md"), mogo, s331_1, s331_2)
|
||
|
||
print("✅ 已生成:")
|
||
print(f" {out / 'data_mogo_meter.csv'} (电表 slave=1)")
|
||
print(f" {out / 'data_s331_1.csv'} (S331 #1 slave=10)")
|
||
print(f" {out / 'data_s331_2.csv'} (S331 #2 slave=20)")
|
||
print(f" {out / 'test_data_summary.md'} (完整对照表)")
|
||
print()
|
||
|
||
# 终端预览
|
||
for title, rows in [("蘑菇云电表", mogo), ("S331 #1", s331_1), ("S331 #2", s331_2)]:
|
||
print(f"=== {title} ===")
|
||
print(f"{'点':<10} {'S4C':>6} {'proto':>5} {'type':<8} {'scale':>5} "
|
||
f"{'raw':>12} {'eng':>10} {'unit':<6} hex=[hi,lo]")
|
||
for r in rows:
|
||
print(f"{r['name']:<10} {r['s4c_addr']:>6} {r['proto_addr']:>5} {r['type']:<8} "
|
||
f"{r['scale']:>5} {str(r['raw_value']):>12} {r['eng_value']:>10} {r['unit']:<6} "
|
||
f"[{r['hex_hi']}, {r['hex_lo']}]")
|
||
print()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|