xBee loopback with hardware flow control

Howdy y'all,

Heres some code I found really handy. This sketch turns your arduino w attached xBee into a loopback device for X-CTU's loopback test. It uses newSoftSerial to make a second serial port on pins 2&3, and uses pin 4 for flow control, specifically the CTS line (pin DIO7 on the xBee).

#include "NewSoftSerial.h"

#define CTS 4

NewSoftSerial xbeeSerial =  NewSoftSerial(2, 3);

char data[220]; 
byte ctsStatus;

void setup()
{
  pinMode(13, OUTPUT);
  pinMode(CTS,INPUT);
  Serial.begin(9600);
  Serial.println("Arduino Console");
  xbeeSerial.begin(9600);
  xbeeSerial.println("xBee Port");
}

void loop()
{
  int i = 0;
  data[0] = 0;
  while (xbeeSerial.available())
  {                                     //read the aether
    data[i] = (char) xbeeSerial.read();
    Serial.print(data[i], BYTE);
    i++;
  }
  ctsStatus = digitalRead(CTS);
  if (ctsStatus == 0)                    // inverse logic, we are clear on 0
  {
    for (int j = 0; (i != j); j++)
    {                                       // spray it back
      Serial.print(data[j], BYTE);
      xbeeSerial.print(data[j], BYTE);
   
    }
  }
}