Control of Xbee_S1 via AT Commands from UNO

What i want to do is to be able communicate with a XBee / control it / read parameters all via AT commands sent from a UNO. For instance i want to make one XBee as a central node and get specific data from 12 more nodes by sequential polling. For this i need to be able to change DL and DH parameters programmitically . The attached code can do this ... though now i am just reading the SL and SH parameters as an example.

What i want to be able to do is this : When the Xbee sends some response, besides printing it, i want to strip the response dropping the \r at the end. Tried but getting struck. Any suggestions to do this ? For instance for the command ATSL\r , the Xbee will respond with a 16 bit number. I want o get that number into a variable.

// Code to communicate with a Xbee S1 via AT commands 

#include <SoftwareSerial.h>
SoftwareSerial debugSerial(2, 3);       // RX, TX. 

//######################################

void setup() {
  Serial.begin(9600);
  debugSerial.begin(9600);
  debugSerial.println(" Starting The XBee Radio !");
  delay(2000);
}

//######################################

void loop() {

  debugSerial.println("NEW !!");

  Serial.print("+++");               // Open a AT session...
  xbeeResponse();
  Serial.print("ATDH\r");           // Do what you want to...
  xbeeResponse();
  Serial.print("ATSL\r");
  xbeeResponse();
  Serial.print("ATCN\r");            // Close the AT session. 
  xbeeResponse();
  delay(2000);
}

//######################################

void xbeeResponse() {
  while ( !Serial.available());        // Wait for XBee to respond..
  while ( Serial.available() > 0 ) {   // Get the response and send to debug spew
    debugSerial.write(Serial.read());
  }
  debugSerial.println(); 
}

PS : In my project the Xbee is just a wireless data link. None of the end nodes Digital or Analog pins are used. So right now dont need API mode.

Ok .. modified the code to get the response from XBee as char array instead of just printing it out. But the one thing that i am unable to understand is the process works smoothly only when the Serial buffer is cleared by a dummy read() at the beginning of loop(). If i don't do this then the debug print stops with the first few responses.. why is that ?

// Code to communicate with a Xbee S1 via AT commands
#include <SoftwareSerial.h>
SoftwareSerial debugSerial(2, 3);       // RX, TX.

const byte charNum = 20;
char rxBytes[charNum];
//######################################
void setup() {
  Serial.begin(9600);
  debugSerial.begin(9600);
  debugSerial.println(" Starting The XBee Radio !");
  delay(2000);
}

//######################################

void loop() {

  debugSerial.println("NEW !!");

  while ( Serial.available() > 0 ) {  // Clear the Serial buffer..
    Serial.read();
  }

  Serial.print("+++");               // Open a AT session...
  debugSerial.println(xbeeResponse());
  Serial.print("ATDH 0013A200\r");   // Set the required destination address..
  debugSerial.println(xbeeResponse());
  Serial.print("ATDL 4151274B\r");
  debugSerial.println(xbeeResponse());
  Serial.print("ATCN\r");            // Close the AT session.
  debugSerial.println(xbeeResponse());
  delay(2000);
}

//######################################

// Function to get the Xbee response... use as required.
char* xbeeResponse() {
  char endMarker = '\r';
  char inByte;
  static byte ndx = 0;

  while ( !Serial.available());        // Wait for XBee to respond..
  while ( Serial.available() > 0 ) {   // Get the response and send to debug spew
    inByte = Serial.read();
    if (inByte != endMarker) {
      rxBytes[ndx] = inByte;
      ndx++;
    }
    else {
      rxBytes[ndx] = '\0';              // terminate the string
      ndx = 0;
    }
  }
  return rxBytes;
}
//######################################

Can I use this coding for series 2 ?
If no so what is the solution.

Tariqaziz:
Can I use this coding for series 2 ?
If no so what is the solution.

Yes you can. Its juts a Peer to Peer link up and does not use any of the specific commands related to the S2 . All the AT commands that I have used are common to both S1 and S2 . ( Of course you cannot make a S1 talk to S2 ... trust you are aware of that !!)