Decoding Packets
Example on how to decode a list of bytes into a packet with a Device ID, Packet ID and corresponding Data.
Decoding a Packet
An example on how to decode a list of bytes into a packet. The data is expected to come in as a list of unsigned 8 bit intergers.
/*
C/examples/decodePacketExample.c
Author: John Sumskas
Email: j.sumskas@reachrobotics.com
Date: 21/06/2022
*/
#include <stdio.h>
#include "../bplprotocol.h"
#define PACKET_LENGTH 11
void main(void){
/* **************************** encodePacket() Example *************************************** */
// create a buffer of bytes for your packet to be filled in with.
uint8_t encodedPacket[PACKET_LENGTH] = {10, 1, 2, 3, 4, 5, 2, 1, 9, 23, 0};
// Set data to zeros
// memset(encodedPacket, 0, MAX_PACKET_LENGTH);
// Encoding data with the following information
struct Packet packet;
// devode the packet.
int result = decodePacket(&packet, encodedPacket, PACKET_LENGTH);
if (result == -1)
{
printf("decodePacket ERROR: LENGTH: %d is longer than Maximum Packet Length %d\n", PACKET_LENGTH, MAX_PACKET_LENGTH);
return;
}
else if (result == -2)
{
printf("decodePacket ERROR: Read Packet Length is invalid.\n");
return;
}
else if (result == -3)
{
printf("decodePacket ERROR: CRC Check did not Match.\n");
return;
}
else if (result < 0)
{
printf("decodePacket ERROR: Unknown Error.\n");
return;
}
printf("Decoded packet \n");
printf("Device ID: %d \n", packet.deviceID);
printf("Packet ID: %d \n", packet.packetID);
printf("Packet data: ");
for (int i = 0; i<packet.dataLength; i++)
{
printf(" %d", packet.data[i]);
}
printf("\n");
// Print the encoded packet to stdio.
}
Decoding a Packet with a Single Float
An example on how to decode a list of bytes into a packet. The data is expected to come in as a single float represented as 4 bytes. This example will show you how to convert the 4 bytes into a single float. This is a very common form for BPL Packets like Position and Velocity.
/*
C/examples/decodePacketWithFloatExample.c
Author: John Sumskas
Email: j.sumskas@reachrobotics.com
Date: 21/06/2022
*/
#include <stdio.h>
#include "../bplprotocol.h"
#define PACKET_LENGTH 10
void main(void){
/* **************************** encodePacket() Example *************************************** */
// create a buffer of bytes for your packet to be filled in with.
uint8_t encodedPacket[PACKET_LENGTH] = {9, 158, 239, 131, 64, 3, 1, 8, 184, 0};
// Set data to zeros
// memset(encodedPacket, 0, MAX_PACKET_LENGTH);
// Encoding data with the following information
struct Packet packet;
// devode the packet.
int result = decodePacket(&packet, encodedPacket, PACKET_LENGTH);
if (result == -1)
{
printf("decodePacket ERROR: LENGTH: %d is longer than Maximum Packet Length %d\n", PACKET_LENGTH, MAX_PACKET_LENGTH);
return;
}
else if (result == -2)
{
printf("decodePacket ERROR: Read Packet Length is invalid.\n");
return;
}
else if (result == -3)
{
printf("decodePacket ERROR: CRC Check did not Match.\n");
return;
}
else if (result < 0)
{
printf("decodePacket ERROR: Unknown Error.\n");
return;
}
printf("Decoded packet: \n");
printf("Device ID: %d \n", packet.deviceID);
printf("Packet ID: %d \n", packet.packetID);
printf("Packet data: ");
for (int i = 0; i<packet.dataLength; i++)
{
printf(" %d", packet.data[i]);
}
printf("\nDecoding data to Float \n");
float f = decodeFloat(packet.data);
printf("DecodedFloat: %f", f);
}
Decoding a Packet with a list of Floats
Some packets will contain a list of floats in their data. This example will decode that packet into a list of floats.
/*
C/examples/decodePacketWithFloatListExample.c
Author: John Sumskas
Email: j.sumskas@reachrobotics.com
Date: 21/06/2022
*/
#include <stdio.h>
#include "../bplprotocol.h"
#define PACKET_LENGTH 26
void main(void){
/* **************************** encodePacket() Example *************************************** */
// create a buffer of bytes for your packet to be filled in with.
uint8_t encodedPacket[PACKET_LENGTH] = {13, 154, 153, 153, 63, 51, 51, 19, 64, 154, 153, 89, 64, 1, 11, 144, 64, 51, 51, 179, 64, 3, 1, 24, 145, 0};
// Set data to zeros
// memset(encodedPacket, 0, MAX_PACKET_LENGTH);
// Encoding data with the following information
struct Packet packet;
// devode the packet.
int result = decodePacket(&packet, encodedPacket, PACKET_LENGTH);
if (result == -1)
{
printf("decodePacket ERROR: LENGTH: %d is longer than Maximum Packet Length %d\n", PACKET_LENGTH, MAX_PACKET_LENGTH);
return;
}
else if (result == -2)
{
printf("decodePacket ERROR: Read Packet Length is invalid.\n");
return;
}
else if (result == -3)
{
printf("decodePacket ERROR: CRC Check did not Match.\n");
return;
}
else if (result < 0)
{
printf("decodePacket ERROR: Unknown Error.\n");
return;
}
printf("Decoded packet: \n");
printf("Device ID: %d \n", packet.deviceID);
printf("Packet ID: %d \n", packet.packetID);
printf("Packet data: ");
for (int i = 0; i<packet.dataLength; i++)
{
printf(" %d", packet.data[i]);
}
printf("\nDecoding data to floats \n");
float floatList[MAX_PACKET_LENGTH/4];
size_t floatLength = decodeFloats(floatList, packet.data, packet.dataLength);
printf("Decoded Floats: ");
for (int i = 0; i<floatLength; i++)
{
printf(" %f", floatList[i]);
}
printf("\n");
}