Reading Serial Data When it Becomes Available

I need to read the byte as soon as it become available in the register, instead of when I call the SerialEvent() function at the end of the loop. Is there a way of doing this with an interrupt, or would I have to modify the already existing code?

instead of when I call the SerialEvent() function at the end of the loop.

You don’t call serialEvent() at the end of loop(). It is called for you, if there is serial data to be read and you have defined a function named serialEvent().

I need to read the byte as soon as it become available in the register

Why? Serial data arrives slowly. Why is it necessary to drop everything, and respond the microsecond that data arrives?

It is far more likely that you need to rewrite your sketch to get rid of blocking code, so that you can check for serial data in the usual way far more often.

Is there a way of doing this with an interrupt

The arrival of serial data already generates an interrupt. The interrupt handler reads the data and stores the byte in the incoming buffer, if there is room.

would I have to modify the already existing code?

I suspect that if you knew how, you wouldn’t need to ask this question. But, the answer is yes.

Why? Serial data arrives slowly. Why is it necessary to drop everything, and respond the microsecond that data arrives?

Partially because I don't want the loop to continue with outdated data, and partially to compare the two sketches and see any differences in performance. The way the my sketch is now it works reasonably well, but I want to try reading the data as soon as it comes in.

The arrival of serial data already generates an interrupt. The interrupt handler reads the data and stores the byte in the incoming buffer, if there is room

How would I change this interrupt to save the data to global variable?

How would I change this interrupt to save the data to global variable?

With a text editor.

Even capturing the SerialEvent is unlikely to get you any discernable performance gain. Read this