String vs string vs char + Arrays? Correct approach?

*not sure if I should make a new thread or not?

anyways..

ok.. so I have something 'close' (I think).. LOL..

The problem is when I try to use the 'sub-parsing' function called in the 'while loop'.

If I comment out the parseAction() call in the while() loop... it executes, and delivers what I expect in the serial monitor.

*but that just a simple break down of sections by comma delimited character.. (havent stored them anywhere where they can be passed around and further parsed/broken down..etc).

if I un-comment that line.. and let it call the parseAction() function..

it executes ONE time perfectly.... then nothing else.

I get a DEBUG line in the (after the) WHILE loop saying that parsing of the comma delimited cunks is complete.. (when really only the FIRST one was parsed and attempted to be passed to the parseAction() function.)

Here is my code..

  • paste this into the serial monitor:
    <b=1:1,b=2:1,m=6:200> and comment/un-comment line 72 for the parseAction(actionPacket); behavior results.
#define SOP '<'
#define EOP '>'
bool hasStarted = false;
bool hasEnded = false;

char incomingSerialData[100];
byte index;

char *actionPacket;
byte packetCounter;

int positionValue = 0;
int amountValue = 0;

void setup() {
  //serial monitor output
  Serial.begin(9600);
}


//example recipie format  (paste into serial monitor for output example)
//<b=1:1,b=2:1,m=6:200> = bottle#1/1 shot, bottle#2/1 shot, mixer#6/200ms



void loop(){
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0){
    char incomingCharacter = Serial.read();
    //Serial.println(incomingCharacter);
    if(incomingCharacter == SOP){
      index = 0;
      incomingSerialData[index] = '\0';
      hasStarted = true;
      hasEnded = false;
    }else if (incomingCharacter == EOP){
      hasEnded = true;
      break;
    }else{
      if (index < 100) {
        incomingSerialData[index] = incomingCharacter;
        index++;
        incomingSerialData[index] = '\0';
      }
    }
  }

 // Packet data done...parse/evaluate data
  if(hasStarted && hasEnded){
      
    Serial.println("");
    Serial.print("TOTAL COMMAND PACKET (CP) CHECK: ");
    Serial.println(incomingSerialData);
    Serial.println("");

    //packet counter reset
    packetCounter = 0;

    //get first delimited packets from command string
    actionPacket = strtok(incomingSerialData, ","); //split action packets from command string

    //keep parsing these delimited packets from the command string until there are none left to parse
    while(actionPacket != NULL){
      packetCounter++;
      Serial.println("-----------------------------------");
      Serial.print("PARSED ACTION PACKET (AP) ");
      Serial.print(packetCounter);
      Serial.print(": ");
      Serial.println(actionPacket);    
      
      //send off to parse function/loop for processing      
      parseAction(actionPacket); //problem stems from here!!!!

      //update parsing params to next comma delimited chunk/action packet
      //only works if we dont call the 'sub parsing function' and pass in the pointer/value a a param
      actionPacket = strtok(NULL, ":");       
    }

    //parsing complete
    Serial.println("");
    Serial.print("PARSING PACKETS COMPLETE");
         
    // Reset for the next packet
    hasStarted = false;
    hasEnded = false;
    index = 0;
    incomingSerialData[index] = '\0';
    
  }

}



void parseAction(char *packetData) {
  Serial.println("");
  Serial.print("TOTAL ACTION PACKET (AP) RECEIVED CHECK: ");
  Serial.println(packetData);
  char *actionToken = strtok(packetData, "="); //split action value from location/amount values
  if(actionToken){
    Serial.print("ACTION COMMAND: ");
    Serial.println(actionToken);  
        
    //check if bottle 'action' data coming over
    if(strcmp(actionToken, "b") == 0){      
      //grab position value    
      char *positionToken = strtok(NULL, ":");
      if(positionToken){
        Serial.print("BOTTLE #: ");
        Serial.println(positionToken);
        positionValue = atoi(positionToken);

        //now grab 'amount' value
        char *amountToken = strtok(NULL, "\0");
        //char *amountToken = strtok(NULL, ",");
        if(amountToken){
          Serial.print("SHOT COUNT: ");
          Serial.println(amountToken);
          amountValue = atoi(amountToken);
        }
      }
    }

    //check if mixer action data coming over
    if(strcmp(actionToken, "m") == 0){
      char *positionToken = strtok(NULL, ":");
      if(positionToken){
        Serial.print("VALVE #: ");
        Serial.println(positionToken);
        positionValue = atoi(positionToken);
        //now grab 'amount' value
        char *amountToken = strtok(NULL, "\0");
        //char *amountToken = strtok(NULL, ",");
        if(amountToken){
          Serial.print("OPEN TIME: ");
          Serial.println(amountToken);
          amountValue = atoi(amountToken);
        }
      }
    }
  }

   //update parsing params
   //actionPacket = strtok(NULL, ",");
}

I feel like something in the parseAction() function is messing up the 'pointer' for the main parent while loop to think it is at the end of the comma delimiters/char array text.

How can I fix that?