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

Comments

author
russ_hensel (author)2015-09-13

The price of Proteus seems a bit steep for diy projects. Do you know of a free, legal version?

author

hi, i am using a free version of "PROTEUS 7.2 sp2".You can download this link https://docs.google.com/folderview?id=0B6bMc7UD3os... hope this will help you.I don't know is this legal or not.Also there are other links from where you can download Proteus.And in PROTEUS 7.2 sp2 you need to add arduino, all information are available in the link

Link: https://www.instructables.com/id/How-to-Simulate-Ar...

Thanks.

author

That is definitely NOT legal and also when you include a crack like you do. The software costs about 600 dollars. You don't use a free version, you are using a cracked version. Be careful, it can contain virusses, trojans and other not to want software. Be warned.

author
rohank26 (author)2016-06-17

in serial.read it means it returns the first byte of incoming data.What about the second byte will it be returned or not?My second query is what is the difference between incoming data and stored data in the serial buffer.

author
BhadaniJ (author)2016-05-26

maja na avi

author
BhadaniJ (author)2016-05-26

bov mast

author
BhadaniJ (author)2016-05-26

hi

author
Fezder (author)2016-02-16

Thanks mate, this helped a lot!

author
EarlWallaceNYC (author)2015-12-23

Thanks for the info.
The documentation on Arduino's String class is hard-to-come-by.

About This Instructable

140,212views

27favorites

License:

More by Abdullah_Al_Mamun_EEE_EWU_Bangladesh:Serial communication process in Arduino Create Internal Interrupt In ArduinoHow to Simulate Arduino in Proteus
Add instructable to: