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:
parent
ed99c29fa0
commit
ccf49c8cfc
@ -27,7 +27,7 @@ class DeviceDisconnectedError(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class MeasurementThread(QObject):
|
class MeasurementThread(QThread): # Changed from QObject to QThread
|
||||||
update_signal = pyqtSignal(float, float, float)
|
update_signal = pyqtSignal(float, float, float)
|
||||||
error_signal = pyqtSignal(str)
|
error_signal = pyqtSignal(str)
|
||||||
|
|
||||||
@ -74,6 +74,8 @@ class MeasurementThread(QObject):
|
|||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.running = False
|
self.running = False
|
||||||
|
self.quit()
|
||||||
|
self.wait(500) # Wait up to 500ms for thread to finish
|
||||||
|
|
||||||
|
|
||||||
class BatteryTester(QMainWindow):
|
class BatteryTester(QMainWindow):
|
||||||
@ -110,16 +112,14 @@ class BatteryTester(QMainWindow):
|
|||||||
self.log_dir = os.path.expanduser("~/adalm1000/logs")
|
self.log_dir = os.path.expanduser("~/adalm1000/logs")
|
||||||
os.makedirs(self.log_dir, exist_ok=True)
|
os.makedirs(self.log_dir, exist_ok=True)
|
||||||
|
|
||||||
# Initialize UI
|
# Thread management
|
||||||
self.setup_ui()
|
|
||||||
|
|
||||||
# Track measurement thread
|
|
||||||
self.measurement_thread = None
|
self.measurement_thread = None
|
||||||
self.measurement_worker = None
|
self.measurement_worker = None
|
||||||
|
|
||||||
# Track test thread
|
|
||||||
self.test_thread = None
|
self.test_thread = None
|
||||||
|
|
||||||
|
# Initialize UI
|
||||||
|
self.setup_ui()
|
||||||
|
|
||||||
# Initialize device after UI is set up
|
# Initialize device after UI is set up
|
||||||
QTimer.singleShot(100, self.init_device)
|
QTimer.singleShot(100, self.init_device)
|
||||||
|
|
||||||
@ -968,24 +968,33 @@ class BatteryTester(QMainWindow):
|
|||||||
"""Reconnect the device with proper cleanup"""
|
"""Reconnect the device with proper cleanup"""
|
||||||
self.status_bar.setText("Attempting to reconnect...")
|
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'):
|
if hasattr(self, 'session'):
|
||||||
try:
|
try:
|
||||||
if self.session_active:
|
if self.session_active:
|
||||||
self.session.end()
|
self.session.end()
|
||||||
del self.session
|
del self.session
|
||||||
except:
|
except Exception as e:
|
||||||
pass
|
print(f"Error cleaning up session: {e}")
|
||||||
|
|
||||||
# Stop any running threads
|
# Reset device state
|
||||||
|
self.session_active = False
|
||||||
self.test_running = False
|
self.test_running = False
|
||||||
self.continuous_mode = False
|
self.continuous_mode = False
|
||||||
self.measuring = 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
|
# Add small delay to allow device to reset
|
||||||
time.sleep(1.5)
|
time.sleep(1.5)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user