serialUSB serialEvent() equivalent on a SAMD (Pro RF) board

I want to respond to serial input events on a SAMD21 Pro RF board using the usual Arduino pattern of

String inputString = "";        // a String to hold incoming data 
bool stringComplete = false;    // flagwhether the string is complete

setup()
{
    // initialize serial:
    SerialUSB.begin(9600);
    // reserve 200 bytes for the inputString:
    inputString.reserve(200);
...
}

loop()
{
    if(stringComplete) 
    {
            SerialUSB.println("Invalid command: " + inputString + ".");
    }
    // clear the string:
    inputString = "";
    stringComplete = false;
}

// serialEvent[x]() is normally anything from serialEvent()  to serialEvent5() or at least 2 or 3, depending on the board.
void serialEvent[x]() 
{
    while(SerialUSB.available()) 
    {
        // get the new byte:
        char inChar = (char)SerialUSB.read();
        // add it to the inputString:
        inputString += inChar;
        // if the incoming character is a newline, set a flag so the main loop can
        // do something about it:
        if(inChar == '\n') 
        {
            stringComplete = true;
        }
    }
}

I cannot find any documentation of a serialUSB version of serialEvent() anywhere. Or decent documentation of serialUSB in general.

I have tried to search all of the header files, grepped through all of the relevant directories. Does such a thing exist? Does serialUSB map to any of the defined hardware serial ports for SAMD chips (SERIAL0-SERIAL5)?

Or am I just barking up the wrong tree?

Hi wittend,

It looks like there is a note on the [Arduino Reference Page for serialEvent(); that mentions it does not work with SAM-based boards since there is not a hardware UART on like AVR-based Arduinos have.

I did some more digging and found this [issue on the SAMD Arduino Core GitHub repo that goes into why that does not work in some more detail. This [post on the Arduino forums appears to have a fix but it may not work for what you are trying to do over SerialUSB.

You might be able to adjust your personal variants file for the SERCOM ports on your SAMD to make it work, though. [This tutorial on adding more SERCOM ports might help point you in the right direction here.](Adding More SERCOM Ports for SAMD Boards - SparkFun Learn)](Uart: interrupt driven reception: How? - Arduino Zero - Arduino Forum)](UART Interrupt · Issue #238 · arduino/ArduinoCore-samd · GitHub)](serialEvent() - Arduino Reference)