Extracting from a char substring

Hi all,

I have a set of data coming in on my serial monitor. I parse this into a data packet that could look like any of the following:







Essentially, I have up to 4 pieces of information, that will be preceded by a letter, and followed by a comma.

I have written the following code to identify whether S is present, and to find the position of 'S' and the position of ',' in the string... I now want to chop out the number but and struggling to find the right operator...

How should this be done?

  char * strtokIndx;

  strtokIndx = strchr(tempChars, 'S');
  int SPEED1 = strtokIndx - tempChars + 1;
  if (SPEED1 > 0) {
   strtokIndx = strchr(tempChars, ',');
  int SPEED2 = strtokIndx - tempChars + 1;
    Serial.print(", ");

Use strtok() function to split your string into tokens at the “,” delimiters.

Then check each token starts with an “S” (or what ever letter you are looking for, R, T, F) and use atoi() to convert that to an integer.

char str[] ="S123,R1654,T334,F1343,";
char* token = strtok(str, ",");
int speed = 0;
while (token != NULL)
  if (token[0] == 'S')
    speed = atoi(&token[1]);
  token = strtok(NULL, ",");