Skip to content

Commit

Permalink
Call serial.close() before exiting
Browse files Browse the repository at this point in the history
  • Loading branch information
dreadnought committed Jan 23, 2022
1 parent db767dd commit da76999
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
29 changes: 16 additions & 13 deletions bin/daly-bms-cli
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,27 @@ parser.add_argument("--verbose", help="Verbose output", action="store_true")
parser.add_argument("--mqtt", help="Write output to MQTT", action="store_true")
parser.add_argument("--mqtt-hass", help="MQTT Home Assistant Mode", action="store_true")

parser.add_argument("--mqtt-topic",
help="MQTT topic to write to. default daly_bms",
parser.add_argument("--mqtt-topic",
help="MQTT topic to write to. default daly_bms",
type=str,
default="daly_bms")


parser.add_argument("--mqtt-broker",
help="MQTT broker (server). default localhost",
parser.add_argument("--mqtt-broker",
help="MQTT broker (server). default localhost",
type=str,
default="localhost")

parser.add_argument("--mqtt-port",
help="MQTT port. default 1883",
parser.add_argument("--mqtt-port",
help="MQTT port. default 1883",
type=int,
default=1883)

parser.add_argument("--mqtt-user",
help="Username to authenticate MQTT with",
parser.add_argument("--mqtt-user",
help="Username to authenticate MQTT with",
type=str)

parser.add_argument("--mqtt-password",
help="Password to authenticate MQTT with",
parser.add_argument("--mqtt-password",
help="Password to authenticate MQTT with",
type=str)

args = parser.parse_args()
Expand Down Expand Up @@ -78,9 +77,11 @@ bms.connect(device=args.device)

result = False

mqtt_client = None
if args.mqtt:
import paho.mqtt.client as paho
mqtt_client = paho.Client()

mqtt_client = paho.Client()
mqtt_client.enable_logger(logger)
mqtt_client.username_pw_set(args.mqtt_user, args.mqtt_password)
mqtt_client.connect(args.mqtt_broker, port=args.mqtt_port)
Expand Down Expand Up @@ -214,8 +215,10 @@ if args.set_discharge_mosfet:

result = bms.set_discharge_mosfet(on=on)

if args.mqtt:
if mqtt_client:
mqtt_client.disconnect()

bms.disconnect()

if not result:
sys.exit(1)
8 changes: 6 additions & 2 deletions dalybms/daly_bms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from .error_codes import ERROR_CODES


class DalyBMS:
def __init__(self, request_retries=3, address=4, logger=None):
"""
Expand Down Expand Up @@ -40,6 +41,10 @@ def connect(self, device):
)
self.get_status()

def disconnect(self):
if self.serial and self.serial.is_open:
self.serial.close()

@staticmethod
def _calc_crc(message_bytes):
"""
Expand Down Expand Up @@ -93,7 +98,7 @@ def _read_request(self, command, extra="", max_responses=1, return_list=False):

def _read(self, command, extra="", max_responses=1, return_list=False):
self.logger.debug("-- %s ------------------------" % command)
if not self.serial.isOpen():
if not self.serial.is_open:
self.serial.open()
message_bytes = self._format_message(command, extra=extra)

Expand Down Expand Up @@ -236,7 +241,6 @@ def get_status(self, response_data=None):
self.status = data
return data


def _calc_num_responses(self, status_field, num_per_frame):
if not self.status:
self.logger.error("get_status has to be called at least once before calling get_cell_voltages")
Expand Down
14 changes: 9 additions & 5 deletions dalybms/daly_sinowealth.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,18 @@ def connect(self, device):
writeTimeout=0.5
)

def disconnect(self):
if self.serial and self.serial.is_open:
self.serial.close()

def _format_message(self, command, length):
message = "0a%s0%s" % (command.zfill(2), length)
message_bytes = bytearray.fromhex(message)
self.logger.debug("message: %s, %s" % (message_bytes, message_bytes.hex()))
return message_bytes

def _read(self, command):
if not self.serial.isOpen():
if not self.serial.is_open:
self.serial.open()
if command in ("10", "11", "12"):
length = 4
Expand Down Expand Up @@ -167,7 +171,7 @@ def get_temperatures(self):

for key, value in responses.items():
# change temperatures from Kelvin to °C
responses[key] = round(value-273, 2)
responses[key] = round(value - 273, 2)
return responses

def get_status(self):
Expand Down Expand Up @@ -226,12 +230,12 @@ def get_balancing_status(self):
def get_all(self):
return {
"soc": self.get_soc(),
#"cell_voltage_range": self.get_cell_voltage_range(),
#"temperature_range": self.get_temperature_range(),
# "cell_voltage_range": self.get_cell_voltage_range(),
# "temperature_range": self.get_temperature_range(),
"mosfet_status": self.get_mosfet_status(),
"status": self.get_status(),
"cell_voltages": self.get_cell_voltages(),
"temperatures": self.get_temperatures(),
#"balancing_status": self.get_balancing_status(),
# "balancing_status": self.get_balancing_status(),
"errors": self.get_errors()
}

0 comments on commit da76999

Please sign in to comment.