Skip to content

Commit

Permalink
added debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-manuel committed Nov 3, 2023
1 parent fb9b213 commit a12a27c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
20 changes: 13 additions & 7 deletions etc/dbus-serialbattery/bms/daly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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]
2 changes: 2 additions & 0 deletions etc/dbus-serialbattery/bms/jkbms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 6 additions & 3 deletions etc/dbus-serialbattery/bms/jkbms_brn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions etc/dbus-serialbattery/bms/lltjbd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion etc/dbus-serialbattery/dbus-serialbattery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
)
Expand Down
6 changes: 5 additions & 1 deletion etc/dbus-serialbattery/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a12a27c

Please sign in to comment.