diff --git a/MainCode/adalm1000_logger.py b/MainCode/adalm1000_logger.py index e2b5394..6173dfd 100644 --- a/MainCode/adalm1000_logger.py +++ b/MainCode/adalm1000_logger.py @@ -27,7 +27,7 @@ class DeviceDisconnectedError(Exception): pass -class MeasurementThread(QObject): +class MeasurementThread(QThread): # Changed from QObject to QThread update_signal = pyqtSignal(float, float, float) error_signal = pyqtSignal(str) @@ -74,6 +74,8 @@ class MeasurementThread(QObject): def stop(self): self.running = False + self.quit() + self.wait(500) # Wait up to 500ms for thread to finish class BatteryTester(QMainWindow): @@ -110,16 +112,14 @@ class BatteryTester(QMainWindow): self.log_dir = os.path.expanduser("~/adalm1000/logs") os.makedirs(self.log_dir, exist_ok=True) - # Initialize UI - self.setup_ui() - - # Track measurement thread + # Thread management self.measurement_thread = None self.measurement_worker = None - - # Track test thread self.test_thread = None + # Initialize UI + self.setup_ui() + # Initialize device after UI is set up QTimer.singleShot(100, self.init_device) @@ -968,24 +968,33 @@ class BatteryTester(QMainWindow): """Reconnect the device with proper cleanup""" self.status_bar.setText("Attempting to reconnect...") - # Clear any existing session + # Clean up measurement thread if it exists + if self.measurement_thread is not None: + try: + if hasattr(self.measurement_thread, 'isRunning') and self.measurement_thread.isRunning(): + self.measurement_thread.stop() + self.measurement_thread.quit() + self.measurement_thread.wait(500) + except Exception as e: + print(f"Error stopping measurement thread: {e}") + finally: + self.measurement_thread = None + + # Clean up any existing session if hasattr(self, 'session'): try: if self.session_active: self.session.end() del self.session - except: - pass - - # Stop any running threads + except Exception as e: + print(f"Error cleaning up session: {e}") + + # Reset device state + self.session_active = False self.test_running = False self.continuous_mode = False self.measuring = False - if hasattr(self, 'measurement_thread'): - self.measurement_thread.quit() - self.measurement_thread.wait() - # Add small delay to allow device to reset time.sleep(1.5)