Howto adapt SerialEvent to Mega Serial 1


I have a mega2560 controlling a series of slow response time hardwares, resulting in a LOOP cycle time of the order of several seconds.

I have been using SerialEvent ( on Serial port 0 of the Mega, to handle gracefully user inputs, the arduino taking care of them only when a full line (ended by \n), is ready. This is handled in the background, and polled every time the LOOP is cycled.

Now, I need in addition to get data from a RS232 device, that is spitting out data semi-continuously; The data evolves slowly with time, so it is not important if some intermediate data is lost. I have added a Max232 chip, that adapts the RS232 signal to voltages compatible with the Arduino, and this is hooked on serial port 1 of the Mega.

I was planning to get once in a while (and without having to wait for them) “full lines” of RS232 data from serial 1, via something akin to SerialEvent (and with a small modification taking care in serialEvent() to erase data from serial 1 if the string collecting message [inputString] would be nearly filled).

BUT: SerialEvent does not seem to work, when simply (in serialEvent()) changing while (Serial.available()) {
by: while (Serial1.available()) {

QUESTION: What would be the simple modification (if it is possible easily), that would make a “Serial1Event” work (with Serial 1 inputs), in addition to a “Serial0Event” that is handling Serial 0 inputs ?

There are serialEvent() callbacks for all 4 serial ports on the Mega.

Great, exactly what I need !

From the serialEvent() example

SerialEvent occurs whenever a new data comes in the hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay response. Multiple bytes of data may be available.

It is not run in the background, nor is it run during delay()'s.