Bugs in Serial.read()??[NEW]

Hello~
Today when I try to use my arduino to control servos from through serial port,I find that Arduino Mega may behave wrong in this condition:
(Who teach me how to put code here)
Thank AWOL for teaching me how to post code:)
This is the hole test code for detect a special array of HEX bytes:
FF FF 01 03 00 20 DB
That is a answer packet from my servo.

#include <LiquidCrystal.h>
LiquidCrystal lcd(53, 51, 49, 47, 45, 43);
int ledPin =  13;
int TxEN = 39;
int RxEN = 37;

void setup()
{
  Serial.begin(1000000);
  Serial2.begin(1000000);
  pinMode(ledPin, OUTPUT);
  pinMode(TxEN, OUTPUT);
  pinMode(RxEN, OUTPUT);
  lcd.begin(16, 2);  
}

void loop()
{
  byte data = ReadS(1,0x2B,1);
  Serial.write(data);
  delay(1000);
}

byte ReadS(byte ID,byte address,byte length)
{
  byte RevFlag = 0;
  byte rx_counter = 0;
  byte rx_buffer[] = {0,0,0,0,0,0,0};
  byte data;
  Serial2.flush();
  digitalWrite(TxEN, HIGH);
  digitalWrite(RxEN, LOW);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(ID);
  Serial2.write(0x04);
  Serial2.write(0x02);
  Serial2.write(address);
  Serial2.write(length);
  unsigned int checksum = ID+6+address+length;
  checksum = ~(checksum%256);
  byte sum=checksum&0x000000ff;
  Serial2.write(sum);
  digitalWrite(RxEN, HIGH);
  digitalWrite(TxEN, LOW);
  digitalWrite(ledPin, HIGH);
  while(!(Serial2.available()>0));
  while(Serial2.available()>0)//(!RevFlag)&&
  {
    Serial.write(Serial2.available());
    /*digitalWrite(ledPin, HIGH);
    delay(200);
    digitalWrite(ledPin, LOW);
    delay(200);*/
    //Serial.write(Serial1.read());
    rx_buffer[rx_counter] = Serial2.read();
    rx_counter++;
    switch(rx_counter)
    {
      case 1:
      if(rx_buffer[rx_counter-1] != 0xff)
      {
        Serial.write(rx_counter);
        Serial.write(rx_buffer[rx_counter-1]);
        Serial.write(0xff);
        rx_counter = 0;
      }
      break;
      case 2:
      if(rx_buffer[rx_counter-1] != 0xff)
      {
        Serial.write(rx_counter);
        Serial.write(rx_buffer[rx_counter-1]);
        Serial.write(0xff);        rx_counter = 0;
      }
      break;
      case 3:
      if(rx_buffer[rx_counter-1] != ID)
      {
        Serial.write(rx_counter);
        Serial.write(rx_buffer[rx_counter-1]);
        Serial.write(0xff);        rx_counter = 0;
      }
      break;
      case 4:
      if(rx_buffer[rx_counter-1] != 3)
      {
        Serial.write(rx_counter);
        Serial.write(rx_buffer[rx_counter-1]);
        Serial.write(0xff);        rx_counter = 0;
      }
      break;
      case 5:
      if(rx_buffer[rx_counter-1] != 0x00)
      {
        Serial.write(rx_counter);
        Serial.write(rx_buffer[rx_counter-1]);
        Serial.write(0xff);        rx_counter = 0;
      }
      break;
      case 6:
      data = rx_buffer[rx_counter-1];
      break;
      case 7:
      unsigned int RXchecksum;
      for(byte i=2;i<6;i++)
      {
        RXchecksum += rx_buffer[i];
      }
      RXchecksum = ~(RXchecksum%256);
      byte RXsum=RXchecksum&0x000000ff;
      if(rx_buffer[rx_counter-1] != RXsum)
      {
        Serial.write(rx_counter);
        Serial.write(rx_buffer[rx_counter-1]);
        Serial.write(0xff);        rx_counter = 0;
      }
      else RevFlag = 1;
      break;
    }
  }
  /*for(byte i=0;i++;i<7)
  {
    lcd.print(rx_buffer[i]);
  }*/
  Serial2.flush();
  return(data);
}

lcd is just for testing.
When I saw the LED on ,I know that the MC is waiting for the packet.And I send FF FF 01 03 00 20 DB to the Serial2 port,I recieve this from Serial port:
06 05 04 03 02 05 20 FF 01 01 DB FF 97
I think the right answer is
07 06 05 04 03 02 01 00 20
20 is the data I want in loop();

Please Help me~ Thank you!
windrunner

Who teach me how to put code here

Click on "modify".

Highlight the code in the editor, then click on the "#" button in the toolbar.

BTW " While" is not a C keyword. Please copy the code exactly as it is in the IDE.

While(!(Serial1.available()>0));
 Serial.write(Serial1.available());
 a[i] = Serial.read();

Shouldn't that be a Serial1.read ?

As westfw points out your code fragment does not make a lot of sense as is.

But the right answer is 07 06 05 04 03 02 01 00 ,because I put 7 bytes to Serial1 ,so the Serial1.available() must return 7.

If you process serial data in a fast loop, the typical output (number of bytes pending to be read) would be 01, 01, 01, 01, 01, 01, 01, 00. The 7 bytes will not arrive instantly and as you remove one byte (Serial.read) your code will be waiting for the next byte to arrive.