From df69d0e8325d7ed69924974d5fd5e0be7ab1ef3c Mon Sep 17 00:00:00 2001 From: Jan Date: Thu, 3 Jul 2025 13:45:46 +0200 Subject: [PATCH] MainCode/adalm1000_logger.py aktualisiert Thread Safety Violations: Qt objects (like QWidgets) must only be accessed from the main thread (GUI thread) Your measurement thread is directly interacting with GUI elements Painter Conflicts: Multiple threads trying to paint to the same canvas simultaneously Recursive repaint calls Segmentation Fault: Likely caused by improper thread cleanup or accessing deleted objects --- MainCode/adalm1000_logger.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/MainCode/adalm1000_logger.py b/MainCode/adalm1000_logger.py index 68f95b4..8ada27b 100644 --- a/MainCode/adalm1000_logger.py +++ b/MainCode/adalm1000_logger.py @@ -13,13 +13,13 @@ from collections import deque from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QGridLayout, QLabel, QPushButton, QLineEdit, QCheckBox, QFrame, QMessageBox, QFileDialog) -from PyQt5.QtCore import Qt, QTimer, pyqtSignal, pyqtSlot, QObject +from PyQt5.QtCore import Qt, QTimer, pyqtSignal, pyqtSlot, QObject, QThread import pysmu class DeviceDisconnectedError(Exception): pass -class MeasurementThread(QObject): +class MeasurementThread(QThread): update_signal = pyqtSignal(float, float, float) error_signal = pyqtSignal(str) @@ -67,6 +67,7 @@ class MeasurementThread(QObject): class BatteryTester(QMainWindow): def __init__(self): + self.plot_mutex = threading.Lock() super().__init__() # Color scheme @@ -392,7 +393,7 @@ class BatteryTester(QMainWindow): del self.session except: pass - + time.sleep(1) self.session = pysmu.Session(ignore_dataflow=True, queue_size=10000) @@ -418,8 +419,8 @@ class BatteryTester(QMainWindow): self.measurement_thread.update_signal.connect(self.update_measurements) self.measurement_thread.error_signal.connect(self.handle_device_error) - self.thread = threading.Thread(target=self.measurement_thread.run, daemon=True) - self.thread.start() + # Start the QThread directly (no need for threading.Thread) + self.measurement_thread.start() except Exception as e: self.handle_device_error(str(e)) @@ -842,10 +843,11 @@ class BatteryTester(QMainWindow): if not self.time_data: return - self.line_voltage.set_data(self.time_data, self.voltage_data) - self.line_current.set_data(self.time_data, self.current_data) - self.auto_scale_axes() - self.canvas.draw_idle() + with self.plot_mutex: + self.line_voltage.set_data(self.time_data, self.voltage_data) + self.line_current.set_data(self.time_data, self.current_data) + self.auto_scale_axes() + self.canvas.draw_idle() def auto_scale_axes(self): """Auto-scale plot axes with appropriate padding and strict boundaries""" @@ -968,6 +970,8 @@ class BatteryTester(QMainWindow): if hasattr(self, 'measurement_thread'): self.measurement_thread.stop() + self.measurement_thread.quit() + self.measurement_thread.wait(1000) # Wait up to 1 second if hasattr(self, 'session') and self.session: try: