现场调试版同步:S331 点表修正(CDAB/scale 60/int32) + SafeRTUFramer 多点485 + 哨兵过滤
- 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
This commit is contained in:
commit
9eaf87ab7d
43 files changed
+4514
No files matched your search
@@ -0,0 +1,82 @@
|
||||
"""现场点表核查工具。
|
||||
|
||||
用于和 S331 / 410S 的实际点表对齐:读取一段保持寄存器,
|
||||
同时给出 uint16 / int16 / int32 / uint32 / float32(ABCD) / float32(CDAB) 的解释。
|
||||
|
||||
用法示例:
|
||||
python -m gateway.probe --host 192.168.10.31 --slave 1 --start 0 --count 40
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import struct
|
||||
import sys
|
||||
|
||||
from pymodbus.client import ModbusTcpClient
|
||||
|
||||
|
||||
def _read(client: ModbusTcpClient, start: int, count: int, slave_id: int) -> list[int]:
|
||||
try:
|
||||
response = client.read_holding_registers(start, count=count, slave=slave_id)
|
||||
except TypeError:
|
||||
response = client.read_holding_registers(start, count=count, device_id=slave_id)
|
||||
if response is None or response.isError():
|
||||
raise IOError(f"读取失败: {response}")
|
||||
return list(response.registers)
|
||||
|
||||
|
||||
def _f32(words: list[int], order: str) -> float:
|
||||
hi, lo = (words[1], words[0]) if order == "CDAB" else (words[0], words[1])
|
||||
return struct.unpack(">f", struct.pack(">HH", hi, lo))[0]
|
||||
|
||||
|
||||
def _s32(words: list[int], signed: bool) -> int:
|
||||
hi, lo = words[0], words[1]
|
||||
return struct.unpack(">i" if signed else ">I", struct.pack(">HH", hi, lo))[0]
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(description="Modbus TCP 点表核查")
|
||||
parser.add_argument("--host", required=True)
|
||||
parser.add_argument("--port", type=int, default=502)
|
||||
parser.add_argument("--slave", type=int, default=1)
|
||||
parser.add_argument("--start", type=int, default=0, help="起始协议地址(40001 -> 0)")
|
||||
parser.add_argument("--count", type=int, default=32, help="读取寄存器个数")
|
||||
parser.add_argument("--timeout", type=float, default=2.0)
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
client = ModbusTcpClient(args.host, port=args.port, timeout=args.timeout)
|
||||
if not client.connect():
|
||||
print(f"连接失败: {args.host}:{args.port}", file=sys.stderr)
|
||||
return 1
|
||||
try:
|
||||
registers = _read(client, args.start, args.count, args.slave)
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
print(f"{args.host}:{args.port} slave={args.slave} 起始={args.start} 共 {len(registers)} 个寄存器")
|
||||
print(
|
||||
f"{'地 址':>7} {'4xxxx':>7} {'HEX':>6} {'U16':>7} {'I16':>7} "
|
||||
f"{'I32':>12} {'U32':>12} {'Float(ABCD)':>14} {'Float(CDAB)':>14}"
|
||||
)
|
||||
for offset, word in enumerate(registers):
|
||||
address = args.start + offset
|
||||
i32 = u32 = "-"
|
||||
f_abcd = f_cdab = "-"
|
||||
if offset + 1 < len(registers):
|
||||
pair = registers[offset : offset + 2]
|
||||
i32 = str(_s32(pair, True))
|
||||
u32 = str(_s32(pair, False))
|
||||
f_abcd = f"{_f32(pair, 'ABCD'):.5g}"
|
||||
f_cdab = f"{_f32(pair, 'CDAB'):.5g}"
|
||||
print(
|
||||
f"{address:>7} {40001 + address:>7} {word:>6} {word:>7} "
|
||||
f"{word - 65536 if word > 32767 else word:>7} {i32:>12} {u32:>12} "
|
||||
f"{f_abcd:>14} {f_cdab:>14}"
|
||||
)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user