Interrupt when serial data available

Hi all,

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 ?

Thanks for your reply.

Is there a way to launch an interrupt when a byte is available on the serial buffer ?

Serial data arrival already triggers an interrupt. You could possibly modify that code to do other stuff.

I wouldn't recommend that, though. Refactoring your code will, in the end, produce more maintainable code.

Thanks for your answer.

Do you know how can I catch this interrupt ?

Do you know how can I catch this interrupt ?

Look in the HardwareSerial.cpp and HardwareSerial.h files.

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.

Thanks for the replies.

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.

Thanks.

I have a similar issue. How did you increase the size of the serial buffer? Also, what baud rate are you running that port at?