3

I have a static method that searches (and returns) into String msg the value between a 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 that case I want return the string between HS tag, so my expected output is

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

but I don't know why I receive a void string.

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

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

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

Any idea?

4
  • What does this print: Serial.println(newMsg.length()); ? Commented Jun 15, 2015 at 22:58
  • Please add: serial.println("END");, and double check that the word END appears at the end of the printout. Commented Jun 15, 2015 at 22:59
  • Does adding a Serial.flush() after Serial.println(newMsg); change anything? Commented Jun 16, 2015 at 0:27
  • Like you suggest, I have added serial.println("END"); and Serial.println(newMsg.length()); after substring. I put response in the main post (in brief length=0 and END put correct after d-----) Commented Jun 16, 2015 at 19:07

1 Answer 1

2
+50

Your code is correct, this should not happen. Which forces you to consider the unexpected ways that this could possibly fail. There is really only one candidate mishap I can think of, your Arduino is running out of RAM. It has very little, the Uno only has 2 kilobytes for example. It doesn't take a lot of string munching to fill that up.

This is not reported in a smooth way. All I can do is point you to the relevant company page. Quoting:

If you run out of SRAM, your program may fail in unexpected ways; it will appear to upload successfully, but not run, or run strangely. To check if this is happening, you can try commenting out or shortening the strings or other data structures in your sketch (without changing the code). If it then runs successfully, you're probably running out of SRAM. There are a few things you can do to address this problem:

  • If your sketch talks to a program running on a (desktop/laptop) computer, you can try shifting data or calculations to the computer, reducing the load on the Arduino.
  • If you have lookup tables or other large arrays, use the smallest data type necessary to store the values you need; for example, an int takes up two bytes, while a byte uses only one (but can store a smaller range of values).
  • If you don't need to modify the strings or data while your sketch is running, you can store them in flash (program) memory instead of SRAM; to do this, use the PROGMEM keyword.

That's not very helpful in your specific case, you'll have to look at the rest of the program for candidates. Or upgrade your hardware, StackExchange has a dedicated site for Arduino enthusiasts, surely the best place to get advice.

Sign up to request clarification or add additional context in comments.

1 Comment

YESSSSS!!!! Your suggestion has been very useful!...I optimized my code using pointers where it was possible and now all works well!!!...I'm a java developer, pointers(c++ in general) are not my best friends ; )! You have gained 50 points!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.