The trim payload function should just remove trailing zeros when encoding. Unfortunately it causes the packets to not be parsed correctly (I get the parse state MAVLINK_PARSE_STATE_GOT_BAD_CRC1) by the C++ mavlink parser on the other side. When I comment the function out, everything works ok.