Receiving a string via Xbee in API Mode

avrcmicrocontrollerxbee

In my earlier question at this link , i asked a question on how to use minicom in linux to communicate between two Xbee's in API mode and got the answer. While i tried the same with XCTU on Windows one one side and AVR + Xbee on the other side,i was able to communicate between 2 Xbee.

On the AVR side, i had written this code to receive the char and store it in string.

#include <yuktix.h>
#include <debug.h>
#include <uart2560.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <debug.h>

/*for sensor readings*/
#include <AVRI2C.h>
#include <spi.h>

//char Id;
//char cmd;
static char ch;
static char *frame;
static uint8_t index = 0;
int main(){

    init_processor();
    uart_init(0,B9600);
    uart_init(1,B9600);
    uart_init(2,B9600);

    for(int j = 0; j < 1000 ; j++)
        _delay_ms(1);

    uart_puts(0, " \r \nstarting Xbee Test \r \n");

    while(1){

    while(uart_available(2) > 0){
        KDEBUG("Inside uart_available");
        KDEBUG("\n");
        ch = uart_getc(2);
        uart_putc(0,ch);
        KDEBUG("Saving it in frame \n");
        *frame++ = ch;
        KDEBUG("saved in frame \n");
        //if(ch == '\\');
        }
        KDEBUG("COming out of While loop");
       *frame = '\0'; 
        KDEBUG(frame);


   }
}

As per my last question answer, i am receiving char on the AVR side. As every frame of XBEE API mode start with 7E, where 7E is equivalent to ~, i am receiving it, but some of the chars were unreadable and data which i send in the API frame is totally readable. My question is

  1. How can i store received API frame in a string as my code don't seems to work.
  2. Should i convert the string into HEX via some binary_to_hex code and then parse it , as i need to know from which address this data is coming, what kind of frame it is, what is the number of the bytes in the frame, so that after excluding the source address , i can take out the data.
  3. Am i missing anything. Data on the AVR side will always be in Binary, but how can i process the data?

Best Answer

1) You need to intialize frame as array and it's length is the maximum frame length you may recieve. Now you are only making a pointer which point to one place then you write to bytes after that in RAM and it may cause errors. If you didn't see errors you only get lucky!!

2) Numbers is numbers 1 is the same as (0b00000001) in binary as the same as (0x01) in hex. you only need to compare as you want. For example to compare the start byte you could do one of the followings:

if(frame[0] == 0x7E){
    // Here you are comparing with Hexadicimal
}

if(frame[0] == 126){
    // Here you are comparing with Decimal
}

if(frame[0] == 0b01111110){
    // Here you are comparing with Binary
}

Note that any conditions of the above have the same machine code in AVR. You are only represent the number to yourself.

Also i have some notes on that code:

1) Why are you add statickeyword to ch , *frame , index it has no meaning at all here.

2) For frame it is better to be unsigend char or uint8_t as char alone is signed. intialize as array like uint8_t frame[10];