Encoding Packets

Example on how to encode a packet into bytes using the BPL C SDK.

Encoding a Packet

An example on how to encode a BPL Packet. BPL Packet consists of a Device ID (1-255), a PacketID (1-255), and an array of data (bytes)


/* 
C/examples/encodePacketBareExample.c

Author: John Sumskas
Email: j.sumskas@reachrobotics.com
Date: 21/06/2022

*/
#include <stdio.h>
#include "../bplprotocol.h"

void main(void){

    /* **************************** encodePacketBare() Example *************************************** */
    // create a buffer of bytes for your packet to be filled in with.
    uint8_t encodedPacket[MAX_PACKET_LENGTH];
    // Set data to zeros
    memset(encodedPacket, 0, MAX_PACKET_LENGTH);   

    // Encoding data with the following information
    uint8_t deviceID = 0x01;
    uint8_t packetID = MODE;
    uint8_t data[1] = {5};   // Int packet

    // encode the packet. 
    size_t packetLength = encodePacketBare(encodedPacket, deviceID, packetID, data, 1);

    // Print the encoded packet to stdio.
    printf("Encoded Packet: ");
    for (int i = 0; i<=packetLength; i++){
        printf(" %d", encodedPacket[i]);
    }
    printf("\n");

}

Encoding a Packet using the Packet Struct


/* 
C/examples/encodePacketExample.c

Author: John Sumskas
Email: j.sumskas@reachrobotics.com
Date: 21/06/2022

*/
#include <stdio.h>
#include "../bplprotocol.h"

void main(void){

    /* **************************** encodePacket() Example *************************************** */
    // create a buffer of bytes for your packet to be filled in with.
    uint8_t encodedPacket[MAX_PACKET_LENGTH];
    // Set data to zeros
    memset(encodedPacket, 0, MAX_PACKET_LENGTH);   

    // Encoding data with the following information
    uint8_t deviceID = 0x01;
    uint8_t packetID = MODE;

    uint8_t data[5] = {1, 2, 3, 4, 5};   // Int packet

    struct Packet packet;

    packet.deviceID = deviceID;
    packet.packetID = packetID;
    memcpy(packet.data, data, 5);
    packet.dataLength = 5;

    // encode the packet. 
    size_t packetLength = encodePacket(encodedPacket, &packet);

    // Print the encoded packet to stdio.
    printf("Encoded Packet: ");
    for (int i = 0; i<=packetLength; i++){
        printf(" %d", encodedPacket[i]);
    }
    printf("\n");

}

Encoding a Packet with Float Data


/* 
C/examples/encodePacketWithFloatExample.c

Author: John Sumskas
Email: j.sumskas@reachrobotics.com
Date: 21/06/2022

*/
#include <stdio.h>
#include "../bplprotocol.h"

void main(void){

    /* **************************** encodePacket() Example with floats for data *************************************** */
    // create a buffer of bytes for your packet to be filled in with.
    uint8_t encodedPacket[MAX_PACKET_LENGTH];
    // Set data to zeros
    memset(encodedPacket, 0, MAX_PACKET_LENGTH);   

    // Encoding data with the following information
    uint8_t deviceID = 0x01;
    uint8_t packetID = POSITION;
    float f = 4.123;

    uint8_t encodedFloatData[4];
    size_t dataLength = encodeFloat(encodedFloatData, f);

    struct Packet packet;

    packet.deviceID = deviceID;
    packet.packetID = packetID;
    memcpy(packet.data, encodedFloatData, dataLength);
    packet.dataLength = dataLength;

    // encode the packet. 
    size_t packetLength = encodePacket(encodedPacket, &packet);

    // Print the encoded packet to stdio.
    printf("Encoded Packet: ");
    for (int i = 0; i<=packetLength; i++){
        printf(" %d", encodedPacket[i]);
    }
    printf("\n");

}

Encoding a Packet with a List of Floats Data


/* 
C/examples/encodePacketBareExample.c

Author: John Sumskas
Email: j.sumskas@reachrobotics.com
Date: 21/06/2022

*/
#include <stdio.h>
#include "../bplprotocol.h"

void main(void){

    /* **************************** encodePacket() Example with floats for data *************************************** */
    // create a buffer of bytes for your packet to be filled in with.
    uint8_t encodedPacket[MAX_PACKET_LENGTH];
    // Set data to zeros
    memset(encodedPacket, 0, MAX_PACKET_LENGTH);   

    // Encoding data with the following information
    uint8_t deviceID = 0x01;
    uint8_t packetID = POSITION;
    float floatList[5] = {1.2, 2.3, 3.4, 4.5, 5.6};

    uint8_t encodedFloatData[4*5];
    size_t dataLength = encodeFloats(encodedFloatData, floatList, 5);

    struct Packet packet;

    packet.deviceID = deviceID;
    packet.packetID = packetID;
    memcpy(packet.data, encodedFloatData, dataLength);
    packet.dataLength = dataLength;

    // encode the packet. 
    size_t packetLength = encodePacket(encodedPacket, &packet);

    // Print the encoded packet to stdio.
    printf("Encoded Packet: ");
    for (int i = 0; i<=packetLength; i++){
        printf(" %d", encodedPacket[i]);
    }
    printf("\n");

}