Any concerns? support@freenove.com
Chapter 2 Serial Communication
The following is the program code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
String inputString = ""; //a String to hold incoming data
bool stringComplete = false; // whether the string is complete
void setup() {
Serial.begin(115200);
Serial.println(String("\nESP32S3 initialization completed! \r\n")
+ String("Please input some characters,\r\n")
+ String("select \"Newline\" below and Ctrl + Enter to send message to
ESP32S3. \r\n"));
}
void loop() {
if (Serial.available()) { // judge whether data has been received
char inChar = Serial.read(); // read one character
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
if (stringComplete) {
Serial.printf("inputString: %s \n", inputString);
inputString = "";
stringComplete = false;
}
}
In loop(), determine whether the serial port has data, if so, read and save the data, and if the newline
character is read, print out all the data that has been read.
Reference
Constructs an instance of the String class.
For more information, please visit
https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/
Get the number of bytes (characters) available for reading from the serial port. This is data that’s already
arrived and stored in the serial receive buffer.
Reads incoming serial data.