readStringUntil() reads characters from the serial buffer into a String. The function terminates if it times out (see setTimeout()).
Serial.readStringUntil() inherits from the Stream utility class.
Use the following function to read the incoming serial data until a terminator is found:
Serial.readStringUntil(terminator)
The function admits the following objects and parameters:
Serial: serial port object. See the list of available serial ports for each board on the Serial main page.
terminator: the character to search for. Allowed data types: char.
The function returns the entire String read from the serial buffer, up to the terminator character. If the terminator character can’t be found, or if there is no data before the terminator character, it will return NULL.
The following code reads the serial incoming data until a newline is received:
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Enter a word and press Enter:");
}
void loop() {
if (Serial.available()) {
String input = Serial.readStringUntil('\n'); // Read until newline
Serial.print("You entered: ");
Serial.println(input);
}
}
The terminator character is discarded from the serial buffer. If the terminator character can’t be found, all read characters will be discarded.