diff --git a/etc/dbus-serialbattery/bms/daly.py b/etc/dbus-serialbattery/bms/daly.py index afd5c064..0c033254 100644 --- a/etc/dbus-serialbattery/bms/daly.py +++ b/etc/dbus-serialbattery/bms/daly.py @@ -404,7 +404,7 @@ 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()) + # logger.warning("data " + bytearray_to_string(cells_volts_data)) # from each of the received sentences, read up to 3 voltages for i in range(sentences_expected): @@ -526,7 +526,7 @@ def read_battery_code(self, ser): return False battery_code = "" - # logger.warning("data " + bytes(cells_volts_data).hex()) + # logger.warning("data " + utils.bytearray_to_string(cells_volts_data)) for i in range(5): nr, part = unpack_from(">B7s", data, i * 8) if nr != i + 1: @@ -720,7 +720,7 @@ def read_sentence(self, ser, expected_reply, timeout=0.5): reply = ser.read_until(b"\xA5") if not reply or b"\xA5" not in reply: logger.debug( - f"read_sentence {bytes(expected_reply).hex()}: no sentence start received" + f"read_sentence {utils.bytearray_to_string(expected_reply)}: no sentence start received" ) return False @@ -732,21 +732,27 @@ def read_sentence(self, ser, expected_reply, timeout=0.5): toread = ser.inWaiting() time_run = time() - time_start if time_run > timeout: - logger.debug(f"read_sentence {bytes(expected_reply).hex()}: timeout") + logger.debug( + f"read_sentence {utils.bytearray_to_string(expected_reply)}: timeout" + ) return False reply += ser.read(12) _, id, cmd, length = unpack_from(">BBBB", reply) - # logger.info(f"reply: {bytes(reply).hex()}") # debug + # logger.info(f"reply: {utils.bytearray_to_string(reply)}") # debug if id != 1 or length != 8 or cmd != expected_reply[0]: - logger.debug(f"read_sentence {bytes(expected_reply).hex()}: wrong header") + logger.debug( + f"read_sentence {utils.bytearray_to_string(expected_reply)}: wrong header" + ) return False chk = unpack_from(">B", reply, 12)[0] if sum(reply[:12]) & 0xFF != chk: - logger.debug(f"read_sentence {bytes(expected_reply).hex()}: wrong checksum") + logger.debug( + f"read_sentence {utils.bytearray_to_string(expected_reply)}: wrong checksum" + ) return False return reply[4:12] diff --git a/etc/dbus-serialbattery/bms/jkbms.py b/etc/dbus-serialbattery/bms/jkbms.py index 99569e36..0a391d39 100644 --- a/etc/dbus-serialbattery/bms/jkbms.py +++ b/etc/dbus-serialbattery/bms/jkbms.py @@ -343,6 +343,8 @@ def read_serial_data_jkbms(self, command: str) -> bool: s = sum(data[0:-4]) + logger.debug("bytearray: " + utils.bytearray_to_string(data)) + if start == 0x4E57 and end == 0x68 and s == crc_lo: return data[10 : length - 7] elif s != crc_lo: diff --git a/etc/dbus-serialbattery/bms/jkbms_brn.py b/etc/dbus-serialbattery/bms/jkbms_brn.py index 5c118cf7..b4f6fb38 100644 --- a/etc/dbus-serialbattery/bms/jkbms_brn.py +++ b/etc/dbus-serialbattery/bms/jkbms_brn.py @@ -10,8 +10,12 @@ import logging logger = logging.basicConfig(level=logging.DEBUG) + + def bytearray_to_string(data): + return "".join("\\x" + format(byte, "02x") for byte in data) + else: - from utils import logger + from utils import bytearray_to_string, logger # zero means parse all incoming data (every second) CELL_INFO_REFRESH_S = 0 @@ -358,8 +362,7 @@ def assemble_frame(self, data: bytearray): def ncallback(self, sender: int, data: bytearray): logger.debug(f"--> NEW PACKAGE! lenght: {len(data)}") - logger.debug("ncallback(): data") - logger.debug(data) + logger.debug("ncallback(): " + bytearray_to_string(data)) self.assemble_frame(data) def crc(self, arr: bytearray, length: int) -> int: diff --git a/etc/dbus-serialbattery/bms/lltjbd.py b/etc/dbus-serialbattery/bms/lltjbd.py index 5c658fac..24399a40 100644 --- a/etc/dbus-serialbattery/bms/lltjbd.py +++ b/etc/dbus-serialbattery/bms/lltjbd.py @@ -610,13 +610,13 @@ def read_hardware_data(self): @staticmethod def validate_packet(data): - if not data: - return False - if data is False: return False start, op, status, payload_length = unpack_from("BBBB", data) + + logger.debug("bytearray: " + utils.bytearray_to_string(data)) + if start != 0xDD: logger.error( ">>> ERROR: Invalid response packet. Expected begin packet character 0xDD" diff --git a/etc/dbus-serialbattery/dbus-serialbattery.py b/etc/dbus-serialbattery/dbus-serialbattery.py index e4bde35e..69c90437 100644 --- a/etc/dbus-serialbattery/dbus-serialbattery.py +++ b/etc/dbus-serialbattery/dbus-serialbattery.py @@ -96,7 +96,9 @@ def get_battery(_port) -> Union[Battery, None]: "Testing " + test["bms"].__name__ + ( - ' at address "' + f"\\x{bytes(test['address']).hex()}" + '"' + ' at address "' + + utils.bytearray_to_string(test["address"]) + + '"' if "address" in test else "" ) diff --git a/etc/dbus-serialbattery/utils.py b/etc/dbus-serialbattery/utils.py index a609225a..19b01a24 100644 --- a/etc/dbus-serialbattery/utils.py +++ b/etc/dbus-serialbattery/utils.py @@ -37,7 +37,7 @@ def _get_list_from_config( # Constants -DRIVER_VERSION = "1.0.20231102dev" +DRIVER_VERSION = "1.0.20231103dev" zero_char = chr(48) degree_sign = "\N{DEGREE SIGN}" @@ -318,6 +318,10 @@ def kelvin_to_celsius(kelvin_temp): return kelvin_temp - 273.1 +def bytearray_to_string(data): + return "".join("\\x" + format(byte, "02x") for byte in data) + + def format_value(value, prefix, suffix): return ( None