Getting byte data from xbee into the Arduino Mega

Hi,

I want to read byte #11 and byte#12, e.g. 0x02 and 0x7D and combine them with word() to make one number which is the voltage from the sending pot.

I can’t for the life of me figure out why I can’t isolate byte #11 and byte #12 from Serial.read when I know the data is there.

Setup:

Two xbee series 1

Transparente mode

Arduino Mega

Logic level converter - between the mega and receiving xbee

Using this program I can read the data being transmitted from the Transmitting xbee (connected to a pot) to the receiving xbee which is connected to a logic level converter then to the Mega Serial1 port.

int data = 0;

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop()
{
 if(Serial1.available() > 0)  // checks for data
 
  {
   
   data = Serial1.read();  // reads data from Serial1
    
    if(data <= 0x0F)    // prints a leading zero on Serial.print if
                        // Hex is less than or equal to 15 as arduino 
                        // wont print a leading 0.  This took some time to figure out with help.
      {
        Serial.print("0");
      }
    
    Serial.println(data,HEX);  
    
  } 
}

The HEX results are

7E 00 0A 83 12 34 1B 00 01 02 00 02 7D 9A then back to 7E…

I need the payload 02 7D bytes #11 and #12 after the delimiter.

Problem:

I can’t figure out why the folowing code does not work. I just want to see bytes 02 and 7D so I can combine them and using the arduino word() function put them together 027D (637 decimal) to drive a motor… I tried and tried to figure it out…

int hibyte = 0;  //byte # 11
int lowbyte = 0;  // byte # 12
int data = 0;
int Voltage = 0;

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop()
{
 if(Serial1.available() > 0)  // checks for data
 {
   data = Serial1.read();  // reads data from Serial
   
   if(data==0x7E)  //start delimiter
     {
       for(int i = 1; i <=10; i++)   //counter
         {
           Serial1.read();   //removes bytes 1 through 10 from the buffer
         }
         
       hibyte = Serial1.read();
       lowbyte = Serial1.read();
   
       
       Serial.print(hibyte);
       Serial.print(lowbyte);
       
  } 
}
}

After running this code the result is -1-1-1-1-1- forever. I know there is data there as I can see it from the 1st program above. What am I doing wrong?

Thank you so much!!

Since I don’t use an Arduino I am not familiar with debugging methods on that processor and development tools.

In general to program this in C I would read the serial data from the XBee into an interrupt driven ring buffer (FIFO).

When data is in the ring buffer pull the earliest data and check that it is the Frame start byte (0x7E). Put the byte into the first element of an array (named data_array), then the next 13 bytes into the array (if the Frame contains 14 bytes).

To get the two data bytes into a 16 bit value use:

int data_value;

data_value = (data_array[11]<<8) + data_array[12];

It looks like your ‘for’ loop is skipping bytes 1 to 9 (i <=10 which means i is less than OR equal to 10) then reading byte 10 & 11as the high and low byte.

These are 0x00 and 0x02 which are not printable ASCII characters.

Can you step through the code and check that the ‘for’ loop is doing what you thank it should?

Or just try changing the ‘for’ loop to “for{ i=1; i<11; i++}” and see if that works.

The 0x02 value isn’t printable ASCII but the 0x7D is a ‘}’ character.

Maybe what you what to do is convert the byte value to two ASCII values before printing.

Thank you Waltr. I will try and see if this works.

Scott