String help please

Guys - this is killing me. the amount of time I have spent of this is insane!!! please tell me what I am missing. I want to send commands via serial in the format “!541gg” and be able to trigger a software serial event based on it being a “!5” message, option “41” and now send software serial “test” and “gg”…

I cannot attach all my code right now as I am still working on it, but what I am attaching has the same issue. The “!5” filter does not work, and as soon as index = 6 all the options/if statements in the networkmessage1 & 2 get run???

Please let me know what I am doing wrong…

Thanks

/*
#include <SoftwareSerial.h>

SoftwareSerial mySerial1(10, 11); // RX, TX

String stringOne;
char inData[6];
char inChar;
byte index =0;

void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }




  // set the data rate for the SoftwareSerial port
  mySerial1.begin(9600);

}

void loop() // run over and over
{
   if (Serial.available() > 0) 
   {

     checkApplicationMessage();

 }
}

void checkApplicationMessage()
{

       if (index < 6)
       {
         inChar = Serial.read();
         if (inChar == '!'){
           index = 0;
         }
         
         inData[index] = inChar;
         index++;
         inData[index] = '\0';
       }
       else
       {
         index = 0;
       }

    ProcessApplicationMessage();       

  }    
 
void ProcessApplicationMessage()
{

    if (index == 6)
     {


              if((String(inData)).substring(0,2) == "!4")
              {   
              NetworkMessage1();
              }

       
              if((String(inData)).substring(0,2) == "!5")
              {   
              NetworkMessage2();
              }
          index = 0;
          

}
}

void NetworkMessage1()
{


            
            if((String(inData)).substring(2,4) == "01") {mySerial1.print("test1_1"); mySerial1.print((String(inData)).substring(4,6));}
            if((String(inData)).substring(2,4) == "02") {mySerial1.print("test2_1"); mySerial1.print((String(inData)).substring(4,6));}
            if((String(inData)).substring(2,4) == "03") {mySerial1.print("test3_1"); mySerial1.print((String(inData)).substring(4,6));}
         

} 

void NetworkMessage2()
{


            
            if((String(inData)).substring(2,4) == "01") {mySerial1.print("test1_2"); mySerial1.print((String(inData)).substring(4,6));}
            if((String(inData)).substring(2,4) == "02") {mySerial1.print("test2_2"); mySerial1.print((String(inData)).substring(4,6));}
            if((String(inData)).substring(2,4) == "03") {mySerial1.print("test3_2"); mySerial1.print((String(inData)).substring(4,6));}
         

}

Perhaps you could use "indexof" similar to the below to identify character strings in the captured String.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

//A very simple example of sending a string of characters 
//from the serial monitor, capturing the individual 
//characters into a String, then evaluating the contents 
//of the String to possibly perform an action (on/off board LED).

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if(readString.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}
char inData[6];

// then later in your code
       if (index < 6)
       {
         inChar = Serial.read();
         if (inChar == '!'){
           index = 0;
         }

// if index == 5, this will overflow that array with the zero terminator.         
         inData[index] = inChar;
         index++;
         inData[index] = '\0';
       }

Make the inData array size at least 7.

Make the inData array size at least 7.

And ditch the String class. Nothing that the String class does is at all difficult to do with the NULL terminated array of chars you already have.

              if((String(inData)).substring(0,2) == "!4")

could be:

if(strncmp(inData, "!4", 2) == 0)
            if((String(inData)).substring(2,4) == "01")

could be:

if(strncmp(inData[2], "01", 2) == 0)
if(strncmp(&inData[2], "01", 2) == 0)

surely?

surely?

Well, yes.

Guys - that worked perfect! Thanks. My only issue is this:

            if(strncmp(&inData[2],"A6",2)==0) {mySerial1.print("part1-"); mySerial1.println((String(inData)).substring(4,7));}

If i send a message for example like "!5A620" I would expect the output via the myserial1 to be "part1-20". The issue is that all I get is "part1-". what am I missing?

Thanks,

what am I missing?

The fact that you were told to ditch the String class. STOP USING IT! Or stop complaining about it.

beginit: what am I missing?

You've got a false assumption somewhere.

It took me thirty seconds to confirm that (String("!5A620")).substring(4,7) returns a String containing "20", and you should have been able to prove that for yourself. If you don't see that output then perhaps the stream you're printing it to is the wrong one, or your input string does not contain what you thought it did, or the piece of code you're looking at is not the one that produces the output you're getting.

PaulS:

what am I missing?

The fact that you were told to ditch the String class. STOP USING IT! Or stop complaining about it.

Meltdown alert! 8)

I am sorry - I did not mean to seam like I was ignoring the advice - I very much appreciate and value all the input. The problem I have is that my knowledge is limited and the only way I know how to "print" something is as a string. I also am not sure how to pull and join only two characters stored in an array. I did the below and it does work, but I am sure there is a better/correct way to do this. Thanks again for all the help :)

if(strncmp(&inData[2],"A6",2)==0) {mySerial1.print("part1- "); mySerial1.print(String(inData[4]));mySerial1.println(String(inData[5]));}

The String functions are fairly easy to understand and use.

http://arduino.cc/en/Reference/StringObject

The String functions are fairly easy to understand and use.

..but please don't bother, and learn to use the methods more appropriate to a memory-constrained environment.