Softivation

SITL is running, but Mission Planner won't connect

Mission Planner won't connect to SITL: the socket opens but not a single byte arrives. The cause is a single-client TCP port. SITL has three ports. Find the free one with short code.

Furkan IŞIKAY 8 min read

SITL is running in the terminal and the vehicle is up. In Mission Planner you select TCP, address 127.0.0.1, port 5760, Connect. It waits a while, then gives up and shows the warning that no heartbeat was received.

There's no cable and no radio in the picture. It looks like there's nothing left to break. At this point most people start fiddling with the baud rate, the firewall, even the Mission Planner version.

Yet there is only one question to debug: is the socket not opening, or is it opening but no bytes pass through it? These are two completely different faults.

It says connected, but there isn't a single byte

On startup SITL prints a single line saying it is waiting for a connection. When the first client arrives the line changes and the stream starts. Normal so far.

With the second client things change. If your Python code is holding port 5760 and you also point Mission Planner at the same port, Mission Planner's connect call returns successfully, because the operating system kernel completes the TCP handshake.

SITL never takes over that socket. On screen it looks like a connection has been established, seconds pass, and no heartbeat arrives.

One SITL, three separate TCP ports, and each port accepts one client

On the left, the Python code and Mission Planner are connected to the same port 5760, and Mission Planner can't receive data. On the right, Mission Planner is connected to 5762 and both clients receive a stream
Where to look: the red dashed arrow on the left. The connection reaches the port but no bytes come back through it. On the right, when the same Mission Planner connects to the free 5762, the stream arrives.

Look at the red arrow on the left in the figure: the connection reaches the port but no data comes back through it. The drawing on the right shows the fix.

The same SITL has three ports

A single arducopter instance opens three separate TCP ports: 5760 is SERIAL0, 5762 is SERIAL1, 5763 is SERIAL2. All three are independent MAVLink endpoints and each accepts its own single client.

You can leave your code on 5760 and connect Mission Planner to 5762. You don't have to set up MAVProxy for a second ground control station.

One more detail: these ports are listened on at 0.0.0.0, not only on 127.0.0.1. If SITL is in a virtual machine, Mission Planner is on the host, and the network is in bridge mode, you can open TCP directly to the virtual machine's address.

If you started a second instance at the same time, the base port shifts. -I 1 adds 10 to all port numbers, so in that instance SERIAL0 becomes 5770.

Example: Your teammate is connected to SITL over 5760 while developing mission code, and you want to watch the flight from Mission Planner on the same machine. If you connect to 5760 before their code closes, Mission Planner waits for a heartbeat and gives up. If you type 5762 in the port box, both of you see the same vehicle at the same time. Your friend's stream is never interrupted.

Why it behaves this way

SITL's serial port driver holds a single connection at a time. Its counterpart in the source code is the _check_connection function in libraries/AP_HAL_SITL/UARTDriver.cpp. If a connection already exists, it returns without calling accept, and the comment "we only want 1 connection at a time" sits right above it.

The waiting second client stays in the listen queue. Because the queue doesn't overflow, no error is returned to the client either, and the line stays silent.

The most common mistake is to start fiddling with settings because the connection looks established. There's only one thing to look at: how many bytes came from that socket.

Small code that measures the port

Measure instead of guessing. This code connects to a single port, listens for six seconds, and reports one of three results: port busy, wrong port, or healthy stream.

Pythonpython3 sitl_kisa.py
# Is the TCP port to SITL actually delivering data
# Usage: python3 sitl_kisa.py <address> <port>
import socket, sys, time

adres = sys.argv[1] if len(sys.argv) > 1 else "127.0.0.1"
port = int(sys.argv[2]) if len(sys.argv) > 2 else 5760

s = socket.socket()
s.settimeout(5)
try:
    s.connect((adres, port))
except Exception as hata:
    print("connect : failed ->", hata)
    raise SystemExit(1)
print("connect : ok")

s.settimeout(1.0)
tampon = b""
bitis = time.time() + 6
while time.time() < bitis:
    try:
        tampon += s.recv(4096)
    except socket.timeout:
        pass
s.close()

kalp = sum(1 for i in range(len(tampon) - 10)
           if tampon[i] == 0xFD and tampon[i+7:i+10] == b"\x00\x00\x00")
print("received : %d bytes, %d heartbeats" % (len(tampon), kalp))
print("diagnosis :", "socket open, not a single byte -> port busy" if not tampon else
      ("bytes but no heartbeat -> wrong port" if not kalp else "stream healthy"))

If you run the code in four different situations, you get these:

Terminal
$ python3 sitl_kisa.py 127.0.0.1 5760          # with a single client
connect : ok
received : 116546 bytes, 36 heartbeats
diagnosis : stream healthy

$ python3 sitl_kisa.py 127.0.0.1 5760          # while the code is already connected
connect : ok
received : 0 bytes, 0 heartbeats
diagnosis : socket open, not a single byte -> port busy

$ python3 sitl_kisa.py 127.0.0.1 5762          # a second port at the same time
connect : ok
received : 776 bytes, 32 heartbeats
diagnosis : stream healthy

$ python3 sitl_kisa.py 127.0.0.1 5799          # a port that was never opened
connect : failed -> [Errno 111] Connection refused

In the second case connect returns successfully but not a single byte arrives. At the same time 5762 gives data without any problem.

What happens while the second client waits

If you connect two clients to the same port one after the other, over time you see this: the second client connects at second 10, receives not a single byte until second 25, and as soon as the first client closes, the stream moves to it.

When the second client connects to the same port

The second client connects at second 10, receives no data until second 25, and when the first client closes, the stream moves to it
Where to look: between seconds 10 and 25. Client 2's connection looks established but it receives no bytes at all. The stream moves to it only when client 1 closes.

In the figure, look at the dashed box between seconds 10 and 25. The client that looks connected but receives no data is exactly the situation where Mission Planner waits for a heartbeat and gives up.

Error Messages and Their Meanings

No Heartbeat Packets Received

Mission Planner connected to the port but couldn't read a valid heartbeat. The text in the source code says this plainly: two valid heartbeats are expected before connecting. So this message tells you that no data passes through the established connection.

[Errno 111] Connection refused

This is a different fault. Nobody is listening at that address: SITL is off, the port is wrong, or the virtual machine's address is different. The code above tells this apart in the first second.

Waiting for connection ....

SITL prints this to its own output. It means SERIAL0 is free and the first client is awaited. If you can't connect while this line is on screen, the problem is on the network side, not the SITL side.

Closed connection on SERIAL0

You were connected and dropped. If your code exited or you pressed Disconnect in Mission Planner, that's normal. If it happens on its own, check whether two clients are entering the same port in turn.

If you want to go deeper into simulation

The diagnosis here is only the first of the network problems you'll meet on the SITL and Gazebo side. We build the path from setup to camera feed and from there to swarm scenarios step by step throughout the AI and Drone Simulation course.

AI and Drone Simulation Virtual Machine: comes with ArduPilot SITL, ROS Noetic, and Gazebo 11 installed. You start simulating without any setup hassle.

Was this article helpful?

Free resourceArduPilot SITL setup guideSet up the simulation on your own computer: Ubuntu, Windows WSL, or a virtual machine, start to finish.