diff --git a/buildfiles.lst b/buildfiles.lst index 364730f9..ae9d1bc5 100644 --- a/buildfiles.lst +++ b/buildfiles.lst @@ -5,6 +5,9 @@ etc/dbus-serialbattery/service/log/run etc/dbus-serialbattery/LICENSE etc/dbus-serialbattery/README.md etc/dbus-serialbattery/start-serialbattery.sh +etc/dbus-serialbattery/disabledriver.sh +etc/dbus-serialbattery/installlocal.sh +etc/dbus-serialbattery/installrelease.sh etc/dbus-serialbattery/dbus-serialbattery.py etc/dbus-serialbattery/dbushelper.py etc/dbus-serialbattery/battery.py @@ -13,4 +16,6 @@ etc/dbus-serialbattery/lltjbd.py etc/dbus-serialbattery/daly.py etc/dbus-serialbattery/ant.py etc/dbus-serialbattery/util_max17853.py -etc/dbus-serialbattery/mnb.py \ No newline at end of file +etc/dbus-serialbattery/mnb.py +etc/dbus-serialbattery/jkbms.py +etc/dbus-serialbattery/sinowealth.py diff --git a/create_build.sh b/create_build.sh index 01b3658f..9ddffbb4 100644 --- a/create_build.sh +++ b/create_build.sh @@ -1,2 +1,3 @@ -dos2unix rc.local conf/serial-starter.d etc/dbus-serialbattery/service/run etc/dbus-serialbattery/service/log/run etc/dbus-serialbattery/LICENSE etc/dbus-serialbattery/README.md etc/dbus-serialbattery/start-serialbattery.sh etc/dbus-serialbattery/dbus-serialbattery.py etc/dbus-serialbattery/dbushelper.py etc/dbus-serialbattery/battery.py etc/dbus-serialbattery/utils.py etc/dbus-serialbattery/lltjbd.py etc/dbus-serialbattery/daly.py etc/dbus-serialbattery/ant.py etc/dbus-serialbattery/util_max17853.py etc/dbus-serialbattery/mnb.py etc/dbus-serialbattery/jkbms.py etc/dbus-serialbattery/sinowealth.py -tar -czvf venus-data.tar.gz --mode='a+rwX' rc.local conf/serial-starter.d etc/dbus-serialbattery/service/run etc/dbus-serialbattery/service/log/run etc/dbus-serialbattery/LICENSE etc/dbus-serialbattery/README.md etc/dbus-serialbattery/start-serialbattery.sh etc/dbus-serialbattery/dbus-serialbattery.py etc/dbus-serialbattery/dbushelper.py etc/dbus-serialbattery/battery.py etc/dbus-serialbattery/utils.py etc/dbus-serialbattery/lltjbd.py etc/dbus-serialbattery/daly.py etc/dbus-serialbattery/ant.py etc/dbus-serialbattery/util_max17853.py etc/dbus-serialbattery/mnb.py etc/dbus-serialbattery/jkbms.py etc/dbus-serialbattery/sinowealth.py +#!/bin/sh +dos2unix rc.local conf/serial-starter.d etc/dbus-serialbattery/service/run etc/dbus-serialbattery/service/log/run etc/dbus-serialbattery/LICENSE etc/dbus-serialbattery/README.md etc/dbus-serialbattery/start-serialbattery.sh etc/dbus-serialbattery/disabledriver.sh etc/dbus-serialbattery/installlocal.sh etc/dbus-serialbattery/installrelease.sh etc/dbus-serialbattery/dbus-serialbattery.py etc/dbus-serialbattery/dbushelper.py etc/dbus-serialbattery/battery.py etc/dbus-serialbattery/utils.py etc/dbus-serialbattery/lltjbd.py etc/dbus-serialbattery/daly.py etc/dbus-serialbattery/ant.py etc/dbus-serialbattery/util_max17853.py etc/dbus-serialbattery/mnb.py etc/dbus-serialbattery/jkbms.py etc/dbus-serialbattery/sinowealth.py +tar -czvf venus-data.tar.gz --mode='a+rwX' rc.local conf/serial-starter.d etc/dbus-serialbattery/service/run etc/dbus-serialbattery/service/log/run etc/dbus-serialbattery/LICENSE etc/dbus-serialbattery/README.md etc/dbus-serialbattery/start-serialbattery.sh etc/dbus-serialbattery/disabledriver.sh etc/dbus-serialbattery/installlocal.sh etc/dbus-serialbattery/installrelease.sh etc/dbus-serialbattery/dbus-serialbattery.py etc/dbus-serialbattery/dbushelper.py etc/dbus-serialbattery/battery.py etc/dbus-serialbattery/utils.py etc/dbus-serialbattery/lltjbd.py etc/dbus-serialbattery/daly.py etc/dbus-serialbattery/ant.py etc/dbus-serialbattery/util_max17853.py etc/dbus-serialbattery/mnb.py etc/dbus-serialbattery/jkbms.py etc/dbus-serialbattery/sinowealth.py diff --git a/etc/dbus-serialbattery/ant.py b/etc/dbus-serialbattery/ant.py index 55db5378..604b6d70 100644 --- a/etc/dbus-serialbattery/ant.py +++ b/etc/dbus-serialbattery/ant.py @@ -99,11 +99,11 @@ def read_serial_data_ant(self, command): data = read_serial_data(command, self.port, self.baud_rate, self.LENGTH_POS, self.LENGTH_CHECK, self.LENGTH_FIXED) if data is False: - logger.info(">>> ERROR: Incorrect Data") + logger.error(">>> ERROR: Incorrect Data") return False if len(data) == self.LENGTH_FIXED: return data else: - logger.info(">>> ERROR: Incorrect Reply") + logger.error(">>> ERROR: Incorrect Reply") return False diff --git a/etc/dbus-serialbattery/battery.py b/etc/dbus-serialbattery/battery.py index 1053e4f7..3963d6ad 100644 --- a/etc/dbus-serialbattery/battery.py +++ b/etc/dbus-serialbattery/battery.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function, unicode_literals -import utils +from utils import * import math class Protection(object): @@ -105,7 +105,7 @@ def manage_charge_current(self): # Change depending on the SOC values if 98 < self.soc <= 100: self.control_charge_current = 5 - elif 95 < self.soc <= 97: + elif 95 < self.soc <= 98: self.control_charge_current = self.max_battery_current/4 elif 91 < self.soc <= 95: self.control_charge_current = self.max_battery_current/2 @@ -113,11 +113,11 @@ def manage_charge_current(self): self.control_charge_current = self.max_battery_current # Change depending on the SOC values - if self.soc <= 20: + if self.soc <= 10: self.control_discharge_current = 5 - elif 20 < self.soc <= 30: + elif 10 < self.soc <= 20: self.control_discharge_current = self.max_battery_discharge_current/4 - elif 30 < self.soc <= 35: + elif 20 < self.soc <= 30: self.control_discharge_current = self.max_battery_discharge_current/2 else: self.control_discharge_current = self.max_battery_discharge_current @@ -148,52 +148,52 @@ def get_max_cell(self): def get_min_cell_desc(self): cell_no = self.get_min_cell() - if cell_no is None: - return None - return 'C' + str(cell_no + 1) + return cell_no if cell_no is None else 'C' + str(cell_no + 1) def get_max_cell_desc(self): cell_no = self.get_max_cell() - if cell_no is None: - return None - return 'C' + str(cell_no + 1) + return cell_no if cell_no is None else 'C' + str(cell_no + 1) def get_min_cell_voltage(self): - min_voltage = 9999 + min_voltage = None if len(self.cells) == 0 and hasattr(self, 'cell_min_voltage'): return self.cell_min_voltage - for c in range(min(len(self.cells), self.cell_count)): - if self.cells[c].voltage is not None and min_voltage > self.cells[c].voltage: - min_voltage = self.cells[c].voltage - return None if min_voltage == 9999 else min_voltage + try: + min_voltage = min(c.voltage for c in self.cells if c.voltage is not None) + except ValueError: + pass + return min_voltage def get_max_cell_voltage(self): - max_voltage = 0 + max_voltage = None if len(self.cells) == 0 and hasattr(self, 'cell_max_voltage'): return self.cell_max_voltage - for c in range(min(len(self.cells), self.cell_count)): - if self.cells[c].voltage is not None and max_voltage < self.cells[c].voltage: - max_voltage = self.cells[c].voltage - return None if max_voltage == 0 else max_voltage + try: + min_voltage = max(c.voltage for c in self.cells if c.voltage is not None) + except ValueError: + pass + return min_voltage def get_midvoltage(self): - if self.cell_count is None or self.cell_count == 0: + if self.cell_count is None or self.cell_count == 0 or self.cell_count < 4 or len(self.cells) != self.cell_count: return None, None halfcount = int(math.floor(self.cell_count/2)) half1voltage = 0 half2voltage = 0 - for c in range(halfcount): - half1voltage += self.cells[c].voltage - half2voltage += self.cells[halfcount + c].voltage - # handle uneven cells + try: + half1voltage = sum(c.voltage for c in self.cells[:halfcount] if c.voltage is not None) + half2voltage = sum(c.voltage for c in self.cells[halfcount:halfcount*2] if c.voltage is not None) + except ValueError: + pass + # handle uneven cells by giving half the voltage of the last cell to half1 and half2 extra = 0 if (2*halfcount == self.cell_count) else self.cells[self.cell_count-1].voltage/2 # get the midpoint of the battery midpoint = (half1voltage + half2voltage)/2 + extra - return midpoint, abs(1 - half1voltage/half2voltage) * 100 + return midpoint, abs(1 - half1voltage/half2voltage)*100 def get_balancing(self): for c in range(min(len(self.cells), self.cell_count)): @@ -230,4 +230,15 @@ def get_max_temp(self): return self.temp2 else: return None - \ No newline at end of file + + def log_cell_data(self): + if logger.getEffectiveLevel() > logging.INFO and len(self.cells) == 0: + return False + + cell_res = "" + cell_counter = 1 + for c in self.cells: + cell_res += "[{0}]{1}V ".format(cell_counter, c.voltage) + cell_counter = cell_counter + 1 + logger.info("Cells:" + cell_res) + return True \ No newline at end of file diff --git a/etc/dbus-serialbattery/dbus-serialbattery.py b/etc/dbus-serialbattery/dbus-serialbattery.py index 56474cf7..baa591af 100644 --- a/etc/dbus-serialbattery/dbus-serialbattery.py +++ b/etc/dbus-serialbattery/dbus-serialbattery.py @@ -10,13 +10,12 @@ if sys.version_info.major == 2: import gobject else: - from gi.repository import GObject as gobject -import logging + from gi.repository import GLib as gobject import sys from dbushelper import DbusHelper -from utils import DRIVER_VERSION, DRIVER_SUBVERSION -import battery +from utils import DRIVER_VERSION, DRIVER_SUBVERSION, logger +import logging from lltjbd import LltJbd from daly import Daly from ant import Ant @@ -24,10 +23,8 @@ from sinowealth import Sinowealth #from mnb import MNB -# Logging -logging.info('Starting dbus-serialbattery') -logger = logging.getLogger(__name__) -logger.setLevel(logging.INFO) + +logger.info('Starting dbus-serialbattery') def main(): @@ -56,9 +53,9 @@ def get_battery_type(_port): while count > 0: # create a new battery object that can read the battery and run connection test for test in battery_types: - logger.info('Testing ' + test.__class__.__name__) + logger.warning('Testing ' + test.__class__.__name__) if test.test_connection() is True: - logger.info('Connection established to ' + test.__class__.__name__) + logger.warning('Connection established to ' + test.__class__.__name__) return test count -= 1 @@ -75,7 +72,7 @@ def get_port(): logger.info('No Port needed') return '/dev/tty/USB9' - logger.info('dbus-serialbattery v' + str(DRIVER_VERSION) + DRIVER_SUBVERSION) + logger.warning('dbus-serialbattery v' + str(DRIVER_VERSION) + DRIVER_SUBVERSION) port = get_port() battery = get_battery_type(port) @@ -96,7 +93,7 @@ def get_port(): if not helper.setup_vedbus(): logger.error("ERROR >>> Problem with battery set up at " + port) return - logger.info('Battery connected to dbus from ' + port) + logger.warning('Battery connected to dbus from ' + port) # Poll the battery at INTERVAL and run the main loop diff --git a/etc/dbus-serialbattery/dbushelper.py b/etc/dbus-serialbattery/dbushelper.py index c24a1786..79816768 100644 --- a/etc/dbus-serialbattery/dbushelper.py +++ b/etc/dbus-serialbattery/dbushelper.py @@ -44,7 +44,7 @@ def handle_changed_setting(self, setting, oldvalue, newvalue): if setting == 'instance': self.battery.role, self.instance = self.get_role_instance() self._dbusservice['/DeviceInstance'] = self.instance - logger.info("DeviceInstance = %d", self.instance) + logger.debug("DeviceInstance = %d", self.instance) return def setup_vedbus(self): @@ -195,6 +195,7 @@ def publish_dbus(self): self._dbusservice['/Alarms/HighTemperature'] = self.battery.protection.temp_high_discharge self._dbusservice['/Alarms/LowTemperature'] = self.battery.protection.temp_low_discharge - logging.debug("logged to dbus ", round(self.battery.voltage / 100, 2), + logger.debug("logged to dbus ", round(self.battery.voltage / 100, 2), round(self.battery.current / 100, 2), round(self.battery.soc, 2)) + self.battery.log_cell_data() diff --git a/etc/dbus-serialbattery/disabledriver.sh b/etc/dbus-serialbattery/disabledriver.sh new file mode 100644 index 00000000..1b141d39 --- /dev/null +++ b/etc/dbus-serialbattery/disabledriver.sh @@ -0,0 +1,2 @@ +#!/bin/sh +rm -f /data/conf/serial-starter.d \ No newline at end of file diff --git a/etc/dbus-serialbattery/installlocal.sh b/etc/dbus-serialbattery/installlocal.sh new file mode 100644 index 00000000..1ed62c46 --- /dev/null +++ b/etc/dbus-serialbattery/installlocal.sh @@ -0,0 +1,3 @@ +#!/bin/sh +tar -zxf venus-data.tar.gz -C /data +sh /data/rc.local \ No newline at end of file diff --git a/etc/dbus-serialbattery/installrelease.sh b/etc/dbus-serialbattery/installrelease.sh new file mode 100644 index 00000000..406f29d0 --- /dev/null +++ b/etc/dbus-serialbattery/installrelease.sh @@ -0,0 +1,4 @@ +#!/bin/sh +curl -s https://api.github.com/repos/Louisvdw/dbus-serialbattery/releases/latest | grep "browser_download_url.*gz" | cut -d : -f 2,3 | tr -d \" | wget -O venus-data.tar.gz -qi - +tar -zxf venus-data.tar.gz -C /data +sh /data/rc.local \ No newline at end of file diff --git a/etc/dbus-serialbattery/jkbms.py b/etc/dbus-serialbattery/jkbms.py index 0f1f3638..73528162 100644 --- a/etc/dbus-serialbattery/jkbms.py +++ b/etc/dbus-serialbattery/jkbms.py @@ -14,7 +14,7 @@ def __init__(self, port,baud): BATTERYTYPE = "Jkbms" LENGTH_CHECK = 1 LENGTH_POS = 2 - LENGTH_SIZE = '>H' + LENGTH_SIZE = 'H' CURRENT_ZERO_CONSTANT = 32768 command_status = b"\x4E\x57\x00\x13\x00\x00\x00\x00\x06\x03\x00\x00\x00\x00\x00\x00\x68\x00\x00\x01\x29" @@ -49,8 +49,8 @@ def refresh_data(self): return result def get_data(self, bytes, idcode, start, length): - # logger.info("start "+str(start) + " length " + str(length)) - # logger.info(binascii.hexlify(bytearray(bytes[start:start + 1 + length])).decode('ascii')) + # logger.debug("start "+str(start) + " length " + str(length)) + # logger.debug(binascii.hexlify(bytearray(bytes[start:start + 1 + length])).decode('ascii')) start = bytes.find(idcode, start, start + 1 + length) if start < 0: return False return bytes[start+1:start+length+1] @@ -123,7 +123,7 @@ def to_fet_bits(self, byte_data): def to_protection_bits(self, byte_data): pos=13 tmp = bin(byte_data)[15-pos:].rjust(pos + 1, zero_char) - # logger.info(tmp) + # logger.debug(tmp) self.protection.soc_low = 2 if is_bit_set(tmp[pos-0]) else 0 self.protection.set_IC_inspection = 2 if is_bit_set(tmp[pos-1]) else 0 # BMS over temp self.protection.voltage_high = 2 if is_bit_set(tmp[pos-2]) else 0 @@ -146,10 +146,22 @@ def read_serial_data_jkbms(self, command): return False start, length = unpack_from('>HH', data) - end, crc = unpack_from('>BI', data[-5:]) - - if start == 0x4E57 and end == 0x68: + terminal = unpack_from('>L', data[4:])[0] + cmd, src, tt = unpack_from('>bbb', data[8:]) + frame = unpack_from('>H', data[-9:])[0] + end, crc_hi, crc_lo = unpack_from('>BHH', data[-5:]) + + s = sum(data[0:-4]) + logger.debug('S%d C%d C%d' % (s, crc_lo, crc_hi)) + logger.debug('T%d C%d S%d F%d TT%d' % (terminal, cmd, src, frame, tt)) + + if start == 0x4E57 and end == 0x68 and s == crc_lo: return data[10:length-19] + elif s != crc_lo: + logger.error('CRC checksum mismatch: Expected 0x%04x, Got 0x%04x' % (crc_lo, s)) + return False else: logger.error(">>> ERROR: Incorrect Reply ") return False + + \ No newline at end of file diff --git a/etc/dbus-serialbattery/lltjbd.py b/etc/dbus-serialbattery/lltjbd.py index fa62cd0c..7228e605 100644 --- a/etc/dbus-serialbattery/lltjbd.py +++ b/etc/dbus-serialbattery/lltjbd.py @@ -1,14 +1,9 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function, unicode_literals from battery import Protection, Battery, Cell -import logging from utils import * from struct import * -# Logging -logger = logging.getLogger(__name__) -logger.setLevel(logging.INFO) - class LltJbdProtection(Protection): def __init__(self): @@ -125,14 +120,12 @@ def read_gen_data(self): return False voltage, current, capacity_remain, capacity, self.cycles, self.production, balance, \ - balance2, protection, version, self.soc, fet, self.cell_count, self.temp_sensors, temp1, temp2 \ - = unpack_from('>HhHHHHhHHBBBBBHH', gen_data) + balance2, protection, version, self.soc, fet, self.cell_count, self.temp_sensors \ + = unpack_from('>HhHHHHhHHBBBBB', gen_data) self.voltage = voltage / 100 self.current = current / 100 self.capacity_remain = capacity_remain / 100 self.capacity = capacity / 100 - self.to_temp(1, kelvin_to_celsius(temp1 / 10)) - self.to_temp(2, kelvin_to_celsius(temp2 / 10)) self.to_cell_bits(balance, balance2) self.version = float(str(version >> 4 & 0x0F) + "." + str(version & 0x0F)) self.to_fet_bits(fet) @@ -140,6 +133,10 @@ def read_gen_data(self): self.max_battery_voltage = MAX_CELL_VOLTAGE * self.cell_count self.min_battery_voltage = MIN_CELL_VOLTAGE * self.cell_count + for t in range(self.temp_sensors): + temp1 = unpack_from('>H', gen_data, 23 + (2*t))[0] + self.to_temp(t + 1, kelvin_to_celsius(temp1 / 10)) + return True def read_cell_data(self): @@ -164,7 +161,7 @@ def read_hardware_data(self): return False self.hardware_version = unpack_from('>' + str(len(hardware_data)) + 's', hardware_data)[0] - logger.info(self.hardware_version) + logger.debug(self.hardware_version) return True def read_serial_data_llt(self, command): diff --git a/etc/dbus-serialbattery/sinowealth.py b/etc/dbus-serialbattery/sinowealth.py index 63992789..087ad176 100755 --- a/etc/dbus-serialbattery/sinowealth.py +++ b/etc/dbus-serialbattery/sinowealth.py @@ -10,12 +10,6 @@ def __init__(self, port,baud): super(Sinowealth, self).__init__(port,baud) self.charger_connected = None self.load_connected = None - self.cell_min_voltage = None - self.cell_max_voltage = None - self.cell_min_no = None - self.cell_max_no = None - self.cell_count = None - self.cell_voltages = {} self.poll_interval = 2000 self.type = self.BATTERYTYPE # command bytes [StartFlag=0A][Command byte][response dataLength=2 to 20 bytes][checksum] @@ -60,6 +54,8 @@ def get_settings(self): self.hardware_version = "Daly/Sinowealth BMS " + str(self.cell_count) + " cells" logger.info(self.hardware_version) + self.read_capacity() + for c in range(self.cell_count): self.cells.append(Cell(False)) return True @@ -140,8 +136,9 @@ def read_pack_voltage(self): if pack_voltage_data is False: return False pack_voltage = unpack_from('>H', pack_voltage_data[:-1]) - logger.info(">>> INFO: current pack voltage: %f", pack_voltage[0]/1000) - self.voltage = pack_voltage[0]/1000 + pack_voltage = pack_voltage[0]/1000 + logger.info(">>> INFO: current pack voltage: %f", pack_voltage) + self.voltage = pack_voltage return True def read_pack_current(self): @@ -149,8 +146,9 @@ def read_pack_current(self): if current_data is False: return False current = unpack_from('>i', current_data[:-1]) - logger.info(">>> INFO: current pack current: %f", current[0]/1000) - self.current = current[0]/1000 + current = current[0]/1000 + logger.info(">>> INFO: current pack current: %f", current) + self.current = current return True def read_remaining_capacity(self): @@ -158,11 +156,11 @@ def read_remaining_capacity(self): if remaining_capacity_data is False: return False remaining_capacity = unpack_from('>i', remaining_capacity_data[:-1]) - logger.info(">>> INFO: remaining battery capacity: %f Ah", remaining_capacity[0]/1000) self.capacity_remain = remaining_capacity[0]/1000 - if self.capacity is None: - self.read_capacity() - self.total_ah_drawn = self.capacity - self.capacity_remain + logger.info(">>> INFO: remaining battery capacity: %f Ah", self.capacity_remain) + + if self.capacity is not None and self.capacity_remain is not None: + self.total_ah_drawn = self.capacity - self.capacity_remain return True def read_capacity(self): @@ -191,34 +189,31 @@ def read_pack_config_data(self): def read_cell_data(self): if self.cell_count is None: self.read_pack_config_data() - - cell_index = 1 - while cell_index <= self.cell_count: - self.cells[cell_index - 1].voltage = self.read_cell_voltage(cell_index) - if self.cells[cell_index - 1].voltage is None: - return False - cell_index += 1 - + + for c in range(self.cell_count): + self.cells[c].voltage = self.read_cell_voltage(c + 1) return True def read_cell_voltage(self, cell_index): cell_data = self.read_serial_data_sinowealth(str(chr(cell_index))) if cell_data is False: - return False + return None cell_voltage = unpack_from('>H', cell_data[:-1]) - logger.info(">>> INFO: Cell %u voltage: %f V", cell_index, cell_voltage[0]/1000 ) + cell_voltage = cell_voltage[0]/1000 - return cell_voltage[0]/1000 + logger.info(">>> INFO: Cell %u voltage: %f V", cell_index, cell_voltage ) + return cell_voltage def read_temperature_data(self): if self.temp_sensors is None: - self.read_pack_config_data() + return False + temp_ext1_data = self.read_serial_data_sinowealth(self.command_temp_ext1) if temp_ext1_data is False: return False temp_ext1 = unpack_from('>H', temp_ext1_data[:-1]) - self.temp1 = kelvin_to_celsius(temp_ext1[0]/10) + self.to_temp(1, kelvin_to_celsius(temp_ext1[0]/10)) logger.info(">>> INFO: BMS external temperature 1: %f C", self.temp1 ) if self.temp_sensors == 2: @@ -227,7 +222,7 @@ def read_temperature_data(self): return False temp_ext2 = unpack_from('>H', temp_ext2_data[:-1]) - self.temp2 = kelvin_to_celsius(temp_ext2[0]/10) + self.to_temp(2, kelvin_to_celsius(temp_ext2[0]/10)) logger.info(">>> INFO: BMS external temperature 2: %f C", self.temp2 ) # Internal temperature 1 seems to give a logical value diff --git a/etc/dbus-serialbattery/utils.py b/etc/dbus-serialbattery/utils.py index e57abdac..f150015a 100644 --- a/etc/dbus-serialbattery/utils.py +++ b/etc/dbus-serialbattery/utils.py @@ -6,11 +6,12 @@ from struct import * # Logging -logger = logging.getLogger(__name__) -logger.setLevel(logging.INFO) +logging.basicConfig() +logger = logging.getLogger("SerialBattery") +logger.setLevel(logging.WARNING) # Constants - Need to dynamically get them in future -DRIVER_VERSION = 0.8 +DRIVER_VERSION = 0.9 DRIVER_SUBVERSION = '' zero_char = chr(48) degree_sign = u'\N{DEGREE SIGN}' @@ -39,24 +40,35 @@ def read_serial_data(command, port, baud, length_pos, length_check, length_fixed ser.flushInput() ser.write(command) + length_byte_size = 1 + if length_size is not None: + if length_size.upper() == 'H': + length_byte_size = 2 + elif length_size.upper() == 'I' or length_size.upper() == 'L': + length_byte_size = 4 + count = 0 toread = ser.inWaiting() - while toread < (length_pos+1): + while toread < (length_pos+length_byte_size): sleep(0.005) toread = ser.inWaiting() count += 1 if count > 50: logger.error(">>> ERROR: No reply - returning") return False - # raise Exception("No reply from {}".format(port)) + #logger.info('serial data toread ' + str(toread)) res = ser.read(toread) - if len(res) < length_pos and length_fixed is None: - logger.error(">>> ERROR: No reply - returning") - return False - length_size = length_size if length_size is not None else 'B' - length = length_fixed if length_fixed is not None else unpack_from(length_size, res,length_pos)[0] + if length_fixed is not None: + length = length_fixed + else: + if len(res) < (length_pos+length_byte_size): + logger.error(">>> ERROR: No reply - returning") + return False + length_size = length_size if length_size is not None else 'B' + length = unpack_from('>'+length_size, res,length_pos)[0] + #logger.info('serial data length ' + str(length)) count = 0