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)
This commit is contained in:
Jan 2025-07-14 12:37:40 +02:00
parent 60fc215f30
commit 725bc83ab6

View File

@ -825,14 +825,16 @@ class BatteryTester(QMainWindow):
self.start_live_monitoring() self.start_live_monitoring()
else: else:
self.record_button.setChecked(False) self.record_button.setChecked(False)
self.current_cycle_file = None # Ensure it's None if creation failed
except Exception as e: except Exception as e:
print(f"Error starting recording: {e}") print(f"Error starting recording: {e}")
self.record_button.setChecked(False) 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)}") QMessageBox.critical(self, "Error", f"Failed to start recording:\n{str(e)}")
else: else:
# Stop recording # Stop recording
try: 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.finalize_log_file()
self.record_button.setText("Start Recording") self.record_button.setText("Start Recording")
self.status_bar.showMessage("Live recording stopped") self.status_bar.showMessage("Live recording stopped")
@ -985,8 +987,8 @@ class BatteryTester(QMainWindow):
self.capacity_label.setText(f"{self.capacity_ah:.4f}") self.capacity_label.setText(f"{self.capacity_ah:.4f}")
# Logging (1x per second) # Logging (1x per second)
if hasattr(self, 'log_writer') and (now - self._last_log_time >= 1.0): if hasattr(self, 'log_writer') and hasattr(self, 'current_cycle_file') and self.current_cycle_file is not None:
if self.time_data and hasattr(self, 'current_cycle_file') and not self.current_cycle_file.closed: if self.time_data and not self.current_cycle_file.closed and (now - self._last_log_time >= 1.0):
try: try:
current_time = self.time_data[-1] current_time = self.time_data[-1]
voltage = self.voltage_data[-1] voltage = self.voltage_data[-1]
@ -1018,12 +1020,13 @@ class BatteryTester(QMainWindow):
self._last_log_time = now self._last_log_time = now
except Exception as e: except Exception as e:
print(f"Error writing to log file: {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: try:
self.current_cycle_file.close() self.current_cycle_file.close()
except: except:
pass pass
self.record_button.setChecked(False) self.record_button.setChecked(False)
self.current_cycle_file = None
def start_test(self): def start_test(self):
"""Start the selected test mode""" """Start the selected test mode"""
@ -1496,7 +1499,7 @@ class BatteryTester(QMainWindow):
def stop_test(self): def stop_test(self):
"""Request immediate stop of the current test or monitoring""" """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 return
self.request_stop = True self.request_stop = True
@ -1519,9 +1522,10 @@ class BatteryTester(QMainWindow):
pass pass
# Stop recording if active # 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.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") self.record_button.setText("Start Recording")
# Reset device to safe state # Reset device to safe state