Skip to content

Commit ead1e2b

Browse files
committed
Update RS-485 discovery helper to also probe EG4 A5/5A BMS
1 parent 9639880 commit ead1e2b

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

‎scripts/discover-rs485.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,49 @@
2121
sys.path.insert(0, str(repo))
2222

2323
from collector.rs485 import Rs485DeviceConfig
24+
from collector.rs485.eg4_a5_bms import Eg4A5BmsDevice
2425
from collector.rs485.jk_bms import JkBmsDevice, STATUS_REQUEST, STATUS_REQUEST_FALLBACK, _append_checksum
2526

2627

28+
def probe_eg4_a5_bms(port: str, baud: int, timeout: float = 1.0) -> bool:
29+
"""Listen for EG4 A5/5A broadcast frames and see if they parse."""
30+
try:
31+
import serial
32+
except ImportError:
33+
return False
34+
35+
try:
36+
dev = serial.Serial(port, baudrate=baud, timeout=timeout)
37+
except Exception as exc:
38+
print(f" Could not open {port} at {baud}: {exc}")
39+
return False
40+
41+
found = False
42+
try:
43+
dev.reset_input_buffer()
44+
# Send a minimal poll probe to wake a quiet BMS.
45+
dev.write(bytes([0xA5, 0x5A, 0x05, 0x82, 0x21, 0x13, 0x00, 0x00]))
46+
time.sleep(0.2)
47+
data = dev.read(4096)
48+
if data:
49+
print(f" [{baud}] EG4 A5 probe got {len(data)} bytes: {data[:60].hex()}")
50+
drv = Eg4A5BmsDevice(Rs485DeviceConfig(port=port, baudrate=baud, timeout=timeout))
51+
parsed = drv.read()
52+
if parsed:
53+
print(f" [{baud}] EG4 A5 BMS parsed successfully:")
54+
for key in ("voltage", "current", "power", "temperature_pcb", "cell_count", "cell_delta_voltage"):
55+
info = parsed.get(key)
56+
if info:
57+
print(f" {key}: {info['value']} {info.get('unit', '')}")
58+
found = True
59+
else:
60+
print(f" [{baud}] EG4 A5 BMS parse failed")
61+
finally:
62+
dev.close()
63+
64+
return found
65+
66+
2767
def probe_jk_bms(port: str, baud: int, timeout: float = 1.0) -> bool:
2868
"""Send JK BMS status requests and see if anything parses."""
2969
try:
@@ -74,6 +114,8 @@ def main():
74114
print(f"Probing {args.port} for RS-485 devices...")
75115
for baud in args.baud:
76116
print(f"\nBaud {baud}:")
117+
if probe_eg4_a5_bms(args.port, baud, args.timeout):
118+
print(f" -> EG4 A5/5A BMS detected at {baud} baud")
77119
if probe_jk_bms(args.port, baud, args.timeout):
78120
print(f" -> JK BMS detected at {baud} baud")
79121

0 commit comments

Comments
 (0)