Skip to content

Instantly share code, notes, and snippets.

@kiyoto1022
Created Nov 5, 2021
Embed
What would you like to do?
#include <stdlib.h>
#define XBEE_FRAME 0x7E
#define CHECK_SUM_LENGTH 1
// API frame structure
// https://www.digi.com/resources/documentation/Digidocs/90001942-13/concepts/c_api_frame_structure.htm?tocpath=XBee%20API%20mode%7C_____2
struct api_frame {
int type;
int address_64[8];
int address_16[2];
int receive_options;
int number_of_samples;
int digital_channel_mask[2];
int analog_channel_mask;
};
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
long delimiter = Serial.read();
if (delimiter == XBEE_FRAME) {
delay(50);
long len_msb = Serial.read();
long len_lsb = Serial.read();
long frame_data_length = len_msb + len_lsb;
long rf_data_length = frame_data_length - (sizeof(struct api_frame) / sizeof(int));
// Read
int received_data[frame_data_length];
for (int i = 0; i < (frame_data_length); i++) {
received_data[i] = Serial.read();
}
// Drop the checksum.
Serial.read();
// Show data
Serial.print("Receive data from xbee [ " + String(delimiter, HEX) + " " + String(len_msb, HEX) + " " + String(len_lsb, HEX) + " ");
for (int i = 0; i < sizeof(received_data) / sizeof(int); i++) {
Serial.print(String(received_data[i], HEX) + " ");
}
Serial.println("]");
// Include in the structure
struct api_frame *frame = (struct api_frame*)received_data;
int *rf_data = (received_data + (sizeof(struct api_frame) / sizeof(int)));
Serial.println("delimiter :" + String(delimiter, HEX));
Serial.println("len_msb :" + String(len_msb, HEX));
Serial.println("len_lsb :" + String(len_lsb, HEX));
Serial.println("type :" + String(frame->type, HEX));
Serial.println("address_64 :" + String(frame->address_64[0], HEX) + String(frame->address_64[1], HEX) + String(frame->address_64[2], HEX) + String(frame->address_64[3], HEX) + String(frame->address_64[4], HEX) + String(frame->address_64[5], HEX) + String(frame->address_64[6], HEX) + String(frame->address_64[7], HEX));
Serial.println("address_16 :" + String(frame->address_16[0], HEX) + String(frame->address_16[1], HEX));
Serial.println("receive_options :" + String(frame->receive_options, HEX));
Serial.println("number_of_samples :" + String(frame->number_of_samples, HEX));
Serial.println("digital_channel_mask :" + String(frame->digital_channel_mask[0], HEX) + String(frame->digital_channel_mask[1], HEX));
Serial.println("analog_channel_mask :" + String(frame->analog_channel_mask, HEX));
Serial.print("rf_data :");
for (int i = 0; i < rf_data_length; i++) {
Serial.print(String(rf_data[i], HEX));
}
Serial.println("");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment