XBee

From embeddedTS Manuals
XBee
Xbee.jpg
Released Mar. 2010
Documentation
Datasheet
Product Manual
Mechanical Drawing
Digi International

Overview

The XBee is a 802.15.4 compliant radio module. It connects to the boards using a UART, and several DIO pins. See your SBC manual for more information on the UART and DIO connection.

Getting Started

Digi offers the best introductions to this product. Many of these tutorials or samples are aimed at different platforms, but the UART information will still be relevant.

XBee Learn More

XBee 802.15.4 Digital Input/Output Line Passing

Programming Example

This source example assumes the baud rate is already set up to 9600 (required by command mode). Replace the XBEEDEVICE with your COM port.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <strings.h>

#define XBEEDEVICE "/dev/pts/0"

int main(int argc, char **argv)
{
        int fd, c, res;
        struct termios oldtio, newtio;
        char buf[255] = {0};
        
        fd = open(XBEEDEVICE, O_RDWR | O_NOCTTY ); 
        if(fd <0) 
        {
                perror(XBEEDEVICE); 
                return -1;
        }
        
        tcgetattr(fd, &oldtio); /* save current port settings */
        
        bzero(&newtio, sizeof(newtio));
        newtio.c_cflag = CRTSCTS | CS8 | CLOCAL | CREAD;
        newtio.c_iflag = IGNPAR;
        newtio.c_oflag = 0;

        /* set input mode (non-canonical, no echo,...) */
        newtio.c_lflag = 0;
         
        newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
        newtio.c_cc[VMIN]     = 2;   /* blocking read until 2 chars received */
        
        tcflush(fd, TCIFLUSH);
        tcsetattr(fd, TCSANOW, &newtio);

        // Sending +++ within 1 second sets the XBee into command mode
        write(fd, "+++", 3);

        res = read(fd, buf, 255);
        buf[res] = 0;

        if(0 == strncmp(buf, "OK", 2))
        {
                printf("XBee Detected\n");
        }
        else
        {
                printf("Could not find XBee\n");
        }

        tcsetattr(fd, TCSANOW, &oldtio);

        return 0;
}