Skip to content

Commit

Permalink
Merge pull request #22 from transistorgit/master
Browse files Browse the repository at this point in the history
fix sporadic init failures, improve daly serial comm error rate
  • Loading branch information
mr-manuel committed Mar 9, 2024
2 parents 3ec053a + 4e5cbee commit 87e99ae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Empty file modified etc/dbus-serialbattery/battery.py
100644 → 100755
Empty file.
36 changes: 20 additions & 16 deletions etc/dbus-serialbattery/bms/daly.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ def __init__(self, port, baud, address):
"force_discharging_off_callback",
]

# 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"
# command bytes [StartFlag=A5][Address=40][Command=94][DataLength=8][8x fill bytes][checksum]
# use 0xAA (or 0x55) as fill bytes to allow the daly's "weak" uart to sync better - this reduces read errors dramatically
command_base = b"\xA5\x40\x94\x08\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\x00"
command_set_soc = b"\x21"
command_rated_params = b"\x50"
command_batt_details = b"\x53"
Expand Down Expand Up @@ -71,6 +72,8 @@ def test_connection(self):
if result:
self.read_soc_data(ser)
self.read_battery_code(ser)
self.read_capacity(ser)
self.read_production_date(ser)

except Exception:
(
Expand All @@ -92,7 +95,7 @@ def test_connection(self):
return result

def get_settings(self):
self.capacity = utils.BATTERY_CAPACITY
self.capacity = utils.BATTERY_CAPACITY if not None else 0.0
with open_serial_port(self.port, self.baud_rate) as ser:
self.read_capacity(ser)
self.read_production_date(ser)
Expand Down Expand Up @@ -198,6 +201,7 @@ def refresh_data(self):

if not result: # TROUBLESHOOTING for no reply errors
logger.info("refresh_data: result: " + str(result))

return result

def update_soc(self, ser):
Expand All @@ -217,14 +221,17 @@ def read_status_data(self, ser):
logger.debug("No data received in read_status_data()")
return False

(
self.cell_count,
self.temp_sensors,
self.charger_connected,
self.load_connected,
state,
self.cycles,
) = unpack_from(">bb??bhx", status_data)
try:
(
self.cell_count,
self.temp_sensors,
self.charger_connected,
self.load_connected,
state,
self.cycles,
) = unpack_from(">bb??bhx", status_data)
except Exception:
return False

self.max_battery_voltage = utils.MAX_CELL_VOLTAGE * self.cell_count
self.min_battery_voltage = utils.MIN_CELL_VOLTAGE * self.cell_count
Expand Down Expand Up @@ -505,7 +512,6 @@ def read_fed_data(self, ser):
self.capacity_remain = capacity_remain / 1000
return True

# new
def read_capacity(self, ser):
capa_data = self.request_data(ser, self.command_rated_params)
# check if connection success
Expand All @@ -514,13 +520,12 @@ def read_capacity(self, ser):
return False

(capacity, cell_volt) = unpack_from(">LL", capa_data)
if capacity and capacity > 0:
if capacity is not None and capacity > 0:
self.capacity = capacity / 1000
return True
else:
return False

# new
def read_production_date(self, ser):
production = self.request_data(ser, self.command_batt_details)
# check if connection success
Expand All @@ -532,7 +537,6 @@ def read_production_date(self, ser):
self.production = f"{year + 2000}{month:02d}{day:02d}"
return True

# new
def read_battery_code(self, ser):
data = self.request_data(ser, self.command_batt_code, sentences_to_receive=5)

Expand Down Expand Up @@ -560,7 +564,7 @@ def unique_identifier(self) -> str:
"""
Used to identify a BMS when multiple BMS are connected
"""
if self.custom_field != "":
if self.custom_field is not None and self.custom_field != "":
return self.custom_field.replace(" ", "_")
else:
return str(self.production) + "_" + str(int(self.capacity))
Expand Down

0 comments on commit 87e99ae

Please sign in to comment.