MAVlink - Change altitude

I have tried to set target altitude with these functions to no avail:

MAV_CMD_CONDITION_CHANGE_ALT (113)
MAV_CMD_NAV_CONTINUE_AND_CHANGE_ALT (30)
MAV_CMD_DO_CHANGE_ALTITUDE (186)
MAV_CMD_NAV_LOITER_TO_ALT (31)

Specifically:

mavlink_msg_command_long_pack(2, 0, &msg, 1, 1, 31, 0, 0, 0, 0, 0, 0, 0, 420);
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
Serial.write(buf,len);

I try this with SITL and the altitude won’t change.
According to Mission Commands — Plane documentation, at least commands 30 and 31 seem to be supported.

Is this even a correct way to control altitude with a companion computer (Arduino)? If so, how should this be implemented?
Thank you very much.

Yes it is possible, but you need to be in guided mode.

Doesn’t seem to be working. The plane takes off with TAKEOFF and holds an altitude of 50m. After changing mode to Guided, it still holds this altitude even though I periodically send

mavlink_msg_command_long_pack(2, 0, &msg, 1, 1, 31, 0, 0, 0, 0, 0, 0, 0, 420);