Reading serial output of Xbee radios properly and cleanly

Hello, I have made many programs using arduino and zigbee together, but they are very messy. I want to be able to clean them up. In my code, I have one arduino xB.write a piece of data. Then on another arduino board I have it read those data. The problem is that on the receiving end, I get a jumbled mess of 0-255. I have to glue them together to read something legible.

For example, I receive these data.

126, 0,
14, 144, 0, 19,
162, 0, 64, 185, 209, 180, 215, 238, 1,
2, 167, 205, -1,
126, 0,
18, 146, 0, 19,
162, 0, 64, 185, 209, 180, 215, 238,
1, 1, 0, 0, 1, 2, 17, 95, -1,
126, 0,
14, 144, 0, 19,
162, 0, 64, 185, 209, 180, 215, 238, 1,
2, 166, 206, -1,
126, 0,
18, 146, 0, 19,
162, 0, 64, 185, 209, 180, 215, 238, 1,
1, 0, 0, 1, 2, 17, 95, -1,

I have to program it to start at 126 or FE in hex, then glue all the following bytes together, then read until the Xth bit to receive the byte I am looking for. In the above example I am looking for a 2 then something around 166. As you can see, this string only comes in correctly every second iteration of the 126 being printed. Is there a way to wait until a full string comes in clean so I don’t have to have super messy code to parse it all together? I am trying to follow Faludis wireless book.

Thank you. Code is below.

Sending arduino code (cut very short for this question)

#include <SoftwareSerial.h>
const byte XB_RX = 2;           // XBee's RX (Din) pin
const byte XB_TX = 3;           // XBee's TX (Dout) pin
const int XBEE_BAUD = 9600;     // Your XBee's baud (9600 is default)
const int SERIAL_BAUD = 4800;   // Your XBee's baud (9600 is default)
SoftwareSerial xB(XB_RX, XB_TX);// Designates pins on arduino board to be used for communication


xB.begin(XBEE_BAUD);
xB.write(MSB[i]);   // Writes MSB of sensor [i] to coordinator radio
xB.write(LSB[i]);   // Writes LSB of sensor [i] to coordinator radio

Receiving code(again cut short)

#include <SoftwareSerial.h>
const byte XB_RX = 2;           // XBee's RX (Din) pin
const byte XB_TX = 3;           // XBee's TX (Dout) pin
const int XBEE_BAUD = 9600;     // Your XBee's baud (9600 is default)
const int SERIAL_BAUD = 4800;   // Your XBee's baud (9600 is default)
SoftwareSerial xB(XB_RX, XB_TX);// Designates pins on arduino board to be used for communication

 Serial.begin(SERIAL_BAUD);    // Sets the baud rate for communication via terminal monitor on computer
  xB.begin(XBEE_BAUD);          // Sets the baud rate for communication with the Transmitting Xbee radio

// make sure everything we need is in the buffer packet
  if (xB.available() >= 21) {                 // If available # of bytes is greater than 21 proceed
    if (xB.read() == 0x7E) {                  // Start byte always = 126 or 7E in Hex
      if (xB.read() == 0x00) {                // First bit of packet length, typically 0
        if (xB.read() == 14 + (sensors * 2)) {// Second bit of packet length, in this case it is # of inputs * 2 + 14 preceeding bits
          if (xB.read() == 0x90) {            // 144(90 hex) seems to be the golden value here while 146 is an escaping character and should be avoided

            // blink debug LED to indicate when data is received
            digitalWrite(debugLED, HIGH);
            delay(10);
            digitalWrite(debugLED, LOW);

            // read the variables that we're not using out of the buffer and discard
            for (int i = 0; i < 11; i++) {    // 11 means there are 11 variables between the "144" above and our "Data" bytes
              byte discard = xB.read();       // Discards bytes
            }

            // Begin to read the 2 part bytes sent from the other radio
            for (int i = 0; i < sensors; i++) { // Begins loop the size of # of input sensors 1-5
              MSB[i] = xB.read();               // Reads the MSB of input[i]
              LSB[i] = xB.read();               // Reads the LSB of input[i]
              V[i] = LSB[i] + (MSB[i] * 256);   // Converts multiple 0-256 values of MSB&LSB to a single 0-1024 value
              V_out[i] = V[i] / 4;              // Converts single 0-1024 value (V[i]) to single 0-256 value for voltage output
              V[i] = map(V[i], 0, 1023, 0, 500);// Maps the 0-1024 to 0-500 for serial printing in terms of voltage display
              V[i] = V[i] / 100;                // Arduino does not like changing from an int to a float in the same line??? 500V now equals 5V
              analogWrite(output[i], V_out[i]); // Ouputs sensor voltage(i) to pin (i+5)
              //Serial.print(MSB[i]); Serial.print(" MSBi ");   // Uncomment to print MSB to computer serial terminal screen
              //Serial.print(LSB[i]); Serial.print(" LSBi ");   // Uncomment to print LSB to computer serial terminal screen
              Serial.print("Input_"); Serial.print(i); Serial.print(" = "); Serial.print(V[i]); Serial.println(" Volts ");
            }

            // Now read the current
            MSB_I = xB.read();    // Reads the MSB of the current input
            LSB_I = xB.read();    // Reads the LSB of the current input
            current_bits = (LSB_I + (MSB_I * 256));   // calculates the number of bits from 0-1023 using LSB and MSB (0-255)
            current = current_bits / (205.4 * 0.185);   // Calculates a actual current from the current bits (Amps)
            Serial.print("Current = "); Serial.print(current); Serial.println(" Amps ");
            current_out = current * 100; // Multiply by 100 to get rid of decimal places
            current_out = map(current_out, 0 , 500, 0 , 255);   // Map the 0-5 V to 0-255 for voltage output on arduino pins
            analogWrite(current_pin, current_out);    // Outputs voltage corresponding to input current on other board (0.45A = 0.45V)

            Serial.println(" ");  // Creates new line for serial debugging purposes

            //tone(5, 150);   // Need to work on tone if don't want to use capacitors in final design.
            // Only problem is this requires control ports on CS datalogger which I don't have
            // Somebody else figure this out

          }
        }
      }
    }
  }
}

Please let me know if you want the full code? Thank you!

Please let me know if you want the full code?

That depends. Do you want an answer?

I figured it out. Instead of using faludis example of

if xB.available()

I used

while xB.available()

Fixed everything and now all my zigbee outputs are clean, and full transfers.