1

Here is my code. The incoming string is "*CRB12344,Temp25,Humidity55,CC5#"

I am unable to get value of Temp which is 25 and Humidity which is 55 and CC which 5. It only display the value of CR i.e B12344 (in this case). May be issue with strtok function?

String inputString = "";boolean stringComplete = false, StartCapturing = false;char inData[50];char inChar, IncomingWeight;char IncomingString[50];const int httpPort = 80;char *i, *k,*p;String CardID;int Temprature, Humidity, TC, CC;byte SendData = 0;void setup(){  Serial.begin(9600);}void loop(){  serialEvent();  if (stringComplete)  {    inputString.toCharArray(IncomingString, 50);    Serial.println();    Serial.println(IncomingString);    i = strstr(IncomingString, "CR");    {      if (i)      {        p = i+2;        CardID = strtok(p, ",");        Serial.print("Card Number :");        Serial.print(CardID);        Serial.println();      }    }    i = strstr(IncomingString, "Temp");    {      if (i)      {        Temprature = strtoul(i + 4 , NULL, 10);        Serial.print("Temprature :");        Serial.print(Temprature);        Serial.println();      }    }    i = strstr(IncomingString, "Humidity");    {      if (i)      {        Humidity = strtoul(i + 8 , NULL, 10);        Serial.print("Humidity :");        Serial.print(Humidity);        Serial.println();      }    }    i = strstr(IncomingString, "TC");    {      if (i)      {        TC = strtoul(i + 2 , NULL, 10);        Serial.print("Total Count :");        Serial.print(TC);        Serial.println();      }    }    i = strstr(IncomingString, "CC");    {      if (i)      {        CC = strtoul(i + 2 , NULL, 10);        Serial.print("Current Count :");        Serial.print(CC);        Serial.println();      }    }    stringComplete = false;    inputString = "";    delay(10);  }
James Waldby - jwpat7's user avatar
James Waldby - jwpat7
8,9203 gold badges21 silver badges34 bronze badges
askedNov 12, 2016 at 10:56
MICRO's user avatar

1 Answer1

3

strtok() is a destructive function. It replaces the token with an end-of-string marker (\0). Thus once used the original string is irevocably lost.

Your first use ofstrtok() changes the string from:

*CRB12344,Temp25,Humidity55,CC5#\0

to:

*CRB12344\0Temp25,Humidity55,CC5#\0

Thus any future references toInputString only see up to the end-of-string marker:

*CRB12344\0

Instead you should usestrtok() in awhile loop to slice the string into tokens and examine what the token is:

char *tok = strtok(IncomingString, ",");while (tok) {    if (strncmp(tok, "*CR", 3) == 0) { // tok is *CRB1234        // ... whatever ...    } else if (strncmp(tok, "Temp", 4) == 0) { // tok is Temp25        // ... whatever ...    } else if (strncmp(tok, "Humidity", 8) == 0) { // tok is Humidity55        // ... whatever ...    } else if (strncmp(tok, "CC", 2) == 0) { // tok is CC5#        // ... whatever ...     }    tok = strtok(NULL, ","); // Note: NULL, not IncomingString}
answeredNov 12, 2016 at 11:35
Majenko's user avatar
1
  • Didn't know this. I understand the mistake now.CommentedNov 12, 2016 at 11:57

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.