From 725bc83ab665a44b341b2d343fc75d1cad365cc0 Mon Sep 17 00:00:00 2001 From: Jan Date: Mon, 14 Jul 2025 12:37:40 +0200 Subject: [PATCH] MainCode/adalm1000_logger.py aktualisiert def toggle_recording(self): def update_status(self): def stop_test(self): These changes add proper checks for the existence and state of current_cycle_file before trying to access its properties or methods. The program should now handle stopping live monitoring without crashing. (D) --- MainCode/adalm1000_logger.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/MainCode/adalm1000_logger.py b/MainCode/adalm1000_logger.py index 122fc6b..a8234d0 100644 --- a/MainCode/adalm1000_logger.py +++ b/MainCode/adalm1000_logger.py @@ -825,14 +825,16 @@ class BatteryTester(QMainWindow): self.start_live_monitoring() else: self.record_button.setChecked(False) + self.current_cycle_file = None # Ensure it's None if creation failed except Exception as e: print(f"Error starting recording: {e}") self.record_button.setChecked(False) + self.current_cycle_file = None # Ensure it's None on error QMessageBox.critical(self, "Error", f"Failed to start recording:\n{str(e)}") else: # Stop recording try: - if hasattr(self, 'current_cycle_file') and self.current_cycle_file: + if hasattr(self, 'current_cycle_file') and self.current_cycle_file is not None: self.finalize_log_file() self.record_button.setText("Start Recording") self.status_bar.showMessage("Live recording stopped") @@ -985,8 +987,8 @@ class BatteryTester(QMainWindow): self.capacity_label.setText(f"{self.capacity_ah:.4f}") # Logging (1x per second) - if hasattr(self, 'log_writer') and (now - self._last_log_time >= 1.0): - if self.time_data and hasattr(self, 'current_cycle_file') and not self.current_cycle_file.closed: + if hasattr(self, 'log_writer') and hasattr(self, 'current_cycle_file') and self.current_cycle_file is not None: + if self.time_data and not self.current_cycle_file.closed and (now - self._last_log_time >= 1.0): try: current_time = self.time_data[-1] voltage = self.voltage_data[-1] @@ -1018,12 +1020,13 @@ class BatteryTester(QMainWindow): self._last_log_time = now except Exception as e: print(f"Error writing to log file: {e}") - if hasattr(self, 'current_cycle_file'): + if hasattr(self, 'current_cycle_file') and self.current_cycle_file is not None: try: self.current_cycle_file.close() except: pass self.record_button.setChecked(False) + self.current_cycle_file = None def start_test(self): """Start the selected test mode""" @@ -1496,7 +1499,7 @@ class BatteryTester(QMainWindow): def stop_test(self): """Request immediate stop of the current test or monitoring""" - if not self.test_running and not self.record_button.isChecked(): + if not self.test_running and not (hasattr(self, 'record_button') and self.record_button.isChecked()): return self.request_stop = True @@ -1519,9 +1522,10 @@ class BatteryTester(QMainWindow): pass # Stop recording if active - if self.record_button.isChecked(): + if hasattr(self, 'record_button') and self.record_button.isChecked(): self.record_button.setChecked(False) - self.finalize_log_file() + if hasattr(self, 'current_cycle_file') and self.current_cycle_file is not None: + self.finalize_log_file() self.record_button.setText("Start Recording") # Reset device to safe state