This writeup compiles the Serial.readBytesUntil() function in depth. We will discuss what it is, how it works, and cover Arduino code that explains how to use it in Arduino code.
Serial.readBytesUntil()
The Serial.readBytesUntil() function is a part of the Arduino Serial class. This function reads data from a stream until it finds a specific character or a maximum number of bytes. Once the character or maximum number of bytes is found, the function stops reading and returns the data it has read.
The Serial.readBytesUntil() function terminates whenever following conditions are met:
- When function detect a terminator character
- The defined buffer length is reached
- The set time is passed or Times out
This function doesn’t return the terminator character it only returns data up to the last character before the terminator. When 0 is returned by this function it means no valid data is found.
Syntax
The Serial.readBytesUntil() function has the following syntax:
Parameters
Following are the parameters of the function Serial.readBytesUntil():
- Terminator Character: The character that the function will stop reading at.
- buffer: In buffer the read serial data is The allowed data type is an array of char or byte.
- length: The maximum number of bytes to read. The allowed data type is int.
Return
The number of bytes placed in the buffer.
Notes: In return data the terminator character is discarded by the function Serial.readBytesUntil() from the data stream.
Example Arduino Code
Following Arduino code explain use of Serial.readBytesUntil() function:
Serial.begin(9600); // Initialize the serial connection with a baud rate of 9600
}
void loop() {
byte buffer[5]; // Define a byte array to store the incoming data
int numBytes = Serial.readBytesUntil('\n', buffer, 5); // Read incoming data from the serial connection until a newline character is encountered
if (numBytes > 0) { // Check if any bytes were read
Serial.print("Received Character: ");
Serial.write(buffer, numBytes); // Print the received data to the serial monitor
Serial.println();
}
}
In the setup() function baud rate will establish serial communication.
In the loop() function, we first define a byte array called ‘buffer’. This array will be used to store the incoming data read from the serial connection.
Next, we call the Serial.readBytesUntil() function, which reads incoming data from the serial connection until it encounters a newline character (‘\n’). Next argument is the length of the buffer which will take a maximum of 5 bytes of data at a time.
The returned byte is stored in the ‘numBytes’ variable. In case if the received data is greater than the buffer length the data will be terminated and will be sent in the next data stream.
Output
Conclusion
The Serial.readBytesUntil() function in Arduino programming reads bytes from a serial input stream until a specified terminator character is received. The function gives total bytes read and stores them in a provided buffer. Read the article to get more information related to Serial.readBytesUntil() Function.