This program checks for the presence of an XBee module in the TS-7553 socket.
Note that the XBee is connected to a serial port in the FPGA. The serial port needs to be activated by xuartctl before it can be used.
#include <sys/types.h> #include <sys/stat.h> #include <sys/select.h> #include <fcntl.h> #include <termios.h> #include <stdio.h> #include <strings.h> #include <unistd.h> #include <sys/types.h> #include <sys/time.h> int main( int argc, char **argv ){ int fd, res, n; struct termios oldtio, newtio; char buf[255] = {0}, *c; char comport[255]; const char cmd[] = "xuartctl --server --port 3 --speed 9600 2>&1"; FILE *fp; fd_set input; struct timeval timeout; // setup com port using xuartctl fp = popen( cmd, "r" ); while( fgets( buf, sizeof buf, fp )){ //printf( "Received: %s-\n", buf ); c = strchr( buf, '=' ); strcpy( comport, c+1 ); *(comport+strlen(comport)-1) = 0; // remoew new line printf( "Using port %s\n", comport ); } pclose( fp ); // open port fd = open( comport, O_RDWR | O_NOCTTY ); if( fd < 0 ){ // nope, bail perror( comport ); return( -1 ); } // ----- Configure port ----- // save current port settings tcgetattr( fd, &oldtio ); // define timeout timeout.tv_sec = 3; timeout.tv_usec = 0; // define new terminal structure bzero( &newtio, sizeof( newtio )); newtio.c_cflag = CRTSCTS | CS8 | CLOCAL | CREAD; newtio.c_iflag = IGNPAR; cfsetispeed( &newtio, B9600 ); cfsetospeed( &newtio, B9600 ); newtio.c_oflag = 0; newtio.c_lflag = 0; // set input mode (non-canonical, no echo,...) 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 printf( " Sending +++\n" ); write( fd, "+++", 3 ); // initialize "input" to contain fd: FD_ZERO( &input ); FD_SET( fd, &input ); n = select( fd+1, &input, NULL, NULL, &timeout ); if( n < 0 ) printf( "select() failed\n" ); else if( n == 0 ) printf( "TIMEOUT, no XBee\n" ); else{ res = read( fd, buf, 250 ); buf[res] = 0; //printf( "Received: %s\n", buf ); if( 0 == strncmp( buf, "OK", 2 )){ printf( "XBee Detected\n" ); }else{ printf( "Something else responded!\n" ); } } tcsetattr( fd, TCSANOW, &oldtio ); return( 0 ); }
To make the program, name this file “hello.c: and simply type “make hello” at the command line.