Arduino substring not working

I have a static method that searches (and returns) in String msg the value between TAG

this is the code function:

static String genericCutterMessage(String TAG, String msg){
    Serial.print("a-----");
    Serial.println(msg);
    Serial.print("b-----");
    Serial.println(TAG);
    if(msg.indexOf(TAG) >= 0){
        Serial.print("msg ");
        Serial.println(msg);
        int startTx = msg.indexOf(TAG)+3;
        int endTx = msg.indexOf(TAG,startTx)-2;
        Serial.print("startTx ");
        Serial.println(startTx);
        Serial.print("endTx ");
        Serial.println(endTx);
        String newMsg = msg.substring(startTx,endTx);
        Serial.print("d-----");
        Serial.println(newMsg);
        Serial.println("END");
        Serial.println(newMsg.length());
        return newMsg;
    } else {
        Serial.println("d-----TAG NOT FOUND");
        return "";
    }
}

      

and this is output

a-----[HS][TS]5132[/TS][TO]5000[/TO][/HS]
b-----HS
msg [HS][TS]5132[/TS][TO]5000[/TO][/HS]
startTx 4
endTx 30
d-----
END
0
fake -_-'....go on!  <-- print out of genericCutterMessage

      

In this case, I want to return the string between the HS tags, so my expected result is

[TS]5132[/TS][TO]5000[/TO]

      

but i dont know why i am getting void string.

to understand how the substring works, I just followed the tutorial on the official Arduino site

http://www.arduino.cc/en/Tutorial/StringSubstring

I'm not an expert in C ++ and Arduino, but it looks like a flush or buffering problem, doesn't it?

Any idea?

+3


source to share


1 answer


Your code is correct, it shouldn't be. This forces you to consider unexpected ways that it can lead to failure. There is actually only one candidate failure I can think of, your Arduino is running out of RAM. It has very little, for example, Uno has only 2 kilobytes. It doesn't take many lines to fill in this.

It is not communicated in a fluid manner. All I can do is point you to the page. Citation:



If you run out of SRAM, your program may crash unexpectedly; it will load successfully, but not running or running strangely. To check if this is happening, you can try commenting out or shortening lines or other data structures in your sketch (without changing your code). If it works well, you will probably run out of SRAM. There are several things you can do to fix this problem:

  • If your sketch speaks of a program running on a computer (desktop / laptop), you can try to transfer data or computation to the computer, reducing the load on the Arduino.
  • If you have lookup tables or other large arrays, use the smallest data type you need to store the values ​​you need; for example, an int takes two bytes, whereas a byte only uses one (but can store a smaller range of values).
  • If you do not need to modify strings or data while the sketch is running, you can store it in (program) memory instead of SRAM; use the PROGMEM keyword for this.

This is not very useful in your particular case, you will have to look at the rest of the candidate program. Or upgrade your hardware, StackExchange has a dedicated site for Arduino enthusiasts, by far the best place to get advice.

+2


source







All Articles