I don't want to resurrect this thread, but I have also just found it and I bet a lot of other users have also found it and I don't really like the solution of using String Message = Serial.readString(); because it's slow.
I am writing this because, and I am proof of this, a newbie will search everywhere and accept any solution from any forum, no matter how old or bad it is. If it works, it works.
If you are using an ESP32, like I am, using String is OK. But if you are using an Arduino though... yeah...String's a bitch.
The ESP32 has enough RAM to handle it. I'm also using String because it is so much more easier to work with than char. It's true that you don't have the same control over String as you do with char, but let's be honest, a newbie like me that recently learned how to read from Serial won't need over complicated syntax to precisely use a char array.
This is my solution:
#include <Arduino.h>
HardwareSerial USE_SERIAL1(1);
#define RX1 36
#define TX1 4
void setup() {
Serial.begin(115200);
delay(1000);
USE_SERIAL1.begin(9600, SERIAL_8N1, RX1, TX1);
}
String Radar_String = "";
void loop() {
if(USE_SERIAL1.available() >0) {
Serial.println("USE_SERIAL1 is available.");
while (USE_SERIAL1.available()) {
char radar = USE_SERIAL1.read();
Radar_String = radar;
Serial.print(Radar_String);
}
} // if(USE_SERIAL1.available() >0)
}
Read from serial using a char: char radar = USE_SERIAL1.read(); because String Message = Serial.readString(); is so slow that it can give me 3 values in the same while loop. And I don't want a cluster of read values. I want to read each value as it comes from the Serial.
Next you can simply use "=" operator to put the char into a String.
And lastly you will want Serial.print(Radar_String); inside the while loop because if it isn't inside it will output nothing. That's how fast char is !
Also, if String Message = Serial.readString(); is this slow on an ESP32, then on an Arduino it will be even slower.
I hope this can help somebody.