Hello,
I have a problem I don't understand, and would like to know if anyone has seen this before or can explain what is going on, as I'm stumped. The code below WORKS (i.e. the string rxString is parsed into tokens) if the char delim is defined within the function parseCommand(), but not if it is defined globally. I don't understand what the difference is.
Please forgive the nasty programming and structure, I'm experimenting and I thought I had everything on track until this showed up...
// Global variable declarations
String receivedTokens[20];
String commandString[6];
char rxString[] = "?e GO go 12838 -2032 0 +32.3 ? A1 500"; // text to tokenise
char* token; // pointer to token
//char delim[] = { 32 }; // Decimal 32 is ASCII 'space' - FAILS if defined here
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
void setup() {
Serial.begin(115200);
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
void parseCommands() {
// char rxString[] ="RL3]\[Test 1}|{3}|{S]\[Test 2}|{41}|{L]\[Test 3}|{1234}|{L"; // text to tokenise
//char rxString[] = "?e GO go 12838 -2032 0 +32.3 ? A1 500"; // text to tokenise
//char* token; // pointer to token
char delim[] = { 32 }; // Decimal 32 is ASCII 'space' - WORKS if defined here
int i = 0;
int validCommand;
bool match = false;
// Test some things out about String
String tokenString[20];
tokenString[0] = "Hello";
tokenString[1] = "A3";
// String receivedTokens[20];
Serial.print(tokenString[0]);
Serial.print(tokenString[1]);
Serial.println();
// Store some commands in String array
// String commandString[6];
commandString[0] = "A1";
commandString[1] = "A2";
commandString[2] = "A3";
commandString[3] = "GO";
commandString[4] = "?";
// Moving on...
Serial.print("Splitting rxString ");
Serial.print(rxString);
Serial.println(" into tokens:");
token = strtok(rxString, delim); // get first token
// token = strtok (rxString,"}]\[|{"); // get first token
receivedTokens[i] = token;
while (token != NULL) {
Serial.print("Token found: ");
Serial.println(token); // print it
token = strtok(NULL, delim); // get next token
// token = strtok (NULL, " "); // get next token
i++;
receivedTokens[i] = token;
}
// Show the tokens stored in the String array
for (i = 0; i < 20; i++) {
Serial.print(" receivedTokens[");
Serial.print(i);
Serial.print("]: ");
Serial.println(receivedTokens[i]);
}
// Match the stored tokens against the 'acceptable commands'
i = 0;
while (!match && i < 5) {
if (receivedTokens[0].equalsIgnoreCase(commandString[i])) {
Serial.println("First token matches a command!");
Serial.print(" receivedTokens[0]: ");
Serial.print(receivedTokens[0]);
Serial.println();
Serial.print(" commandString[");
Serial.print(i);
Serial.print("]: ");
Serial.print(commandString[i]);
Serial.println();
validCommand = i;
match = true;
} else {
validCommand = -1;
match = false;
}
i++;
}
if (validCommand == -1) {
Serial.println(" ! Unrecognized command.");
Serial.print("validCommand: ");
Serial.print(validCommand);
Serial.println();
} else {
Serial.print("validCommand: ");
Serial.print(validCommand);
Serial.println();
}
}
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
void loop() {
parseCommands();
while(true);
}