Anıl Sayar

C++ & Qt Developer

Java & Spring Developer

Novice Photographer

C# & WPF Developer

Freelancer

Anıl Sayar
Anıl Sayar
Anıl Sayar
Anıl Sayar

C++ & Qt Developer

Java & Spring Developer

Novice Photographer

C# & WPF Developer

Freelancer

Blog Post

How to Create Socket for Mavlink in C++

February 29, 2024 C++, Programming, Qt

This article is still in draft and while it has some explanations, it lacks the code. When I have enough time for it, I will add the code. Thank you for understanding.

Mavlink with C++ can be often time consuming and mostly lack resources to head start our projects. Today, we will be opening a socket and get data from our vehicle.

First we have to make a method for initialization. Parameters can be QString ip adress and integer for port.

We need to create the socket itself.
m_socket_fd creates our socket and gives a value. We can check our values if the value is less than 0, which means our socket has been failed to create.

We have to convert our strings to memory addresses as bytes, so we use inet_pton to turn our ip address into binary. Then we have to convert our port too. SO we yse htons(port) from host byte to network byte.

Last thing is we have to bind everything. We will bind our memory address to the socket and check the returning value. If it is zero, we are good. If not,we can simply give error message to the user.

After binding to the socket, we have to store the socket in order to get values from later on. So we copy our memory address using these functions.

So, we need to test if our socket is functioning. We can test the socket by trying to recieve integer from the socket. If return value is greater than zero, we can set the memory address in stone.

And this is basically how connecting to socket works. Now we have to create our receiver to get data. We are using Qt in this article, so we will use Qt items. We have to create a thread class.

In UDP receiver thread, we will pass m_socket_fd to make it easier.

in the run thread, we need to check if interruption is requested. while it is not requested we can pull value from the socket.

We need to make a buffer to store our items. Boundary should be mavlink’s max packet length. Then, we will use recv function to set the buffer our data while setting our data length.

We have to create our mavlink messages. So we will create mavlink_message_t and mavlink_status_t.

Last thing is to parse. We have our data length(recvlen) and data itself(buffer). So we create a for loop for all bytes in the buffer. Then, we will make mavlink to parse the data. If it manages to get the full data, then our parse is done.

From this point, we can do whatever we want. We have our mavlink message set, status set etc.. For example if we want to check for a heartbeat and other statuses, we can make a switch for msg.msgid and sort from there. If we want to get heartbeat, we can add a case with “MAVLINK_MSG_ID_HEARTBEAT”. From then, we can get everything we need. You can create events for other classes to handle it, make an UI for it -since its in Qt why not- etc..

Thank you for reading, and have a good day.

Write a comment