Shop OBEX P1 Docs P2 Docs Learn Events
Inbound Strings with Propeller C over the XBEE — Parallax Forums

Inbound Strings with Propeller C over the XBEE

egeisegeis Posts: 5
edited 2013-11-24 14:55 in Accessories
I am working on a Activity bot, equipped with 4 QTI sensors and an XBEE wireless antenna (starter kit), that uses an application to find the shortest path within a maze. Most decisions are made by the application which will require two-way communication to the activity bot and host PC. The application receives information on each node the robot discovers while the application decides that next course of action then replies to the robot awaiting instructions. Before I start writing the application I've been trying to send those commands using the X-CTU but I've ran into two problems. I cannot find a way to read or piece together an entire string on the Activity Bot side (using C). I have also been unable to locate any examples that are similar to what I'm trying to do on learn.parallax.com and on other websites. If anyone has any suggestions or knows how to read a string from an XBEE I could use some help. This was not the part of the project I though id be banging my head against the wall on... I figured it was going to be the modified depth first search I was also supposed to write this weekend :smile:

Below is the code I've been working with, I've removed most of the COG / sensor code from the QTI to keep it simple.
#include "simpletools.h"
#include "simpletext.h"
#include "fdserial.h"
#include "abdrive.h"
#include <driver.h>


#define XBEE_CONF_RX_PIN 6
#define XBEE_CONF_TX_PIN 5
#define XBEE_CONF_BAUD 9600
#define XBEE_CONF_CONN_WAIT 10 //Timeout while Connecting or Reconnecting


//QTI Configuration
#define QTI_PIN_MIN 0
#define QTI_PIN_MAX 3
#define QTI_ARRAY_SIZE 100 //TODO: Test Speed and Distance this covers and adjust this size.

//Global Variables




//Full Duplex Serial Driver used for output.
fdserial *xb;


//Output buffer
char buf[100];


//Forward Declarations
void wireless(void *par);
void sensors(void *par);
int qti(int pin);
int xbee_connect();


int main()
{
freqout(4,500,2000);
xbee_connect();


while(1)
{
//TODO: Interpret Commands
}


dprint(xb, "[DRIVER] Stopping Robot, if not already stopped.");
drive_speed(0,0);


dprint(xb, "[XBEE Wireless] Shuting Down!");
fdserial_close(xb);
freqout(4,5000,1500);
return 0;
}


/**
* Xbee Wireless Conncetion
*
* Creates the Serial connection and returns whether the two way test was a success.
*
* @return INT
*/
int xbee_connect()
{
int success = 0;


//Create the Serial Connection
xb = fdserial_open(XBEE_CONF_RX_PIN, XBEE_CONF_TX_PIN, 0, XBEE_CONF_BAUD);
dprint(xb, "[XBEE Wireless] Started!\n");


//Empties the Recieving and Transmitting Buffers
fdserial_rxFlush(xb);
fdserial_txFlush(xb);


//Signals the Application to Send a Response through the XBEE.
dprint(xb, "::SEND_TEST\n");
//Wait for a response
dprint(xb, "[XBEE Wireless] Waiting for test response.\n");
while(1)
{
//int ch = fdserial_rxCheck(xb);
char buf[10] = {'\0'};
int n = read(xb,buf,10);
if (buf[0] != '\0')
{
dprint(xb,buf); //Echo back the STRING
}


}


return success;
}

My idea was to send "::RECIEVED" for this test. Although for testing i was originally using "TEST" to see if the entire string could be echoed.

Comments

  • jazzedjazzed Posts: 11,803
    edited 2013-11-24 08:57
    Hi, sounds like a great project.

    Use the simpletext functions like readStr(...), readDec(...), or dscan(...).

    The fdserial functions can be used for non-blocking checks of data before reading fdserial_rxReady(...).

    Documentation for simpletext and fdserial are in the Documents/SimpleIDE/Learn/Simple Libraries folders or on-line.
    https://propsideworkspace.googlecode.com/hg/Learn/Simple Libraries Index.html
  • egeisegeis Posts: 5
    edited 2013-11-24 13:34
    Got it to work, thank you.

    I kept looking in Serial and FdSerial for the answers, along with simple tools. Should have gone back to the main directory, next time I shall. Once this project is completed, I plan to release the code for others to use as a example.

    xb is FdSerial connection
      while(1)
      {    
        //A buffer Large enough to hold the string.    
        char buf[20] = {'\0'};    
    
    
        //Read the xb buffer and put ?copy/point? it to my char array.
        readStr(xb,buf,20); 
    
    
        //Echo back the STRING, if the read buffer is not empty.    
        if (buf[0] != '\0') 
        {
          char str[60];
          sprintf(str, "\n[XBEE RECIEVED] %s\n", buf);
    
          dprint(xb,str); 
    
          if (strcmp(buf,"TEST_RECIEVED")==0) {
            dprint(xb,"Success!\n");
          }
        }
      }
    

    Based on my testing of readStr, it wont output till the buffer has the "max size to read" or the buffer contains an carriage return.
    MOVE_NORTH..........MOVE_NORTH........
    .[XBEE OUT] MOVE_NORTH........
    .MOVE_NORTHMOVE_NORTHMOVE_NORTHMOVE_NOR
    .[XBEE OUT] MOVE_NORTHMOVE_NOR
    

    I easy solution is to either manually add 0D to the packet that is being sent form X-CTU and in the future on the application it is send from add the carriage return before sending.
  • jazzedjazzed Posts: 11,803
    edited 2013-11-24 14:55
    Glad it works.
    egeis wrote: »
    Based on my testing of readStr, it wont output till the buffer has the "max size to read" or the buffer contains an carriage return.

    Yes, that's right. the doc doesn't say anything about the return though.
Sign In or Register to comment.