简体   繁体   中英

How do I send a payload to an XBee

I have implemented the following code on an embedded platform that attempts to communicate with an XBee . The embedded platform that executes the code below is not an xbee:

int main()
{
   char payload[12] = {0x61,0x88,0x00,0x64,0x00,0x00,0x00,0x00,0x00,0xEC,0x00,0x00}
   payload[2] = 0x10;
   payload[9] = 0x01;
   char data = 'H'; // Send simple ASCII character to XBee
   payload[11]= data;

   while (1)
      sendByteofData(payload,12);
}

void sendByteOfData(char * payload, int len)
{
   int x;

   for (x=0;x<4;x++)
      // This function sends IEEE 802.15.4 frames, and I know it
      // works because they are detected in the [sniffer][3].
      send_IEEE_802_15_4_frame(payload,len);
   }

   payload[2] = payload[2] % 256 + 1;
   payload[9] = payload[9] % 256 + 1;

   if (payload[9] % 256 == 0 )
      payload[9] = 0x01;
   else
      payload[9] %= 256;
}

To my surprise the above code actually sent one byte from the embedded platform to the XBee successfully. however, the infinite loop at the end of main() should have produced a stream of bytes.

My suspicion is I need to set payload[2] and payload[9] correctly, and there is probably a flaw in the incremental modulo 256 algorithm shown above.

How do I get a continuous stream of bytes?

A few thoughts...

  1. Make your payload an array of unsigned char or, even better, uint8_t .
  2. To update payload[2] and payload[9], simplify your code:

     ++payload[2]; ++payload[9]; if (payload[9] == 0) payload[9] = 1; 
  3. Add a delay between sends. You might even need to wait for a response before sending the next character.

Since it's a payload of unsigned 8-bit values, they'll automatically roll from 255 to 0. I assume your special case code for payload[9] is trying to roll from 255 to 1 (instead of 0).

Make sure your payload doesn't need to include a checksum of some sort. Updating those two bytes would have an affect on a checksum byte.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM