MainCode/adalm1000_logger.py aktualisiert

Thread Management:

        Changed MeasurementThread to inherit from QThread instead of QObject

        Added proper thread cleanup in reconnect_device()

        Added checks for thread existence before attempting operations

    Error Handling:

        Added more robust error handling around thread operations

        Ensured proper cleanup of resources during reconnection attempts

        Added checks for thread running state before attempting to stop it

    Initialization:

        Explicitly initialized thread variables to None

        Added proper null checks before accessing thread methods

    Device Reconnection:

        Improved the reconnection flow with better cleanup

        Added status messages to keep user informed
(D)
This commit is contained in:
Jan 2025-06-27 16:34:12 +02:00
parent ed99c29fa0
commit ccf49c8cfc

View File

@ -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
except Exception as e:
print(f"Error cleaning up session: {e}")
# Stop any running threads
# 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)