0

I am trying to use the CMDmessanger library on my Arduino uno, and am facing an issue.

My issue starts right from the ArduinoController.ino example.

When i send the frequency command

3,1;

The led starts blinking correctly, once per second.

But when I send a wrong command (basically a command without an argument)

3;

The led just stops blinking. The state of the led depends on which state it was in when I sent the command.

I realised that, when I send the wrongly formatted command, it takes the frequency as 0.

Is there any method by which I can make sure that the command is received in the right format, before it is executed? Ideally I would love it if there is a method to know how many arguments were received, and I could call the readBoolArg, or other equivalent functions to retrieve the argument data.

I am planning on using this library for quite a few projects, and I don't want to be in a situation where things went wrong because there was not formatting check.

Thanks in advance.

asked May 27, 2018 at 11:47
1
  • For some extra context, I am a developer at Inventrom Pvt Ltd and developing a library for interfacing the Bolt WiFi module with the Arduino Here is a link if anyone is interested. Commented May 27, 2018 at 11:50

1 Answer 1

2

The example code uses cmdMessenger.readFloatArg(); (source) to read an argument.

void OnSetLedFrequency()
{
 // Read led state argument, interpret string as boolean
 ledFrequency = cmdMessenger.readFloatArg();
 // Make sure the frequency is not zero (to prevent divide by zero)
 if (ledFrequency < 0.001) { ledFrequency = 0.001; }
 // translate frequency in on and off times in miliseconds
 intervalOn = (500.0/ledFrequency);
 intervalOff = (1000.0/ledFrequency);
 cmdMessenger.sendCmd(kAcknowledge,ledFrequency);
}

That function will set ArgOk to false if there was no next argument.

float CmdMessenger::readFloatArg()
{
 if (next()) {
 dumped = true;
 ArgOk = true;
 //return atof(current);
 return strtod(current, NULL);
 }
 ArgOk = false;
 return 0;
}

Thus you should try to use public function

bool isArgOk();
/**
 * Returns if the latest argument is well formed.
 */
bool CmdMessenger::isArgOk()
{
 return ArgOk;
}

of the CmdMessenger object (which will return ArgOk) to check if the read argument was okay. You could e.g. modify the code as follows

void OnSetLedFrequency()
{
 // Read led state argument, interpret string as boolean
 ledFrequency = cmdMessenger.readFloatArg();
 //Did we just read garbage because the argument wasn't there?
 if(!cmdMessenger.isArgOk()) {
 //Using Serial might be a bad idea because that cmdMessanger operates on the same Serial stream.. but you get the idea.
 Serial.println("You didn't supply a correct argument! Error.");
 return; //goodbye
 }
 // Make sure the frequency is not zero (to prevent divide by zero)
 if (ledFrequency < 0.001) { ledFrequency = 0.001; }
 // translate frequency in on and off times in miliseconds
 intervalOn = (500.0/ledFrequency);
 intervalOff = (1000.0/ledFrequency);
 cmdMessenger.sendCmd(kAcknowledge,ledFrequency);
}
answered May 27, 2018 at 12:12
1
  • Thank you. I didn't realise that's how the isArgOk function worked. Commented May 27, 2018 at 12:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.