Hi, I wanted to split string accordingly before sending. I am learning about substring from this video Arduino Tutorial # 4.5: Better Serial Parsing - YouTube . I wanted to split string +IPD,0,2:#1.1.1s by just extracting #1.1.1s\n, so based on the video, my part1= com.substring(0, com.indexOf("#")); while part 2 = com.substring(com.indexOf("#"), com.indexOf(" "));
Here's my code:
#define esp8266 Serial3
String command;
long int previousMillis=0, currentMillis;
I printed out part2 and it printed out # only. I thought should be #1.1.1s since the video tutorial mention that com.substring(from, up to) ?
Really frustrated. Please help
Stop investing time using the String class - you'll get frustration later on with possibly memory issues
if your buffer holds char cmd[] = "+IPD,0,2:#1.1.1s\n" and you want the final part then the pointer to the string you are interested in is just (cmd+10) if the format is fixed
const char msg[] = "+IPD,0,2:#1.1.1s\n";
void setup()
{
Serial.begin(115200);
Serial.println(msg);
Serial.println(msg + 10); // if format is really known, start at position 10
Serial.println(msg + (strlen(msg) - 7)); // to print the last 7 chars
Serial.println(strchr(msg, '#')+1); // to print from 1st char after the #
}
void loop() {}
well build the cmd buffer by reading from your secondary Serial until your read the \n
for that read the Serial Input Basics link from previous answer - it also shows how to parse for content
The Serial object also knows how to perform a few basic stuff, like find, findUntil, readBytesUntil etc.... but it's a great exercise to implement for yourself
if I were you, I'd read the Serial input and drop the chars until I get the '#', then read and save the incoming chars one after the other in the cmd buffer until I get the '\n'.
--> live parsing as things come in
really read the Serial Input Basics link, it will all become super clear
My problem is solved. Thank you so much for taking some time to reply my post and introduce me many useful references. I truly appreciate it. I thank you from the bottom of my heart and God bless you