Serial.readBytesUntil() reads characters from the serial buffer into an array. The function terminates (checks being done in this order) if one of the follwing condition is met:
- The specified length has been read.
- 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.readBytesUntil() returns the number of characters read into the buffer. A 0 means that the length parameter ≤ 0, a time out occurred before any other input, or a termination character was found before any other input.
Serial.readBytesUntil() inherits from the Stream utility class.
Serial.readBytesUntil(character, buffer, length)
- Serial: serial port object. See the list of available serial ports for each board on the Serial main page.
- character: the character to search for. Allowed data types: char.
- buffer: the buffer to store the bytes in. Allowed data types: array of char or byte.
- length: the number of bytes to read. Allowed data types: int.
- Data type: size_t.
- Select Newline at the ending selection of Serial Monitor
- Type "HELLO" on Serial Monitor
- Click Send button:
- The result on Serial Monitor:
※ NOTES AND WARNINGS:
- The delimiter character is discarded from the serial buffer, unless the number of characters read and copied into the buffer equals length.
- 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)