def btn_callback(self): if not self.is_connected: # Attempt to connect - check that the port is still connected if self.comports.get() in PortManager.serial_ports(): # Port is still up n runnin' self.btn["text"] = "Disconnect" self.comports.config(state="disabled") # Connect to the Arduino and start processing self.msg_handler = Thread(target=MessageHandler.message_handler, args=(self.comports.get(),)) self.msg_handler.start() # Change da flag fool! self.is_connected = True else: # Port not found - refresh list self.load_ports() else: # Disconnect from Arduino MessageHandler.stop_handler() self.msg_handler.join() # Change the flag BACK! self.is_connected = False # Disconnect. Refresh port list self.btn["text"] = "Connect" self.comports.config(state="enabled") self.load_ports()
def load_ports(self): # Load all comports in pc. Select first item in the list if exists self.comports['values'] = PortManager.serial_ports() if len(self.comports['values']) > 0: self.comports.current(0) else: self.comports.set("")