Troubleshooting COM Port Connections in Python GUIs

Hello! I created a GUI using Python that features a menu displaying all the COM ports defined on the device. Users can select from these COM ports to establish a connection. If the chosen COM port fails, the user can easily switch to another available port.


My main problem is that when the GUI opens, all the COM ports fail to connect on the first attempt to ardupilot

The issue I’m facing is that the Mission Planner app successfully connects to the main COM port on the first attempt Once the Mission Planner disconnects, the GUI can connect successfully to ArduPilot every time

the Mission Planner disconnects, the GUI can connect successfully to ArduPilot every time

As long as ArduPilot is not turned off, the GUI connects easily after the disconnection and reconnection of Mission Planner.
My snippet of code related to PyMAVLink in the GUI is as follows:


        try:
            com_ports = list_ports.comports()
            for port_init in com_ports:
                myport = port_init.device
                mybaudrate = 115200
                self.the_connection = mavutil.mavlink_connection(myport, mybaudrate)
                self.the_connection.close()

The code above is related to establishing the connection for the first time

    def comPortSelected(self,port):
        self.flag_com=True
        try:
            self.the_connection.close()
            myport = port.device
            mybaudrate = 115200
            self.the_connection = mavutil.mavlink_connection(myport, mybaudrate)
            print(f"COM Port selected:{port.device}")
            self.the_connection.wait_heartbeat(timeout=1)
            print("Heartbeat from system (system %u component %u)" % (
                self.the_connection.target_system, self.the_connection.target_component))
            msg = self.the_connection.recv_match(type='HEARTBEAT', blocking=True, timeout=1)
            print(msg)
            if msg==None:
                self.communication_label.setText(f'Communication with {port.device} is failed .')
                self.communication_label.setStyleSheet(
                    "color: red; font-weight: bold; font-family: Arial; font-size: 14pt;")
                self.time_off_on=time.time()
                self.the_connection.close()
            else:
                self.communication_label.setText(f'Communication with {port.device} is successfully done.')
                self.communication_label.setStyleSheet(
                    "color: green; font-weight: bold; font-family: Arial; font-size: 14pt;")
                self.time_off_on=time.time()
                self.msg = self.the_connection.recv_match(type='GLOBAL_POSITION_INT', blocking=True)
                self.flag_thread=True
                self.msg_flag=True
        except Exception as e:
            self.communication_label.setText(f"An error occurred: {e}")
            self.communication_label.setStyleSheet(
                "color: red; font-weight: bold; font-family: Arial; font-size: 14pt;")
            self.time_off_on = time.time()
            self.the_connection.close()

This function is called when the user clicks on a specific COM port

@stephendade

@amilcarlucas @rmackay9 @stephendade @ppoirier

Take a look at MethodicConfigurator/MethodicConfigurator/backend_flightcontroller.py at master · ArduPilot/MethodicConfigurator · GitHub for inspiration

1 Like