typedef struct
{
char cmd[4];
unsigned char values[10];
unsigned char numValues;
} parsedCommand;
parsedCommand parseCommand(unsigned char* input)
{
parsedCommand data;
data.numValues = 0;
const char* delimiter = " ";
char* token = strtok(input, delimiter);
strncpy(data.cmd, token, sizeof(data.cmd) - 1);
data.cmd[sizeof(data.cmd) - 1] = '\0';
while (token != NULL && data.numValues < sizeof(data.values))
{
token = strtok(NULL, delimiter);
if (token != NULL)
{
data.values[data.numValues] = atoi(token);
data.numValues++;
}
}
return data;
}
My input is guaranteed to be null terminated. It is basically the buffer in RAM for a string of characters received over UART. I have an already established function that takes each character and appends to my buffer (input in the context of the function above). After the data is received, it finishes by appending '\0' to the buffer in RAM.