Shop OBEX P1 Docs P2 Docs Learn Events
XBee command mode — Parallax Forums

XBee command mode

RsadeikaRsadeika Posts: 3,837
edited 2013-02-02 09:03 in Propeller 1
I am trying to get a C program to communicate with the XBee in AT command mode, without much success. Below, in the program, when I type in "+++", I am expecting to see an "OK" on the screen, but that is not happening.

I want to access more than one XBee from a base XBee unit, and I need to give each one of the XBee units some AT commands to have the individual XBee units in the correct receptive mode. Any help would be appreciated, once I get the hang of this, I might develop an XBee command library, if it has not been done yet.

Ray
/**
 * @file test_xb_opp.c
 * This is the main test_xb_opp program start point.
 */
#include <propeller.h>
#include <stdio.h>
#include <stdlib.h>

FILE *xbee;

/**
 * Main program function.
 */
int main(void)
{
    char cin[5];
    char xin[5];
    
/*                           Tx  Rx         */
    xbee = fopen("SSER:9600, 22, 23","r+");


    waitcnt(CLKFREQ + CNT);
    printf("Start program\n");

    while(1)
    {
        gets(cin);
        fflush(stdin);
        fprintf(xbee,"%s",cin);
        fgets(xin,5,xbee);
        fflush(xbee);
        puts(xin);
    }
    return 0;
}

Comments

  • ersmithersmith Posts: 6,054
    edited 2013-02-01 14:20
    You always need to fflush() any handle when switching between output (fprintf) and input (fgets), or vice-versa.

    Generally my suggestion is to open two separate handles, one for input and one for output (just like the standard C library opens stdin for input and stdout for output), rather than using one with "r+". So I'd suggest replacing the FILE *xbee with FILE *xbeein, *xbeeout and adjusting your code accordingly.

    Eric
  • RsadeikaRsadeika Posts: 3,837
    edited 2013-02-02 07:21
    There was a thread started by Rob, which showed some functioning code to deal with XBee AT command modes. Below is a version that I modified a little for my own specific use. The version below you manually type in the "+++", and then it does the DH/DL commands. At this point I am just trying to figure out what is going on with the AT commands. I still have not figured out what the specific command sequence is for setting of the destination address of the XBee. Sending out just the DH/DL command gets you the address of the XBee you are communicating with, that part works so far. I am using the series 1 XBee modules, at least that part is somewhat straight forward.

    The other part of the discovery is that 'SSER' does not work as expected for this setup, you have to use 'FDS'. It is starting to look like if you want to deal with some specific project, like the XBee, you just might have to build some specific libs for the project, so when is simpleIDE going to get that functionality? I try to avoid the command line version for building libs, although I have not tried it that just yet. So, does anybody have an example command sequence for setting the destination address?

    Ray
    /**
     * @file test_xb_opp.c
     * This is the main test_xb_opp program start point.
     */
    #include <stdio.h>
    #include <propeller.h>
    //#include <driver.h>
    
    extern _Driver _FullDuplexSerialDriver;
    
    
    _Driver *_driverlist[] = {
      &_FullDuplexSerialDriver,
      NULL
    };
    
    FILE *xbeein;
    FILE *xbeeout;
    
    char *
    rfgets(char *buf, int size, FILE *fp)
    {
      int c; 
      int count = 0;
    
    
      --size;
      while (count < size) {
        c = fgetc(fp);
        printf("Saw %d '%c'\n", c, c==13?' ':c);
        if (c == '\n' || c == '\r') break; // Swapped this to drop line terminator
        if (c < 0) break;
        buf[count++] = c;
      }
      buf[count] = 0;
      return (count > 0) ? buf : NULL;
    }
    
    
    int main (int argc,  char* argv[])
    {
       char serData[64];
       char atdh[9];
       char atdl[9];
       char cin[9];
       char xin[9];
    
       xbeein  = fopen("FDS:9600,22,23", "r");
       xbeeout = fopen("FDS:9600,22,23", "w");
       if (xbeein == 0) {
          puts("FATAL: fopen(serIn)\n");
          return 0;
       }
       if (xbeeout == 0) {
          puts("FATAL: fopen(serOut)\n");
          return 0;
       }
    
    
       // This avoids the need to fflush() when entering
       // command mode (The +++ sequence can't have a line
       // terminator, else you violate the gaurd time)
       setvbuf(xbeeout, NULL, _IONBF, 0);
    
    
       memset(serData, 0, 64);
    
    
       memset(atdh, 0, 9);
       memset(atdl, 0, 9);
       waitcnt(CNT+CLKFREQ); // Make sure the line has been quiet in case a restart just occurred
       puts("Program start");
       
       while(1)
       {
            gets(cin);          // Type in manually "+++"
            fputs(cin,xbeeout);
            rfgets(xin, 9, xbeein);
            fputs("ATDH\n", xbeeout);  // Series 1
            rfgets(atdh, 9, xbeein);
            fputs("ATDL\n", xbeeout);  // Series 1
            rfgets(atdl, 9, xbeein);
            fputs("ATCN\n",xbeeout); //Drop out of command mode
       }
    /*   puts("+++");
       
       fputs("+++", serOut);
       rfgets(serData, 64, serIn);
       
       puts(serData);
       puts("ATSH");
       
       fputs("ATSH\n", serOut);
       rfgets(atsh, 9, serIn);
       
       puts(atsh);
       puts("ATSL");
       
       fputs("ATSL\n", serOut);
       rfgets(atsl, 9, serIn);
       
       puts(atsl); */
    
    
       return 0;
    }
    
  • jazzedjazzed Posts: 11,803
    edited 2013-02-02 09:03
    Rsadeika wrote: »
    you just might have to build some specific libs for the project, so when is simpleIDE going to get that functionality?

    Better library support and sharing is in the next release.
Sign In or Register to comment.