diff --git a/CHANGELOG.md b/CHANGELOG.md index e6243b61..12d2ee32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,11 +40,12 @@ * Changed: Fix for https://github.com/Louisvdw/dbus-serialbattery/issues/239 * Changed: Fix for https://github.com/Louisvdw/dbus-serialbattery/issues/311 * Changed: Fix for https://github.com/Louisvdw/dbus-serialbattery/issues/351 +* Changed: Fix for https://github.com/Louisvdw/dbus-serialbattery/issues/397 by @transistorgit * Changed: Fix for https://github.com/Louisvdw/dbus-serialbattery/issues/421 * Changed: Fixed black lint errors * Changed: Fixed cell balancing background for cells 17-24 * Changed: Fixed Time-To-Go is not working, if `TIME_TO_SOC_VALUE_TYPE` is set to other than `1` https://github.com/Louisvdw/dbus-serialbattery/pull/424#issuecomment-1440511018 -* Changed: Improved JBD BMS soc calculation https://github.com/Louisvdw/dbus-serialbattery/pull/439 +* Changed: Improved JBD BMS soc calculation https://github.com/Louisvdw/dbus-serialbattery/pull/439 by @aaronreek * Changed: Logging to get relevant data * Changed: Moved ble part to `installble.sh` * Changed: Optimized installation scripts diff --git a/etc/dbus-serialbattery/daly.py b/etc/dbus-serialbattery/daly.py index ae96c723..492b0500 100644 --- a/etc/dbus-serialbattery/daly.py +++ b/etc/dbus-serialbattery/daly.py @@ -20,7 +20,7 @@ def __init__(self, port, baud, address): # command bytes [StartFlag=A5][Address=40][Command=94][DataLength=8][8x zero bytes][checksum] command_base = b"\xA5\x40\x94\x08\x00\x00\x00\x00\x00\x00\x00\x00\x81" - cellvolt_buffer = b"\xA5\x40\x94\x08\x00\x00\x00\x00\x00\x00\x00\x00\x82\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + cellvolt_buffer = b"\xA5\x40\x94\x08\x00\x00\x00\x00\x00\x00\x00\x00\x82" command_soc = b"\x90" command_minmax_cell_volts = b"\x91" command_minmax_temp = b"\x92" @@ -31,7 +31,7 @@ def __init__(self, port, baud, address): command_cell_balance = b"\x97" command_alarm = b"\x98" BATTERYTYPE = "Daly" - LENGTH_CHECK = 4 + LENGTH_CHECK = 1 LENGTH_POS = 3 CURRENT_ZERO_CONSTANT = 30000 TEMP_ZERO_CONSTANT = 40 @@ -246,13 +246,16 @@ def read_cells_volts(self, ser): buffer[1] = self.command_address[0] # Always serial 40 or 80 buffer[2] = self.command_cell_volts[0] - maxFrame = int(self.cell_count / 3) + 1 + if (int(self.cell_count) % 3) == 0: + maxFrame = (int(self.cell_count / 3)) + else: + maxFrame = (int(self.cell_count / 3) + 1) lenFixed = ( - maxFrame * 12 - ) # 0xA5, 0x01, 0x95, 0x08 + 1 byte frame + 6 byte data + 1byte reserved + maxFrame * 13 + ) # 0xA5, 0x01, 0x95, 0x08 + 1 byte frame + 6 byte data + 1byte reserved + chksum cells_volts_data = read_serialport_data( - ser, buffer, self.LENGTH_POS, self.LENGTH_CHECK, lenFixed + ser, buffer, self.LENGTH_POS, 0, lenFixed ) if cells_volts_data is False: logger.warning("read_cells_volts") @@ -269,14 +272,19 @@ def read_cells_volts(self, ser): for idx in range(self.cell_count): self.cells.append(Cell(True)) + # logger.warning("data " + bytes(cells_volts_data).hex()) + while ( bufIdx < len(cells_volts_data) - 4 ): # we at least need 4 bytes to extract the identifiers b1, b2, b3, b4 = unpack_from(">BBBB", cells_volts_data, bufIdx) if b1 == 0xA5 and b2 == 0x01 and b3 == 0x95 and b4 == 0x08: - frame, frameCell[0], frameCell[1], frameCell[2] = unpack_from( - ">Bhhh", cells_volts_data, bufIdx + 4 + frame, frameCell[0], frameCell[1], frameCell[2], _, chk = unpack_from( + ">BhhhBB", cells_volts_data, bufIdx + 4 ) + if sum(cells_volts_data[bufIdx:bufIdx+12]) & 0xFF != chk: + logger.warning("bad cell voltages checksum") + return False for idx in range(3): cellnum = ( (frame - 1) * 3 @@ -287,9 +295,9 @@ def read_cells_volts(self, ser): self.cells[cellnum].voltage = ( None if cellVoltage < lowMin else cellVoltage ) - bufIdx += 10 # BBBBBhhh -> 11 byte - bufIdx += 1 - + bufIdx += 13 # BBBBBhhhBB -> 13 byte + else: + logger.warning("bad cell voltages header") return True def read_cell_voltage_range_data(self, ser): @@ -359,7 +367,7 @@ def read_serial_data_daly(self, ser, command): start, flag, command_ret, length = unpack_from("BBBB", data) checksum = sum(data[:-1]) & 0xFF - if start == 165 and length == 8 and checksum == data[12]: + if start == 165 and length == 8 and len(data)>12 and checksum == data[12]: return data[4 : length + 4] else: logger.error(">>> ERROR: Incorrect Reply") diff --git a/etc/dbus-serialbattery/utils.py b/etc/dbus-serialbattery/utils.py index 5bd66408..7cf6eac6 100644 --- a/etc/dbus-serialbattery/utils.py +++ b/etc/dbus-serialbattery/utils.py @@ -441,8 +441,10 @@ def read_serialport_data( count = 0 data = bytearray(res) - while len(data) <= length + length_check: - res = ser.read(length + length_check) + + packetlen = length_fixed if length_fixed is not None else length_pos + length_byte_size + length + length_check + while len(data) < packetlen: + res = ser.read(packetlen - len(data)) data.extend(res) # logger.info('serial data length ' + str(len(data))) sleep(0.005)