> dataPart = strtok(data, ",");
...
> cmd = strtok(dataPart, "=");
> cmdValue = atoi(strtok(NULL, "="));
...
> dataPart = strtok(NULL, ",");
You can't use strtok() to tokenise two strings at the same time, even if one of those strings is embedded inside the other.
For example, if the input is always well-formed, then perhaps you could do this.
1 2 3 4 5 6 7 8
|
char data[] = "v=35,rpm=80,temp=25,dist=31051999,vbat=33";
char *cmd;
cmd = strtok(data, ",=");
while (cmd != NULL) {
char *param = strtok(NULL, ",=");
// do stuff with cmd and param
cmd = strtok(NULL, ",=");
}
| |
Which is fine, so long as say "v=35=rpm=80=temp=25,dist,31051999,vbat=33" either never happens or isn't an error.
If you want alternate comma equals tokens, you could look at the POSIX function strtok_r, which does allow you to parse multiple strings at the same time.
https://linux.die.net/man/3/strtok_r
Or perhaps separate out all the C=V commands first,
1 2 3 4 5 6 7 8 9
|
char data[] = "v=35,rpm=80,temp=25,dist=31051999,vbat=33";
char *cmds[10]; // pick a sufficiently large array size
char *cmd;
char *next = data;
int nCmd = 0;
while ( nCmd < 10 && (cmd=strtok(next, ",")) != NULL ) {
cmds[nCmd++] = cmd;
next = NULL;
}
| |
then tokenise each cmds[i] with strtok(p,"=")