Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes 2023.11.03 #851

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 7 additions & 4 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 @@ -166,7 +170,7 @@ def get_bms_max_cell_count(self):

# check where data starts
# for 32s it's at fb[70]
if fb[70] == 255 and fb[71] == 255:
if fb[70] == 255:
self.bms_max_cell_count = 32
self.translate_cell_info = TRANSLATE_CELL_INFO_32S
# for 16s it's at fb[54]
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