# -*- coding: utf-8 -*- """在 COM4 上用不同帧格式探测从站 10,定位 Modbus Poll 参数差异。 对角线实验:N/E/O 校验 × 1/2 停止位。""" from pymodbus.client import ModbusSerialClient for parity, stopbits in (("N", 1), ("E", 1), ("O", 1), ("N", 2)): client = ModbusSerialClient( port="COM4", baudrate=19200, bytesize=8, parity=parity, stopbits=stopbits, timeout=0.8, ) try: ok = client.connect() if not ok: print(f"8-{parity}-1 stop{stopbits}: 打开失败") continue rr = client.read_holding_registers(0, count=8, slave=10) if rr.isError(): print(f"8-{parity}-{stopbits}: 异常响应") else: print(f"8-{parity}-{stopbits}: OK 寄存器0~7={rr.registers[:4]}...") except Exception as e: # noqa: BLE001 print(f"8-{parity}-{stopbits}: 无响应 ({type(e).__name__})") finally: client.close()