Arduino Serial readBytes and Read Bytes Until Functions, Example of Serial.readBytes() and Serial.readBytesUntil()

Arduino Serial Read Bytes Function – Serial.readBytes()

  • Arduino Serial.readBytes() Function reads the multiple bytes from the received buffer into a character array (also called buffer).
  • The readBytes function will read the specified number of bytes in the specified variable from serial buffer.
  • The function return the integer value, which specify how many bytes successes-fully read in the variable.
  • The readBytes() function wait for timeout time, by default it is one second.
  • The readBytes() function will complete by two events, either it will read the number of bytes specified or by timeout time completes.

The most popular Serial.find() function is having an issue, that it will clear the serial receive buffer after the test. If you want to test multiple string in the same received data than you have to read first in the variable, so that you can test as many times as you want.

Arduino readBytes

Syntax of Serial.readBytes function

char readData[10];//The character array is used as buffer to read into.
int x = Serial.readBytes(readData,10);//10 is the length of data to read.

The function require two things, variable name to read into, number of bytes to read.

The variable x will hold the bytes read and readData will hold the string received.

Caution: The Serial.readBytes function return the number of bytes read in the buffer variable. In the above code syntax integer x will hold the number of valid character in the readData buffer.

The Serial.readBytes() function is mostly used to test some target word or string is received or not.

How Serial.readBytes() Function Works

The function continuously read the received data from the buffer and if target number of character is received it will return with the number of character. And if the number of character it read is less than specified number and timeout time is completes than it will return the number of characters, which is less than the target length.

And if the target number or length is not received until one second ( timeout ) than it will return with less number of characters.

Caution: The default wait time of timeout is one second, which can be modified with the help of function Serial.setTimeout().

Arduino readBytes Example

In this small project code we will send some data from serial monitor and return back the same data with number of bytes it read.

Example 1 – Serial.readBytes() function code to loop-back from PC (serial monitor).

/*
    Serial.readBytes() test program.
	Author: Nilesh Chaurasia
    https://elextutorial.com
*/
void setup() 
{
	Serial.begin(9600); // Set the baud rate to 9600
	
}
void loop() 
{
	char readData[10]="0000000000";//The character array is used as buffer to read into.
	int x = Serial.readBytes(readData,10);//It require two things, variable name to read into, number of bytes to read.
	Serial.println(x);//display number of character received in readData variable.
	Serial.println(readData);//send back the 10 bytes of data.
	delay(500);
}   
				 

In the example below we have send three bytes “123” first and than we have sent 6 characters “123456”. Otherwise it will show “0” numbers and same previous data.

Note: if you send less than 10 character it will show some previous data in the buffer variable also.

Arduino Example for Serial read bytes function Serial.readBytes()
Fig.1 – Arduino Example for Serial read bytes function.

Arduino Serial Read Bytes Until Function – readBytesUntil

  • Arduino Serial readBytesUntil Function reads the received buffer until it receive a terminating character.
  • If the specified number of character is received in the buffer than the function return or complete.
  • By default the readBytesUntil function wait for terminating character or number of character or timeout to complete.
  • The Serial.readBytesUntil() function wait for one second or timeout time.
  • You can consider the terminating character as command end symbol, like in c language compiler look for “;” for end of statement.

The concept of terminating character in readBytesUntil solve the problem of delay in case the number of character in the command is less than the specified number of characters. As soon as the function receive terminating character it will return with a number, depends upon how many character received before terminating character.

Syntax of Serial.readBytesUntil function

char readData[10];//The character array is used as buffer to read into.
int x = Serial.readBytesUntil(“\n”,readData,10);//10 is the length of data to read.

The “\n” is a newline character, used as terminating character in this function.

How readBytesUntil Function Works

The function continuously read the received data and store in the variable provided.

The function can exit by three events-

  • The timeout time completes, by default the timeout time is one second.
  • Before the timeout time, the function received the specified number of characters ( 10 char ).
  • Before above two events, the function received terminating character ( “\n” newline character ).
  • In all the three exit case the Serial.readBytesUntil() function return the number of character read in the buffer variable.

Caution: The default wait time is one second, which can be modified with the help of function Serial.setTimeout().

Arduino Serial readBytesUntil Function Example

In this small project code we will send some data from serial monitor ( with set line ending character as Newline ) and return back the same data with number of bytes it read.

Example 1 – Serial.readBytes() function code to loop-back from PC (serial monitor).

/*
    Serial.readBytes() test program.
	Author: Nilesh Chaurasia
    https://elextutorial.com
*/
void setup() 
{
	Serial.begin(9600); // Set the baud rate to 9600
	
}
void loop() 
{
	char readData[10]="0000000000";//The character array is used as buffer to read into.
	int x = Serial.readBytesUntil("\n",readData,10);//It require two things, variable name to read into, number of bytes to read.
	Serial.println(x);//display number of character received in readData variable.
	Serial.println(readData);//send back the 10 bytes of data.
	delay(500);
}   
				 

In the example below we have send three bytes “abc” first and than we have sent 6 characters “123456”. Otherwise it will show “0” numbers and same previous data.

The function executes after every 500 ms.

Arduino Serial Read Bytes Until readBytesUntil() Function Example
Fig.1 – Arduino Serial Read Bytes Until Example.
You can leave a response, or trackback from your own site.

3 Responses to “Arduino Serial readBytes and Read Bytes Until Functions, Example of Serial.readBytes() and Serial.readBytesUntil()”

  1. I enjoy you because of all your valuable efforts on this website. Betty really likes participating in research and it’s obvious why. My spouse and i know all about the compelling form you give priceless techniques through this web site and encourage contribution from others on this point plus my child is truly learning a lot of things. Have fun with the rest of the year. You’re the one conducting a fantastic job.

  2. Mack Davidek says:

    Perfect piece of work you have done, this website is really cool with superb information.

  3. Hairstyles says:

    Greetings from California! I’m bored at work so I decided to check out your blog on my iphone during lunch break. I enjoy the information you present here and can’t wait to take a look when I get home. I’m shocked at how fast your blog loaded on my phone .. I’m not even using WIFI, just 3G .. Anyhow, superb blog!

Leave a Reply to the article



Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape