0

I am trying to split an incoming string into 2 integer values since I have to control 2 dc motors with it. The processor is node MCU and I get following error:-

Com Port Reading:-

WiFi connected
Received: "84+62"
"84+62"

Soft WDT reset

>>>stack>>>

ctx: cont
sp: 3ffffd90 end: 3fffffc0 offset: 01b0
3fffff40:  2b343822 00223236 3ffee70c 40204148  
3fffff50:  3ffe85ee 3ffee6d8 3ffee70c 402043c5  
3fffff60:  3ffe85dc 3ffee70c 3ffee6d8 402043c5  
3fffff70:  3ffe85f0 3ffee69c 00000000 00000001  
3fffff80:  00000000 40200038 3ffefca4 0000000f  
3fffff90:  00000007 00000000 00000001 402048f9  
3fffffa0:  3fffdad0 00000000 3ffee744 40204984  
3fffffb0:  feefeffe feefeffe 3ffe851c 401009a5  
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v951aeffa
~ld

Code:-

if (msg.length() > 0) {
        Serial.print("Received: "); Serial.println(msg);
        char charBuf[50];
        msg.toCharArray(charBuf, 50);
        Serial.println(charBuf);

        char left[3] = {};
        char right[3] = {};
        int n = 0;

        while (charBuf != 0) {
          for (int i = 0; i < strlen(charBuf); i ++) {
            if (charBuf[i] != '+') {
              left[n] += charBuf[i];
              n++;
            }
            else {
              n = 0;
              break;
            }
            right[n] += charBuf[i];
            n++;
          }
        }
        Serial.println("Left Motor: "); Serial.print(left);
        Serial.println("Right Motor: "); Serial.print(right);

    }

My desired result is following:-

Left Motor: 84
Right Motor: 62

So what changes I need to make?

  • 1
    your code loops in while (charBuf != 0) { until watchdog resets the MCU – Juraj Mar 22 '19 at 9:49
  • Use strtok() to split your string. – Majenko Mar 22 '19 at 10:02
  • can you show an example using strtok() – suu Mar 22 '19 at 10:28
0

Here's an example sketch that uses strtok().

char charBuf[] = "\"84+62\"";

// Set array size to maximum number of integers you will receive.
uint16_t myArray[2];

void setup(){

  Serial.begin(9600);

  int counter = 0;
  char* pch = strtok(charBuf, "\"+");

  while(pch != NULL){
    myArray[counter] = atoi(pch);
    pch = strtok(NULL, "\"+");
    counter += 1;
  }

  // Print out the contents of the integer array.
  for(int i = 0; i < counter; i++){
    Serial.println(myArray[i]);
  }
}

void loop(){}

myArray[0] contains 84 and myArray[1] contains 62.

| improve this answer | |
0

You get this memory leak because you overflow the variable right. This does not happen in the for loop itself even though this could be implemented more straight forward. But it's the while loop that doesn't come to an end (charBuf doesn't change and will not become 0) and in the second iteration n will be 1 initially (due to n++ at the end of the for loop). That causes the overflow at right[n] += charBuf[i];.

So: remove the while loop (and use strtok()).

| improve this answer | |
0

"84+62"

The additional doublequote characters are part of your string and charBuf

    Serial.println(charBuf);  // Text with " and  +

    char* cpl = nullptr;
    char* cpr = nullptr; 
    for (char* cp = charBuf; *cp != 0; cp++) {
        if (*cp != '"' && ! cpl )  cpl = cp;
        if (*cp == '"') *cp = 0;           
        if (*cp == '+' ) {
          *cp = 0; // split
          cpr = cp+1;  
        }
    }
    Serial.println("Left Motor: "); Serial.println(atoi(cpl));
    Serial.println("Right Motor: "); Serial.print(atoi(cpr));
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.