Here's a snippet of a program I've written:
int8_t extractParameters(struct scpi_token* command, bool parmsRequired, uint8_t minCount, uint8_t maxCount, struct scpi_token* param1, struct scpi_token* param2, struct scpi_token* param3) { struct scpi_token* args; args = command; while(args != NULL && args->type == 0) { // Ignore the parsed command token to leave args pointing at the first parameter args = args->next; } int8_t count = 0; while(args != NULL && args->type == 1) { // Then grab the actual parameters if (count == 0) param1 = args; if (count == 1) param2 = args; if (count == 3) param3 = args; count++; args = args->next; } // Serial.print(param2->value); // If paramaters aren't required and none passed, then ok if (count == 0 && !parmsRequired) return count; if (count < minCount) { scpi_error error; error.id = -109; error.description = "Missing Parameter Error; too few parameters passed"; error.length = 50; scpi_queue_error(&ctx, error); return -1; } if (count > maxCount) { scpi_error error; error.id = -108; error.description = "Parameter not allowed; too many parameters passed"; error.length = 49; scpi_queue_error(&ctx, error); return -1; } return count; } scpi_error_t measureVoltage(struct scpi_parser_context* context, struct scpi_token* command){ // struct scpi_token* args; struct scpi_token* expectedValue; struct scpi_token* resolution; float voltage; String unit; float divisor = 0.0; int8_t count = extractParameters(command, false, 2, 2, expectedValue, resolution, NULL); if (count != -1) { if (count == 0 || (count == 2 && resolution->length == 1 && memcmp(resolution->value, "V", resolution->length) == 0)) { divisor = 1000.0; unit = "V"; } else { if (count == 2 && (resolution->length == 2 && memcmp(resolution->value, "mV", resolution->length) == 0)) { divisor = 1.0; unit = "mV"; } else { // SCPI ERROR scpi_error error; error.id = -224; error.description = "Illegal Parameter Error;Invalid resolution"; error.length = 42; scpi_queue_error(&ctx, error); } } } if (divisor > 0) { voltage = ( (rand() % (30000 + 1 - 0)) + 0) / divisor; Serial.print("Voltage is: "); Serial.print(voltage,3); Serial.println(unit); } scpi_free_tokens(command); return SCPI_SUCCESS; }
It's actually a bit of SCPI processing code but what it does, is not relevant to my question, but here's the problem. As the code stands above it doesn't work but if I uncomment this line:
// Serial.print(param2->value);
in the first function under the while{} loop - line 19 - it does. It compiles, but on line 53 I call the first function, expecting 'resolution' to have the value of 'param2' on return from the first function. With the line uncommented, it does; with it commented it doesn't.
I'm assuming the compiler is tidying stuff up - perhaps it thinks param2 isn't used given that it isn't referenced in that function once set. So, I further suspect that I'm not passing declaring the function correctly, or passing the parameters correctly. I've scratched my head for a few hours now but I can't work out the issue - c/c++ is not a language I'm overly familiar with.
Can anyone help me work out what is going on?
Thanks
Andrew