Using ESP32 GPIO, Serial.printf(...) prints weird characters when input is larger than 12 characters
Learning C with the Freenove WSP32-WROVER starter kit and there's an issue I cannot find an answer to. Using the provided code to input some data with UART, it would seem to have a strange behavior whenever I input string that is larger than 12 characters. Under 12, everything is fine.
/**********************************************************************
Filename : SerialRW
Description : Use UART read and write data between ESP32 and PC.
Auther : www.freenove.com
Modification: 2020年07月11日
**********************************************************************/
String inputString = ""; //a String to hold incoming data
bool stringComplete = false; // whether the string is complete
void setup() {
Serial.begin(9600);
Serial.println(String("\nESP32 initialization completed!\n")
+ String("Please input some characters,\n")
+ String("select \"Newline\" below and click send button. \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;
}
}
The output of any larger than 12 characters string is sometimes a little bit different, but always seems to follow a similar pattern. For any 13 characters long string, I get :
⸮⸮?
The ouput changes if I add characters, for example 20 characters is this :
D⸮⸮?
I assume the unprintable character is a new line.
The baud rate is set at the same value both in the code and in the serial monitor and I get the same results if I change the baud rate.
Any explanation regarding this behaviour and how to avoid it would be most welcomed.
1 Answer 1
Change:
Serial.printf("inputString: %s \n", inputString);
to:
Serial.printf("inputString: %s \n", inputString.c_str());
%s
expects a const char *
which is not what inputString
yields.
What you're seeing in the difference between strings larger and smaller than 12 characters is down to whether or not small string optimization is in use and how structures are passed in the ESP32's calling convention.
-
1Thanks a lot, this did solve the issue <3UmbrellaCorpAgent– UmbrellaCorpAgent2022年07月28日 22:03:51 +00:00Commented Jul 28, 2022 at 22:03
-
As a rule, it is better to use char array thhen string. While string might be smaller then your maximum size of array, its variable nature makes it very hard for memory allocation to guess a right size. While it is not a problem in a simple program like this when you get to larger projects where your functions are used within libraries, variable nature of string makes memory alocation within gaps in memory fragmentation too unreliable. (I have solved many crashes simply by changing strings to char arrays)Its well worth getting into a habit of char arrays rather then stringsTomas– Tomas2022年07月28日 23:27:49 +00:00Commented Jul 28, 2022 at 23:27
-
I have tried to remove "in fragmentation" unfortunately 5 minute edit rule for comment is too low for dissgraphic person"Tomas– Tomas2022年07月28日 23:35:44 +00:00Commented Jul 28, 2022 at 23:35