Duplicate data on serial read

Hi,
I'm using the OnSerialEvent to detect data coming from my GPS and after I read in the data I use the Serial.flush() to clear the input buffer. However, when a new serial event occurs, I get the same data again. Currently the data is repeating once and then new data comes in. Why does flush not clean out the buffer? What could be causing a new serial event other than a new NMEA sentence coming in from my GPS? Code is below. Appreciate any advice anyone might have.
Jim

//-----------------------------------------
String NMEASentence;
int stringComplete = false;
int i;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if(stringComplete) {
    Serial.print("Start dump: ");
    Serial.print(NMEASentence);
    Serial.println(" :End Dump");
    Serial.flush();
    stringComplete = false;
  }
}

void serialEvent() {
  while (Serial.available()) {
    char byteGPS = (char)Serial.read();     
    if (byteGPS == '

Moderator edit: [code] ... [/code] tags added. (Nick Gammon)) {
     NMEASentence = "$";
   }
   else if(byteGPS == 10 || byteGPS == 13) {
     stringComplete = true;
   }
   else {    
     NMEASentence += byteGPS;
   }
 }
}
//----------------------------------------


*Moderator edit:* <mark>[</mark><mark>code</mark><mark>]</mark> ... <mark>[</mark><mark>/code</mark><mark>]</mark> tags added. (Nick Gammon)
    Serial.flush();
    stringComplete = false;

You don't need to "flush" the serial for a start. Second you might empty out the string. eg.

    NMEASentence = "";
    stringComplete = false;

Personally I would not even use the String class. It is very likely to fragment memory, and then crash. Just use a static buffer of the appropriate size, and append to it.

Thanks,
Originally I didn't use String but I changed it over as part of the "Let's try this... oh, that didn't work" iterative process. Just to get this into terminology I (non-software guy) understands, are you suggesting I use a character array in place of the string or is what you're describing different (if so I would appreciate more info).

I've read that flush does not do what I want it to do but what I was expecting was that after I read the data out of the serial buffer the buffer would be automatically reset and the next serial event would be new data. This obviously isn't happening. Should I wait some millis after the read is complete before I believe any serial events? Could it be that I didn't fully read out the data in the serial buffer so it didn't reset?

Thanks for your thoughts.

The serialEvent handler is called every time loop() is executed and there are characters waiting in the serial buffer. Serial.flush() is not doing what you think it's doing. It clears the outgoing buffer but does not affect the receive buffer. To clear the incoming buffer use this code line:

while (Serial.available()) Serial.read();

salacain:
Should I wait some millis after the read is complete before I believe any serial events? Could it be that I didn't fully read out the data in the serial buffer so it didn't reset?

No, not at all. This is wild speculation.

You didn't clear your own buffer. End of story. You don't need to delay, you don't need to flush. And personally I never use serialEvent.

Here: