From 35bea6bdd1849db500468179555659814c10b1c3 Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 28 Jun 2025 00:59:54 +0200 Subject: [PATCH] MainCode/adalm1000_logger.py aktualisiert Graceful Shutdown Sequence: Stop all measurement threads first Wait for them to complete Then cleanup the USB session Error Handling: Add more specific exception handling for USB operations Consider adding retries for non-critical operations Logging: Replace print statements with proper logging Consider logging to a file for debugging purposes --- MainCode/adalm1000_logger.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/MainCode/adalm1000_logger.py b/MainCode/adalm1000_logger.py index 6e448ab..1a203cc 100644 --- a/MainCode/adalm1000_logger.py +++ b/MainCode/adalm1000_logger.py @@ -75,7 +75,9 @@ class MeasurementThread(QThread): self._running = False if self.isRunning(): self.quit() - self.wait(500) + if not self.wait(500): # Wait up to 500ms for clean exit + print("Warning: Thread didn't exit cleanly, terminating") + self.terminate() class BatteryTester(QMainWindow): def __init__(self): @@ -352,6 +354,8 @@ class BatteryTester(QMainWindow): def init_device(self): """Initialisiert das ADALM1000-Gerät""" + # Temporarily enable USB debugging + os.environ['LIBUSB_DEBUG'] = '3' # Set to 0 in production self.cleanup_device() try: @@ -389,6 +393,9 @@ class BatteryTester(QMainWindow): if self.measurement_thread is not None: try: self.measurement_thread.stop() + # Wait for thread to finish + if not self.measurement_thread.wait(1000): # 1 second timeout + print("Warning: Measurement thread didn't stop cleanly") self.measurement_thread = None except Exception as e: print(f"Fehler beim Stoppen des Mess-Threads: {e}") @@ -397,7 +404,10 @@ class BatteryTester(QMainWindow): if hasattr(self, 'session'): try: if self.session_active: + # Add small delay before ending session + time.sleep(0.1) self.session.end() + self.session_active = False del self.session except Exception as e: print(f"Fehler beim Bereinigen der Sitzung: {e}")