Serial.readStringUntil()

Description

readStringUntil() reads characters from the serial buffer into a String. The function terminates (checks being done in this order) if one of the follwing condition is met:

  • The timeout is elapsed (see Serial.setTimeout())
  • The delimiter character is detected (in which case the function returns the characters up to the last character before the supplied delimiter). The delimiter itself is not returned in the buffer.

Serial.readStringUntil() inherits from the Stream utility class.

Syntax

Serial.readStringUntil(delimiter)

Parameter Values

  • Serial: serial port object. See the list of available serial ports for each board on the Serial main page.
  • delimiter: the character to search for. Allowed data types: char.

Return Values

  • The entire String read from the serial buffer, up to the delimiter character

Example Code

void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps } void loop() { // check if data is available if (Serial.available() > 0) { // read the incoming string: String incomingString = Serial.readStringUntil('\n'); // prints the received data Serial.print("I received: "); Serial.println(incomingString); } }
  • Select Newline at the ending selection of Serial Monitor
  • Type "HELLO" on Serial Monitor
  • Click Send button:
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • The result on Serial Monitor:
COM6
Send
I received: HELLO
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTES AND WARNINGS:

  • The delimiter character is discarded from the serial buffer.
  • Serial functions are not only used for the communication between an Arduino board and Serial Monitor of Arduino IDE but also used for the communication between:
  • An Arduino board and other Arduino board
  • An Arduino board and other sensors/devices
  • An Arduino board and computer (any Serial software on computer)

See Also

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.

※ OUR MESSAGES