Serial.readString()
Description
Serial.readString() reads characters from the serial buffer into a String. The function terminates if it times out (see setTimeout()).
Serial.readString() inherits from the Stream utility class.
Syntax
Serial.readString()
Parameter Values
- Serial: serial port object. See the list of available serial ports for each board on the Serial main page.
Return Values
- A String() read from the serial buffer
Example Code
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// check if data is available
if (Serial.available() > 0) {
// read the incoming string:
String incomingString = Serial.readString();
// prints the received data
Serial.print("I received: ");
Serial.println(incomingString);
}
}
- Type "HELLO" on Serial Monitor and click Send button:
COM6
Autoscroll
Clear output
9600 baud
Newline
- The result on Serial Monitor:
COM6
I received: HELLO
Autoscroll
Clear output
9600 baud
Newline
※ NOTES AND WARNINGS:
- Serial.readString() may read a single incoming string in multiple times (resulting in multiple fragments). To read a single string at one time, use Serial.readStringUntil() with delimiter instead.
- 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)
See Also
- Language : Arduino - Serial
- Language : Serial.available()
- Language : Serial.availableForWrite()
- Language : Serial.begin()
- Language : Serial.end()
- Language : Serial.find()
- Language : Serial.findUntil()
- Language : Serial.flush()
- Language : Serial.getTimeout()
- Language : if(Serial)
- Language : Serial.parseFloat()
- Language : Serial.parseInt()
- Language : Serial.peek()
- Language : Serial.print()
- Language : Serial.println()
- Language : Serial.read()
- Language : Serial.readBytes()
- Language : Serial.readBytesUntil()
- Language : Serial.readStringUntil()
- Language : serialEvent()
- Language : Serial.setTimeout()
- Language : Serial.write()
※ ARDUINO BUY RECOMMENDATION
Arduino UNO R3 | |
Arduino Starter Kit |
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.