Created
September 18, 2018 11:57
-
-
Save kategray/57ed7aa0569c1aa13c876ad48531f3d3 to your computer and use it in GitHub Desktop.
XBEE Packet Calculation
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
#include <stdio.h> | |
#include <stdint.h> | |
#define CRC_LOCATION 13 | |
#define DATA_START 3 | |
int main(void) { | |
// your code goes here | |
uint8_t packet[] = {0x7E, 0x00, 0x0A, 0x01, 0x01, 0x50, 0x01, 0x00, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0X00}; | |
printf ("CRC: 0x%.2X\n", packet[CRC_LOCATION]); | |
// Check if the CRC is valid | |
uint16_t checksum = 0x00; | |
for (int i = 3; i < sizeof(packet); i++) { | |
printf ("byte %d: 0x%.2X\n", i, packet[i]); | |
checksum += packet[i]; | |
} | |
checksum &= 0x00FF; | |
printf ("Checksum value: 0x%.2X\n", checksum); | |
printf ("Checksum is %s.\n", (0xFF == checksum) ? "valid" : "invalid"); | |
// Calculate the correct checksum | |
checksum = 0x00; | |
packet[CRC_LOCATION] = 0x00; | |
for (int i = 3; i < sizeof(packet); i++) { | |
printf ("byte %d: 0x%.2X\n", i, packet[i]); | |
checksum += packet[i]; | |
} | |
checksum &= 0x00FF; | |
checksum = 0xFF - checksum; | |
printf ("Correct Checksum: 0x%.2X\n", checksum); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment