From bcd20e9179b0fe82389e34de866ddbb0d8262f37 Mon Sep 17 00:00:00 2001 From: Pat Date: Sat, 14 Dec 2024 13:00:12 -0600 Subject: [PATCH 1/5] Add retry to i2c functions. --- adafruit_lc709203f.py | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/adafruit_lc709203f.py b/adafruit_lc709203f.py index 72fc171..2d843f2 100644 --- a/adafruit_lc709203f.py +++ b/adafruit_lc709203f.py @@ -62,6 +62,7 @@ LC709203F_CMD_ALARMPERCENT = const(0x13) LC709203F_CMD_ALARMVOLTAGE = const(0x14) +LC709203F_I2C_RETRY_COUNT = 10 class CV: """struct helper""" @@ -280,11 +281,18 @@ def _read_word(self, command: int) -> int: self._buf[0] = LC709203F_I2CADDR_DEFAULT * 2 # write byte self._buf[1] = command # command / register self._buf[2] = self._buf[0] | 0x1 # read byte - - with self.i2c_device as i2c: - i2c.write_then_readinto( - self._buf, self._buf, out_start=1, out_end=2, in_start=3, in_end=7 - ) + + for x in range (LC709203F_I2C_RETRY_COUNT): + try: + with self.i2c_device as i2c: + i2c.write_then_readinto(self._buf, self._buf, out_start=1, out_end=2, in_start=3, in_end=7) + break + except OSError as e: + pass + if x == (LC709203F_I2C_RETRY_COUNT-1): + #Retry count reached + raise e + crc8 = self._generate_crc(self._buf[0:5]) if crc8 != self._buf[5]: raise OSError("CRC failure on reading word") @@ -296,6 +304,14 @@ def _write_word(self, command: int, data: int) -> None: self._buf[2] = data & 0xFF self._buf[3] = (data >> 8) & 0xFF self._buf[4] = self._generate_crc(self._buf[0:4]) - - with self.i2c_device as i2c: - i2c.write(self._buf[1:5]) + + for x in range (LC709203F_I2C_RETRY_COUNT): + try: + with self.i2c_device as i2c: + i2c.write(self._buf[1:5]) + return + except OSError as e: + pass + + #Retry count reached + raise e From 7ebf09d4a59a9b500e297e9887f155298a6a6774 Mon Sep 17 00:00:00 2001 From: Pat Date: Sat, 14 Dec 2024 19:23:02 -0600 Subject: [PATCH 2/5] Add CRC check to retry loop --- adafruit_lc709203f.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/adafruit_lc709203f.py b/adafruit_lc709203f.py index 2d843f2..c12f627 100644 --- a/adafruit_lc709203f.py +++ b/adafruit_lc709203f.py @@ -286,17 +286,19 @@ def _read_word(self, command: int) -> int: try: with self.i2c_device as i2c: i2c.write_then_readinto(self._buf, self._buf, out_start=1, out_end=2, in_start=3, in_end=7) - break + + crc8 = self._generate_crc(self._buf[0:5]) + if crc8 != self._buf[5]: + raise OSError("CRC failure on reading word") + return (self._buf[4] << 8) | self._buf[3] except OSError as e: + #print("OSError: ", x, "/10: ", e) pass if x == (LC709203F_I2C_RETRY_COUNT-1): #Retry count reached raise e - crc8 = self._generate_crc(self._buf[0:5]) - if crc8 != self._buf[5]: - raise OSError("CRC failure on reading word") - return (self._buf[4] << 8) | self._buf[3] + def _write_word(self, command: int, data: int) -> None: self._buf[0] = LC709203F_I2CADDR_DEFAULT * 2 # write byte @@ -311,6 +313,7 @@ def _write_word(self, command: int, data: int) -> None: i2c.write(self._buf[1:5]) return except OSError as e: + #print("OSError: ", x, "/10: ", e) pass #Retry count reached From 83fd19bfb2446c239b48e38c6d8228c57ba5a8c9 Mon Sep 17 00:00:00 2001 From: Pat Date: Sat, 14 Dec 2024 19:46:29 -0600 Subject: [PATCH 3/5] fixes to make pylint happy --- adafruit_lc709203f.py | 51 ++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/adafruit_lc709203f.py b/adafruit_lc709203f.py index c12f627..81b1aa5 100644 --- a/adafruit_lc709203f.py +++ b/adafruit_lc709203f.py @@ -64,6 +64,7 @@ LC709203F_I2C_RETRY_COUNT = 10 + class CV: """struct helper""" @@ -281,24 +282,31 @@ def _read_word(self, command: int) -> int: self._buf[0] = LC709203F_I2CADDR_DEFAULT * 2 # write byte self._buf[1] = command # command / register self._buf[2] = self._buf[0] | 0x1 # read byte - - for x in range (LC709203F_I2C_RETRY_COUNT): + + for x in range(LC709203F_I2C_RETRY_COUNT): try: with self.i2c_device as i2c: - i2c.write_then_readinto(self._buf, self._buf, out_start=1, out_end=2, in_start=3, in_end=7) - + i2c.write_then_readinto( + self._buf, + self._buf, + out_start=1, + out_end=2, + in_start=3, + in_end=7, + ) + crc8 = self._generate_crc(self._buf[0:5]) if crc8 != self._buf[5]: raise OSError("CRC failure on reading word") return (self._buf[4] << 8) | self._buf[3] - except OSError as e: - #print("OSError: ", x, "/10: ", e) - pass - if x == (LC709203F_I2C_RETRY_COUNT-1): - #Retry count reached - raise e - - + except OSError as exception: + # print("OSError: ", x, "/10: ", exception) + if x == (LC709203F_I2C_RETRY_COUNT - 1): + # Retry count reached + raise exception + + # Code should never reach this point + return None def _write_word(self, command: int, data: int) -> None: self._buf[0] = LC709203F_I2CADDR_DEFAULT * 2 # write byte @@ -306,15 +314,18 @@ def _write_word(self, command: int, data: int) -> None: self._buf[2] = data & 0xFF self._buf[3] = (data >> 8) & 0xFF self._buf[4] = self._generate_crc(self._buf[0:4]) - - for x in range (LC709203F_I2C_RETRY_COUNT): + # exception = None + + for x in range(LC709203F_I2C_RETRY_COUNT): try: with self.i2c_device as i2c: i2c.write(self._buf[1:5]) return - except OSError as e: - #print("OSError: ", x, "/10: ", e) - pass - - #Retry count reached - raise e + except OSError as exception: + # print("OSError: ", x, "/10: ", exception) + if x == (LC709203F_I2C_RETRY_COUNT - 1): + # Retry count reached + raise exception + + # Retry count reached + # raise exception From 3650482c65c766c0ad8d46dd31841dcaca984cd3 Mon Sep 17 00:00:00 2001 From: Pat Date: Sun, 15 Dec 2024 11:58:31 -0600 Subject: [PATCH 4/5] Add try/catch to sensor read. Remove unused comments. --- adafruit_lc709203f.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/adafruit_lc709203f.py b/adafruit_lc709203f.py index 81b1aa5..8edf1e0 100644 --- a/adafruit_lc709203f.py +++ b/adafruit_lc709203f.py @@ -158,18 +158,27 @@ def init_RSOC(self) -> None: # pylint: disable=invalid-name @property def cell_voltage(self) -> float: """Returns floating point voltage""" - return self._read_word(LC709203F_CMD_CELLVOLTAGE) / 1000 - + try: + return self._read_word(LC709203F_CMD_CELLVOLTAGE) / 1000 + except OSError: + return None + @property def cell_percent(self) -> float: """Returns percentage of cell capacity""" - return self._read_word(LC709203F_CMD_CELLITE) / 10 - + try: + return self._read_word(LC709203F_CMD_CELLITE) / 10 + except OSError: + return None + @property def cell_temperature(self) -> float: """Returns the temperature of the cell""" - return self._read_word(LC709203F_CMD_CELLTEMPERATURE) / 10 - 273.15 - + try: + return self._read_word(LC709203F_CMD_CELLTEMPERATURE) / 10 - 273.15 + except OSError: + return None + @cell_temperature.setter def cell_temperature(self, value: float) -> None: """Sets the temperature in the LC709203F""" @@ -300,12 +309,12 @@ def _read_word(self, command: int) -> int: raise OSError("CRC failure on reading word") return (self._buf[4] << 8) | self._buf[3] except OSError as exception: - # print("OSError: ", x, "/10: ", exception) + print("OSError in read: ", x, "/10: ", exception) if x == (LC709203F_I2C_RETRY_COUNT - 1): # Retry count reached raise exception - # Code should never reach this point + # Code should never reach this point, add this to satisfy pylint R1710. return None def _write_word(self, command: int, data: int) -> None: @@ -314,7 +323,6 @@ def _write_word(self, command: int, data: int) -> None: self._buf[2] = data & 0xFF self._buf[3] = (data >> 8) & 0xFF self._buf[4] = self._generate_crc(self._buf[0:4]) - # exception = None for x in range(LC709203F_I2C_RETRY_COUNT): try: @@ -322,10 +330,7 @@ def _write_word(self, command: int, data: int) -> None: i2c.write(self._buf[1:5]) return except OSError as exception: - # print("OSError: ", x, "/10: ", exception) + print("OSError in write: ", x, "/10: ", exception) if x == (LC709203F_I2C_RETRY_COUNT - 1): # Retry count reached raise exception - - # Retry count reached - # raise exception From 4acfed649de7aac355dedd75f92ab0ca52451992 Mon Sep 17 00:00:00 2001 From: Pat Date: Wed, 1 Jan 2025 22:52:10 -0600 Subject: [PATCH 5/5] Remove debug outputs --- adafruit_lc709203f.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/adafruit_lc709203f.py b/adafruit_lc709203f.py index 8edf1e0..002a223 100644 --- a/adafruit_lc709203f.py +++ b/adafruit_lc709203f.py @@ -162,7 +162,7 @@ def cell_voltage(self) -> float: return self._read_word(LC709203F_CMD_CELLVOLTAGE) / 1000 except OSError: return None - + @property def cell_percent(self) -> float: """Returns percentage of cell capacity""" @@ -170,7 +170,7 @@ def cell_percent(self) -> float: return self._read_word(LC709203F_CMD_CELLITE) / 10 except OSError: return None - + @property def cell_temperature(self) -> float: """Returns the temperature of the cell""" @@ -178,7 +178,7 @@ def cell_temperature(self) -> float: return self._read_word(LC709203F_CMD_CELLTEMPERATURE) / 10 - 273.15 except OSError: return None - + @cell_temperature.setter def cell_temperature(self, value: float) -> None: """Sets the temperature in the LC709203F""" @@ -309,9 +309,10 @@ def _read_word(self, command: int) -> int: raise OSError("CRC failure on reading word") return (self._buf[4] << 8) | self._buf[3] except OSError as exception: - print("OSError in read: ", x, "/10: ", exception) + # print("OSError in read: ", x+1, "/10: ", exception) if x == (LC709203F_I2C_RETRY_COUNT - 1): # Retry count reached + # print("Retry count reached in read: ", exception) raise exception # Code should never reach this point, add this to satisfy pylint R1710. @@ -330,7 +331,8 @@ def _write_word(self, command: int, data: int) -> None: i2c.write(self._buf[1:5]) return except OSError as exception: - print("OSError in write: ", x, "/10: ", exception) + # print("OSError in write: ", x+1, "/10: ", exception) if x == (LC709203F_I2C_RETRY_COUNT - 1): # Retry count reached + # print("Retry count reached in write: ", exception) raise exception