Softivation

No telemetry while the Raspberry Pi is connected: split the link

If telemetry stops arriving while the Raspberry Pi is connected, the link is being shared. Let one process open the serial port and duplicate the stream with mavlink-router. Every endpoint sees the same data.

Furkan IŞIKAY 6 min read

You connected a Raspberry Pi to the autopilot, and your own Python code is reading telemetry. Then you want to connect the ground control station to the same link too, and its screen stays empty.

The cable is in place, the baud is right, and your code is still printing data. In this situation, looking at the cable is a waste of time: two programs can't read one MAVLink link at the same time.

Why two programs can't share one serial link

A serial link is point to point. The autopilot sends bytes in order, and the reader on the other end collects them and turns them into MAVLink frames.

The operating system lets the same serial device be opened a second time and doesn't tell you. The two processes take turns reading from the same stream, and each gets part of a frame. A frame whose checksum fails is silently dropped.

Look at how the bytes are split between the two programs in the top half of the figure. The second program opens the port silently. Since no error message appears, people look for the problem in the cable, when in fact the link is being shared.

The same bytes: split between two readers vs. copied by a duplicator

Top panel: the bytes of two frames sent by the Pixhawk are split between two programs that opened /dev/serial0; both get half frames and the frames are dropped. Bottom panel: only mavlink-routerd opens the port; the ground control station gets a full copy on UDP 14550 and the Python code on UDP 14551.
Where to look: in the top panel, the color sequence in each program's box is left incomplete. In the bottom panel, all twelve bytes go to each endpoint.

On the network side the picture is clearer. A second connection is made to an endpoint that accepts only one client; the connection looks open, but not a single byte flows through it.

Set up a single reader, duplicate the stream

The fix is to put a duplicator in front of the link. A single process opens the serial port. The ground control station, the mission code, and the logging tool connect to the network endpoints that process opens.

The bottom half of the figure shows this setup. Even if one endpoint closes, the others keep seeing the stream, and the process touching the serial port stays the same.

If the autopilot has two separate telemetry ports, you can use both. TELEM1 goes to the telemetry radio, TELEM2 to the companion computer. These are different serial ports and don't block each other. You need duplication when several programs have to share one port.

Example: your mission code has opened the serial port on the Pi and is reading telemetry. A teammate starts a second program on the Pi that opens the same port so they can watch the flight from the ground station. Their screen stays empty, and after a while your code starts printing that the stream was lost. Once you set up the duplicator, both see the same stream: the ground control station on endpoint 14550, your code on endpoint 14551.

Configuration

Run mavlink-router on the companion computer. Define the serial endpoint once, and open a separate UDP endpoint for each consumer.

Outputmavlink-router configuration
# /etc/mavlink-router/main.conf
[UartEndpoint to_fc]
Device = /dev/serial0
Baud = 57600

[UdpEndpoint gcs]
Mode = Server
Address = 0.0.0.0
Port = 14550

[UdpEndpoint gorev_betigi]
Mode = Normal
Address = 127.0.0.1
Port = 14551

On the Python side, the only change is the connection address. Your code now connects to the duplicated endpoint instead of the serial port.

Python
from pymavlink import mavutil

# Connect not to the serial port but to the UDP endpoint the router duplicates.
arac = mavutil.mavlink_connection("udpin:127.0.0.1:14551", source_system=251)

if arac.wait_heartbeat(timeout=30) is None:
    raise SystemExit("no heartbeat: first check whether routerd is running")

print("connected:", arac.target_system, arac.target_component)

arac.mav.request_data_stream_send(
    arac.target_system, arac.target_component,
    mavutil.mavlink.MAV_DATA_STREAM_ALL, 4, 1)

while True:
    m = arac.recv_match(type="ATTITUDE", blocking=True, timeout=5)
    if m is None:
        print("stream interrupted")
        continue
    print(round(m.roll, 3), round(m.pitch, 3), round(m.yaw, 3))

If you connect two clients behind the duplicator, both receive the same stream at the same time. If you connect a second client directly to the same endpoint, the connection looks open but not a single byte arrives. Here are the message counts the clients received on ArduCopter 4.7:

ClientMessages receivedTime
Single client, directly on the endpoint29966 s
Second client on the same endpoint06 s
Client A behind the router1101720 s
Client B behind the router1101820 s

On a real radio link the absolute numbers drop, but the difference between the two clients stays the same.

You can do the same job with MAVProxy: give one --master and as many --out as you like. MAVProxy can strain the CPU on a weak Raspberry Pi. In that case the ArduPilot documentation recommends switching to mavlink-router.

Error Messages and Their Meanings

The lines below are strings that appear in the tools' own source code. Where you need to look depends on which one you see.

When MAVProxy connects to the serial port but receives no heartbeat, the output runs like this:

Output
Connect /dev/serial0 source_system=255
Log Directory:
Telemetry log: mav.tlog
Waiting for heartbeat from /dev/serial0
MAV> link 1 down

MAVProxy prints this line when it receives no packet at all for a set period. It means the link isn't flowing at all, or another process is sharing the same port. First check who opened the port.

Waiting for heartbeat from /dev/serial0

The first heartbeat never arrived. The problem here isn't sharing but a basic connection issue: TX and RX may be swapped, the baud may not match, or the device path you opened may be wrong.

The stream started, then stopped. This is the typical picture on a shared link: frames are assembled for a while, then the two readers take each other's bytes and the checksum fails.

Timeout in initializing connection.

DroneKit established the connection but couldn't complete the initial parameter stream. The bandwidth may not be enough. If you are sharing the link with a second program, that is the cause too.

WARNING: You should uninstall ModemManager as it conflicts with APM and Pixhawk

ModemManager is installed on the system. This service opens USB serial devices on its own and holds the link as a third party. Remove this service from the companion computer.

If you want to go deeper on this topic

The lessons and exercises that build the link between the companion computer and the ground control station from start to finish are in the Drone Communication Technologies course. The source code evidence, log signature tables, and field cases are there too.

Was this article helpful?

Free resourcePixhawk and ArduPilot error message glossaryThe 5 messages in this article and similar ones are in the glossary: what they mean, why they appear, what to check first.