diff --git a/MainCode/adalm1000_logger.py b/MainCode/adalm1000_logger.py index 28ae7b5..39b814c 100644 --- a/MainCode/adalm1000_logger.py +++ b/MainCode/adalm1000_logger.py @@ -805,7 +805,17 @@ class BatteryTester: error_msg = str(error) print(f"Device error: {error_msg}") - self.root.after_idle(lambda: self.status_light.itemconfig(self.status_indicator, fill='red')) + # Clean up session first + if hasattr(self, 'session'): + try: + if self.session_active: + self.session.end() + del self.session + except Exception as e: + print(f"Error cleaning up session: {e}") + + # Update UI + self.status_light.itemconfig(self.status_indicator, fill='red') self.connection_label.config(text="Disconnected") self.status_var.set(f"Device error: {error_msg}") @@ -813,6 +823,7 @@ class BatteryTester: self.test_running = False self.continuous_mode = False self.measuring = False + if hasattr(self, 'start_button'): self.start_button.config(state=tk.DISABLED) if hasattr(self, 'stop_button'): @@ -829,7 +840,35 @@ class BatteryTester: self.ax2.set_xlim(0, 1) self.canvas.draw() - # Clean up session + # Show error message and attempt reconnect automatically + if self.root.winfo_exists(): + self.root.after(100, self.attempt_reconnect) + + def attempt_reconnect(self): + """Attempt to reconnect automatically""" + if not self.root.winfo_exists(): + return + + # Show error message first + messagebox.showerror( + "Device Connection Error", + "Could not connect to ADALM1000\n\n" + "1. Check USB cable connection\n" + "2. The device will attempt to reconnect automatically" + ) + + # Then attempt reconnect after a short delay + self.root.after(1000, self.reconnect_device) + + def reconnect_device(self): + """Reconnect the device with proper cleanup""" + if not self.root.winfo_exists(): + return + + self.status_var.set("Attempting to reconnect...") + self.root.update() + + # Clear any existing session if hasattr(self, 'session'): try: if self.session_active: @@ -837,25 +876,12 @@ class BatteryTester: del self.session except: pass - - if self.root.winfo_exists(): # Double-check before showing message - try: - messagebox.showerror( - "Device Connection Error", - f"Could not connect to ADALM1000:\n\n{error_msg}\n\n" - "1. Check USB cable connection\n" - "2. Try the Reconnect button\n" - "3. Restart the application if problem persists" - ) - except: - pass # Ignore errors if window is being destroyed - - def reconnect_device(self): - """Reconnect the device""" - self.status_var.set("Attempting to reconnect...") + + # Stop any running threads self.test_running = False self.continuous_mode = False self.measuring = False + if hasattr(self, 'measurement_event'): self.measurement_event.clear() @@ -865,9 +891,21 @@ class BatteryTester: if hasattr(self, 'test_thread'): self.test_thread.join(timeout=1.0) - # Reset before reinitializing - self.handle_device_error("Reconnecting...") - self.init_device() + # Add small delay to allow device to reset + time.sleep(1.5) + + # Try to initialize device + try: + self.init_device() + if self.session_active: + self.status_var.set("Reconnected successfully") + return + except Exception as e: + print(f"Reconnect failed: {e}") + + # If we get here, reconnection failed + self.status_var.set("Reconnect failed - will retry...") + self.root.after(2000, self.reconnect_device) # Retry after 2 seconds def on_close(self): """Clean up on window close"""