Send MAVLink Packet with C# NuGet

Hi everyone,

I would like to send commands to my drone and I tried to do so using the C# NuGet with the following code as a test :


MAVLink.MavlinkParse mp = new MAVLink.MavlinkParse();

var buffer = mp.GenerateMAVLinkPacket20(MAVLink.MAVLINK_MSG_ID.COMMAND_INT,
                            new MAVLink.mavlink_command_int_t()
                            {
                                command = 511,
                                param1 = MAVLink.MAVLINK_MSG_ID.SCALED_IMU.GetHashCode(),
                                param2 = 100000,
                                target_system = 0
                                
                            }, false, 255, 1, -1);

port.Write(buffer, 0, buffer.Length);

The command is a MAV_CMD_SET_MESSAGE_INTERVAL (511) supposed to ask my drone to send me a SCALED_IMU (#26) message at an interval of 1 second (100000 us).

The thing is I send this command multiple times but I do not receive any SCALED_IMU (#26) message in return.

I also tried with the GenerateMAVLinkPacket10 method and with MAVLink.mavlink_command_long_t() instead, still nothing.

Is there anyone to help me on this ?

I understood what went wrong : It was the broadcasting (target_system = 0) not working so I had to put it to 1 (corresponding to the sysid of my drone).

However I have a second issue : it seems that the C# MAVLink librairy has a bug because everything I put in value or parameter of a message/command is interpreted as the int equivalent of the float, not the float itself, ie :
When i put for example 1.0f as a param_value, I see on QGroundControl that my value has been changed to 1065353216, which is the int equivalent of my float (as described here : Why is 1.0f in C code represented as 1065353216 in the generated assembly? - Stack Overflow)

var buffer = mp.GenerateMAVLinkPacket20(MAVLink.MAVLINK_MSG_ID.PARAM_SET,
                            new MAVLink.mavlink_param_set_t()
                            {
                                param_id = bytes,
                                param_value = 1.0f,
                                param_type = (byte) MAVLink.MAV_PARAM_TYPE.INT32,
                                target_component = 1,
                                target_system = 1
                            }, false, 255, 1, seq++);

image

Is there anyone to help me on this please ?

It works as intended. Your param type is INT32, so the binary data at param_value is interpreted as 32-bit signed integer.

P.S. If you are asking for help with something you downloaded from the Internet, it would be nice to add a link to the NuGet to understand what you are using.

Hi, thank you for your answer !

I finally got it work by putting

param_value = BitConverter.ToSingle(BitConverter.GetBytes((Int32)value),0).

And now it is working well