Created
March 16, 2021 19:08
-
-
Save dvigne/32eb87b48a65d5f5cd1db7822ebab712 to your computer and use it in GitHub Desktop.
Basic Serialization and Deserialization with Mavlink 2.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main(int argc, char const *argv[]) { | |
mavlink_message_t msg; | |
time_t boot = time(NULL); | |
mavlink_msg_system_time_pack(mavlink_system.sysid, mavlink_system.compid, &msg, time(NULL), boot); | |
unsigned char buf[sizeof(msg)]; | |
printf("Message Size: %ld\n", sizeof(msg)); | |
mavlink_msg_to_send_buffer(buf, &msg); | |
std::fstream fs("samplepacket.txt", std::fstream::out | std::fstream::binary); | |
fs.write((char *) &buf, sizeof(msg)); | |
fs.close(); | |
std::fstream ifs("samplepacket.txt", std::fstream::in | std::fstream::binary); | |
unsigned char recv[MAVLINK_MAX_PACKET_LEN]; | |
mavlink_status_t status; | |
mavlink_message_t recvmsg; | |
int chan = MAVLINK_COMM_0; | |
char byte; | |
while(ifs.get(byte)) { | |
if(mavlink_parse_char(chan, (uint8_t) byte, &recvmsg, &status)) { | |
printf("Received message with ID %d, sequence: %d from component %d of system %d\n", recvmsg.msgid, recvmsg.seq, recvmsg.compid, recvmsg.sysid); | |
} | |
} | |
switch (recvmsg.msgid) { | |
case MAVLINK_MSG_ID_SYSTEM_TIME: | |
mavlink_system_time_t time; | |
mavlink_msg_system_time_decode(&recvmsg, &time); | |
printf("Received Time: %ld, %d\n", time.time_unix_usec, time.time_boot_ms); | |
} | |
ifs.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment