Serial Event not updating flag?

Hi, I'm not sure what the problem is so I'll describe the symptoms and hopefully I can get some feed back. I'm testing a concept program. So at this point is pretty basic and straight forward.

When I use the serial monitor to test it with a string like this : 12,"abc", 4.5,"def" I expect a response like "My data received is 12 4.5" and I do, but after a second or so and not all the time I get "My data received is 0 0.0"

I tried a few different approaches to see if I can find out why, but not much luck. I'll appreciate any light you can provide. Thank you

boolean myNewDataReceivedFlag = false; 
int integerData=0 ;
float floatData =0;
...

void setup ()
{
 Serial.begin(9600);
...
}
void loop ()
{
...
if (myNewDataReceivedFlag)
{
Serial.print ("My data received is :");
Serial.print (integerData);
Serial.print (" ");
Serial.println (floatData);
myNewDataReceivedFlag = false;
}
...
}

void SerialEvent ()
{
  //HERE I added a delay that seems to solve the problem. 
 // Now I just guessed it. I don't know why it works
 delay (50) ; // I started at 10, and at 50 it seems to be work //added last minute. I would like to remove it, but then the problem comes back

 integerData = Serial.parseInt();
 floatData = Serial.parseFloat();

while (Serial.Read() != -1); // my attempt to flash the incoming buffer. I have no control over the incomming data, but it follows a format like this ##,"xxx",##.##,"xxx"\n and I only care about the numbers
myNewDataReceivedFlag = true;
}

http://arduino.cc/en/Tutorial/SerialEvent

Don't use delay().

"Serial.Rad()"?

Hi cypherrage, Thank you for the comment. I had that problem before adding the delay. After trying a few changes, nothing worked and then I added it, and it seems to solve the problem, but I don't know why. I much rather not to use it.

Sorry about the Serial.Rad, my bad... a typo it should be Serial.Read();. I'll see if the forum lets me fix it.

What defines the start of your significant data? What defines the end of your significant data?

Define those things, and you'll have no need for delay.

You're not thinking about time like the time it takes to start the message or transmit and receive the data bit by bit at 9600 bits per second including added start and stop bits while the AVR is watching at 16 million cycle speed in the same second.

Have a look at the Serial.available() command.

My serial input code generally has a task inside loop to look for data bytes 1 at a time and keep loop() going around the many many times it will just between characters arriving. But then I parse and lex as the data arrives while the usual method is buffer-then-process which Serial.parseWhatever seems to do, but it's real good to have/keep control to an at least make sure there's data before you use it extent. Really, I don't trust those things to handle errors properly. Or most text to number conversion routines. It's like using a spell checker to spell the wrong word properly sometimes, that's what you get for trusting the thing too much.

http://arduino.cc/en/Tutorial/BlinkWithoutDelay

Did you even look at the Serial.Event tutorial? Look at the use of Serial.available.

Thank you GoForSmoke and AWOL, That was the clue I was looking for. I have a fix length string coming into the buffer, and serial event gets seems to be call a lot. Adding my own parser or waiting until the buffer grows to the length of the incoming string seems to work[if I want to use the built in parser] and kind of explains why the delay was working.

Thanks again for the comments.

serial event gets seems to be call a lot.

It gets called any time "loop" isn't being called

I may switch to using leds to debug more.