Introduction: Arduino Function Serial.read() and Serial.readString()

Serial monitor of Arduino is a very useful feature.Serial monitor is used to see receive data, send data,print data and so on.Serial monitor is connected to the Arduino through serial communication. This serial communication occurs using RX (pin 0) and TX (pin 1) terminal of Arduino. Any kind of data can send through this serial monitor. To read incoming data in Arduino "Serial.reads ()" And "Serial. readString ()" are two very useful functions. This to function reads the data which are come to Arduino serial port. But there is a little difference between the two functions."Serial. reads ()" function reads the data in bytes.it mean if you write a command "int a=Serial.read();",then data store in "a" in bytes."Serial.readString()" read the serial data in string.It mean for "String a=Serial.readString();" command "a" store string.In the picture there are two photos one is ardino IDE serial monitor, another is proteus virtual terminal.

Step 1: Serial.read()

Serial Function

read() use to reads incoming serial data. read().

This has no parameter.

Returns

The first byte of incoming serial data available (or -1 if no data is available) - int

Syntax
Serial.read()

For more information

Link: https://www.arduino.cc/en/Serial/Read

Step 2: Serial.read() Example

Code:

int incomingByte = 0;

void setup() {

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

}

void loop() {

if (Serial.available() > 0) {

incomingByte = Serial.read(); // read the incoming byte:

Serial.print(" I received:");

Serial.println(incomingByte);

}

}

This is an example code of "Serial.reads ()" function.I send data from serial monitor and that data read Serial.read () function. As I said receive data by using Serial.reads () function is in bytes. So an integer variable is declared. To understand what is happeningin Serial.read () function Serial.println () function. But if we use Serial.writes () function then we can see which character is pressed in serial monitor.

Youtube

Step 3: Serial.readString()

Serial Function

readString() reads characters from the serial data into a string.

This has no parameter.

Returns
String that read from the serial.

Syntax
Serial.readString()

For more information

Link:https://www.arduino.cc/en/Serial/ReadString

Step 4: Serial.readString() Example

Code:

String a;

void setup() {

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

}

void loop() {

while(Serial.available()) {

a= Serial.readString();// read the incoming data as string

Serial.println(a);

}

}

"Serial. readString ()" function read serial data as a string. So, when some data are given in serial, Serial.readString () function read as string. In the picture uses a virtual terminal as serial monitor. I wrote "My name is Mamun" after some time it gives return "My name is Mamun". From here it is clearly understandable how does the "Serial.readString()" function work.

Youtube