Serial

serialEvent()

Serial.serialEvent, serialEvent, Serial1.serialEvent, serialEvent

A family of application-defined functions that are called whenever there is data to be read from a serial peripheral.

  • serialEvent: called when there is data available from Serial
  • usbSerialEvent1: called when there is data available from USBSerial1 if this port is available
  • serialEvent1: called when there is data available from Serial1
  • serialEvent2: called when there is data available from Serial2 if this port is available
  • serialEvent4: called when there is data available from Serial4 if this port is available
  • serialEvent5: called when there is data available from Serial5 if this port is available

The serialEvent functions are called in between calls to the application loop(). This means that if loop() runs for a long time due to delay() calls or other blocking calls the serial buffer might become full between subsequent calls to serialEvent and serial characters might be lost. Avoid long delay() calls in your application if using serialEvent.

Since serialEvent functions are an extension of the application loop, it is ok to call any functions that you would also call from loop(). Because of this, there is little advantage to using serial events over just reading serial from loop().

// EXAMPLE - echo all characters typed over serial

void setup()
{
   Serial.begin(9600);
}

void serialEvent()
{
    char c = Serial.read();
    Serial.print(c);
}