Skip to content

Commit

Permalink
Fixes for multi BMS on single USB-adapter (#67)
Browse files Browse the repository at this point in the history
* Fixes for multi bms types whereby adresses get overwritten, and seplosv3 optimization

* Fixes for multi bms types whereby adresses get overwritten, and seplosv3 optimization. Fixed formatting.
  • Loading branch information
cpttinkering authored Aug 24, 2024
1 parent adc5829 commit 91575bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
8 changes: 5 additions & 3 deletions etc/dbus-serialbattery/bms/seplosv3.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ def to_signed_int(value):
return struct.unpack("<h", packval)[0]

def get_modbus(self, slaveaddress=0) -> minimalmodbus.Instrument:
if self.mbdev is not None and slaveaddress == self.slaveaddress:
return self.mbdev

# hack to allow communication to the Seplos BMS using minimodbus which uses slaveaddress 0 as broadcast
# Make sure we re-set these values whenever we want to access the modbus. Just in case of a
# multi-device setup with different addresses and subsequent tries on a different address modified it.
if slaveaddress == 0:
minimalmodbus._SLAVEADDRESS_BROADCAST = 0xF0
else:
minimalmodbus._SLAVEADDRESS_BROADCAST = 0

if self.mbdev is not None and slaveaddress == self.slaveaddress:
return self.mbdev

mbdev = minimalmodbus.Instrument(
self.port,
slaveaddress=slaveaddress,
Expand Down
26 changes: 16 additions & 10 deletions etc/dbus-serialbattery/dbus-serialbattery.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,22 @@ def get_port() -> str:
# check if MODBUS_ADDRESSES is not empty
if utils.MODBUS_ADDRESSES:
for address in utils.MODBUS_ADDRESSES:
battery[address] = get_battery(port, address)
checkbatt = get_battery(port, address)
if checkbatt is not None:
battery[address] = checkbatt
logger.info(
"Successful battery connection at "
+ port
+ " and this Modbus address "
+ str(address)
)
else:
logger.warning(
"No battery connection at "
+ port
+ " and this Modbus address "
+ str(address)
)
# use default address
else:
battery[0] = get_battery(port)
Expand All @@ -281,15 +296,6 @@ def get_port() -> str:
for key_address in battery:
if battery[key_address] is not None:
battery_found = True
elif key_address != 0:
# remove item from battery dict so that only the found batteries are used
del battery[key_address]
logger.warning(
"No battery connection at "
+ port
+ " and this Modbus address "
+ str(key_address)
)

if not battery_found:
logger.error(
Expand Down

0 comments on commit 91575bf

Please sign in to comment.