user46742: 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.

user30175: 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);
}


46742: <p>I am trying to use the <a href="https://playground.arduino.cc/Code/CmdMessenger" rel="nofollow noreferrer">CMDmessanger</a> library on my Arduino uno, and am facing an issue.</p>

<p>My issue starts right from the <a href="https://github.com/thijse/Arduino-CmdMessenger/tree/master/examples/ArduinoController" rel="nofollow noreferrer">ArduinoController.ino</a> example.</p>

<p>When i send the frequency command</p>

<pre><code>3,1;
</code></pre>

<p>The led starts blinking correctly, once per second.</p>

<p>But when I send a wrong command (basically a command without an argument)</p>

<pre><code>3;
</code></pre>

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

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

<p>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.</p>

<p>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.</p>

<p>Thanks in advance.</p>
 30175: <p>The example code uses <code>cmdMessenger.readFloatArg();</code> (<a href="https://github.com/thijse/Arduino-CmdMessenger/blob/master/CmdMessenger.cpp#L454" rel="nofollow noreferrer">source</a>) to read an argument. </p>

<pre><code>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 &lt; 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);
}
</code></pre>

<p>That function will set <code>ArgOk</code> to <code>false</code> if there was no next argument.</p>

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

<p>Thus you should try to use public function</p>

<pre><code>bool isArgOk();

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

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

<pre><code>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 &lt; 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);
}
</code></pre>
