while loop delay the serialEvent function?

Arduino Mega2560, IDE 1.0

Before:
Origianlly the loop() includes the syntax of reading a numbers of analog input pin, and do some calculation
The result will be sent to computer through serialEvent() function
I sent one character, Arduino report the value immediately for one time
Everything works fine

After:
loop() changed to call two different functions
The first one was those measurement and calculation syntax mentioned above
the second one will first enable some external relays, entered a while loop, and then disable the relays

Problem:
During the while loop
sending one character will first have no respond,then one set of information will be received from Arduino once the while loop was ended
sending N character will first have no respond, then N set of information will be received from Arduino once the while loop was ended

serialEvent should be based on interrupt, and I wonder why a while loop will delay the serialEvent function

thanks :roll_eyes:

serialEvent should be based on interrupt

It probably should be, but it isn't. It's called after each execution of the loop() function...

int main(void)
{
	init();

#if defined(USBCON)
	USB.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}

	return 0;
}

So the problem is, the program trapped in the while loop and could not get back to loop()
Then the serialEvent() was dead? :astonished:

No, not dead, just not called.

serialEvent should be based on interrupt,

I don't think it should; if it were people would just do daft things like call delay, or do serial I/O in it.