From 1a22ea3bfa9d23a74038be04a967aee0df7a5c0d Mon Sep 17 00:00:00 2001 From: "Strawder, Paul" Date: Tue, 5 Sep 2023 11:18:28 +0200 Subject: [PATCH] bugfix: LLT/JBD BMS general packet data size check --- etc/dbus-serialbattery/bms/lltjbd.py | 24 ++++++++++++++++-------- requirements.txt | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/etc/dbus-serialbattery/bms/lltjbd.py b/etc/dbus-serialbattery/bms/lltjbd.py index 847b255b..0d1bbfd9 100644 --- a/etc/dbus-serialbattery/bms/lltjbd.py +++ b/etc/dbus-serialbattery/bms/lltjbd.py @@ -260,11 +260,14 @@ def test_connection(self): # Return True if success, False for failure result = False try: - result = self.get_settings() - # get first data to show in startup log, only if result is true - if result: + # 1) Read name of BMS + # 2) Try read BMS settings + # 3) Refresh general data + result = ( self.read_hardware_data() - self.refresh_data() + and self.get_settings() + and self.refresh_data() + ) except Exception as err: logger.error(f"Unexpected {err=}, {type(err)=}") result = False @@ -438,9 +441,7 @@ def write_balancer(self): def refresh_data(self): self.write_charge_discharge_mos() self.write_balancer() - result = self.read_gen_data() - result = result and self.read_cell_data() - return result + return self.read_gen_data() and self.read_cell_data() def to_protection_bits(self, byte_data): tmp = bin(byte_data)[2:].rjust(13, utils.zero_char) @@ -530,7 +531,7 @@ def to_fet_bits(self, byte_data): def read_gen_data(self): gen_data = self.read_serial_data_llt(self.command_general) # check if connect success - if gen_data is False or len(gen_data) < 27: + if gen_data is False or len(gen_data) < 23: return False ( @@ -567,6 +568,13 @@ def read_gen_data(self): # 0 = MOS, 1 = temp 1, 2 = temp 2 for t in range(self.temp_sensors): + if len(gen_data) < 23 + (2 * t) + 2: + logger.warn( + "Expected %d temperature sensors, but received only %d sensor readings!", + self.temp_sensors, + t, + ) + return True temp1 = unpack_from(">H", gen_data, 23 + (2 * t))[0] self.to_temp(t, utils.kelvin_to_celsius(temp1 / 10)) diff --git a/requirements.txt b/requirements.txt index 6de24eea..f5c4cddd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ pyserial==3.5 minimalmodbus==2.0.1 -bleak==0.20.0 \ No newline at end of file +bleak==0.20.2 \ No newline at end of file