0

I am receiving some feedback on the serial port from a modem, it is six lines in total. To facilitate the decoding process, I'd like to break it into 6 distinct string variables instead of just one. Is there an easy way of doing this?

The code sending a command and then receiving a multi-line response:

void atsendbasic(String command, int fallout3, int fallout4){
   unsigned long initialtime4 = millis();
   Serial2.print(command);
   Serial2.flush();  // Wait until it is all sent
    while ((!Serial2.available() && millis() - initialtime4 <= fallout3) || millis() - initialtime4 <= fallout4) continue;
    while ((Serial2.available()&& millis() - initialtime4 <= fallout3) || millis() - initialtime4 <= fallout4) {
      char feedback = Serial2.read();
      Serial.print(feedback);
    }
}

what I am reading on the serial monitor:

15:45:17.907 -> AT+QMTPUBEX=1,0,0,0,"motorbeta/heartbeat","{"pulse":1,"secondary":2}"
15:45:17.907 -> OK
15:45:17.907 -> 
15:45:17.907 -> +QMTPUB: 1,0,0
15:45:17.907 -> 
15:45:17.907 -> +QMTRECV: 1,0,"motorbeta/shutdown","shutdown=1"

So with this approach ideally:

variable1 = AT+QMTPUBEX=1,0,0,0,"motorbeta/heartbeat","{"pulse":1,"secondary":2}"
variable2 = OK
variable3 = 
variable4 = +QMTPUB: 1,0,0
variable5 = 
variable6 = +QMTRECV: 1,0,"motorbeta/shutdown","shutdown=1"
Feynman137
  • 181
  • 2
  • 9
  • Do you really mean 6 distinct char variables, or do you mean 6 distinct string variables? (The difference being that a char variable can only hold a single char, whereas a string variable can hold any number of chars) – Jeremy Friesner Mar 21 '22 at 02:31
  • @JeremyFriesner Okay so for example this is a char string of some size? `char str[] ="15:34:09.044 -> +QMTRECV: 1,0,\"motorbeta/shutdown\",\"shutdown=1\"";` I will edit the question – Feynman137 Mar 21 '22 at 02:41
  • 1
    That's an array of chars. You're probably better off using `std::string` (or at least some string class) instead. – Jeremy Friesner Mar 21 '22 at 02:43
  • at the end of the day i am looking for a way to take the response and make each line a variable that I can easily scan through for the information I need. All of this info happens to be in the 6th line anyway. – Feynman137 Mar 21 '22 at 02:47
  • Abseil `StrSplit` will do what you want. – Taekahn Mar 21 '22 at 02:49
  • Well, if you want to do it C-style, `strtok()` will do the job just fine. If you're looking for a C++-style solution, check out here: https://stackoverflow.com/questions/236129/how-do-i-iterate-over-the-words-of-a-string – Jeremy Friesner Mar 21 '22 at 02:51

0 Answers0