Connect to FC with pymavlink

Hello
I use this piece of code to connect to the autopilot:

from pymavlink import mavutil
import argparse
import time
import serial

myport = 'COM6'
mybaudrate = 115200

# Start a connection listening on a USB port
the_connection = mavutil.mavlink_connection(myport,mybaudrate)

# Wait for the first heartbeat 
#   This sets the system and component ID of remote system for the link
the_connection.wait_heartbeat()
print("Heartbeat from system (system %u component %u)" % (the_connection.target_system, the_connection.target_component))

# Once connected, use 'the_connection' to get and send messages
# Continuation of the code ...

My problem is that sometimes (almost once out of three times) the HEART signal is not received and as a result the connection is not completed and the code is not executed. Is there any solution? Is my code wrong?

@amilcarlucas @stephendade

That code is correct - works reliably on my Win10 box.

Could it be a connection issue to the flight controller?

Thanks for your reply.
I connect my companion computer(NVIDIA Jetson Orin NX) to my FC with FTDI. The connector and the connection is safe, but I don’t know why this problem still occured.

What are you using for myport? COM6 is a Windows serial port name. Under Linux it would be something like /dev/...

2 Likes

yes, I commented the /dev/ttyUSB0 and created this topic on windows, so my code is wrong in this topic.
But in my companion computer the code is write and myport is /dev/ttyUSB0.

Have you tried removing the modemmanager package? sudo apt remove modemmanager.

It can conflict with serial ports.

I tried but it doesn’t work.