My Arduino Mega 2560 needs to receive long strings (500-1500 octets) on the USB serial port, while doing very heavy tasks.
I check Serial.available() at the beginning of loop(), and I do other stuff in the other part of loop().
The problem is my stuff in loop() is too long and the serial buffer is sometime full before the next loop() turn. The end of my string is lost.
I was looking for enabling an interrupt which will let me read the incoming byte in priority.
The Serialevent() function is not appropriate because it is called the following loop() turn...
Is there a way to launch an interrupt when a byte is available on the serial buffer ?
You could change the size of the serial receive buffer - no point in reinventing that particular wheel.
But I'd go with the code analysis and refactor approach.
I didn't find how to catch interrupt HardwareSerial.h/HardwareSerial.cpp, so I increased the buffer size and now it works.
I will try to optimize my code however.