# Serial data string.equals wild cards?

**URL:** <https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526>\
**Category:** Programming\
**Created:** [March 29, 2024, 3:48pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526 "2024-03-29T15:48:10Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![dogboy](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dogboy](https://forum.arduino.cc/u/dogboy)\
**Post date:** [March 29, 2024, 3:48pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/1 "2024-03-29T15:48:10Z")

</div>

Hi,  
I am trying to receive serial commands for a project, where each received command ends with \n.  
This works well, and I can check which command is received using If else statements.

But one command is 3 fixed characters, say CMD, followed by a 3 digit value (0 to 360).  
So the actual command received is CMD\*\*\*\n where \*\*\* is (000-360).

Using the following code, I can easily receive data until a \n is received then check for the command using if statements:

```arduino
if (Serial1.available()) {
    command = Serial1.readStringUntil('\n');
    command.trim();

    if (command.equals("CMD")) {
      Serial1.write("OK. DO SOMETHING\n");
    }
    else if (command.equals("LED")) {
      Serial1.write("OK, turn on LED");
    }
    else {
      Serial1.write("BAD COMMAND\n");
}

```

So the above code will receive serial commands until \n is received. Then check for which command is received.

But the command CMDxxx, where xxx is a number 0 to 360.

This does not work, but can I use a wild card in the

```arduino
if (command.equals("CMD")) {

```

Like:

```arduino
 if (command.equals("CMD",'***')) {

```

So it will still trap the CMD command with 3 other characters directly after it. But not actually check the 3 numbers apart from that they exist.  
I then need to strip out the 3 numbers.

Does that make sense? Or have I just confused the issue here?

Thanks

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [March 29, 2024, 4:00pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/2 "2024-03-29T16:00:40Z")

</div>

look this over

```arduino

void loop()
{
    if (Serial.available ()) {
        char buf [90];
        int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0'; // terminate with null

        char cmd [10];
        int val = 0;
        sscanf (buf, "%s %d", cmd, & val);

        if (! strcmp (cmd, "CMD")) {
            Serial.print (" cmd ");
            Serial.println (val);
        }
        else if (! strcmp (cmd, "LED")) {
            digitalWrite (LED_BUILTIN, val);
        }
    }
}

void setup()
{
    Serial.begin (9600);
    pinMode (LED_BUILTIN, OUTPUT);
}

```

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 29, 2024, 4:33pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/3 "2024-03-29T16:33:55Z")

</div>

> [@gcjr](#):
>
> look this over

CMD123

doesn't work. But

CMD 123

does.

Curiously the obvious changing to

```arduino
        sscanf (buf, "%s%d", cmd, & val);

```

doesn't seem to work either?

I'm not a fan of _sscanf_(), so I don't know if it can be made to work in this case.

a7

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [March 29, 2024, 4:41pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/4 "2024-03-29T16:41:40Z")

</div>

```arduino
sscanf (buf, "%3s%d", cmd, & val);

```

works but doesn't separating the cmd from an argument make more sense, or are there going to be #'d commands CMD1, CMD1, ...?

---

<div class="post-metadata">

**Author:** ![MaximoEsfuerzo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/maximoesfuerzo/32/923798_2.png) [@MaximoEsfuerzo](https://forum.arduino.cc/u/MaximoEsfuerzo)\
**Post date:** [March 29, 2024, 4:42pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/5 "2024-03-29T16:42:03Z")

</div>

You could use

```arduino
if (command.indexOf("CMD") > -1) {
  // extract the value and what follows
}

```

And then extract the value as you like.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [March 29, 2024, 5:43pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/6 "2024-03-29T17:43:05Z")

</div>

> [@gcjr](#):
>
> doesn't [doing something else] make more sense

Maybe so. Maybe @dogboy has the ability to change both sides of the transaction to suit your idea of what makes more sense.

_sscanf_() can be used as you have shown with the new format argument.

I read this in post #1:

> [@dogboy](#):
>
> So the actual command received is CMD\*\*\*\n where \*\*\* is (000-360).

I don't see it as either making sense or not making sense. It is what it is.

a7

---

<div class="post-metadata">

**Author:** ![dogboy](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dogboy](https://forum.arduino.cc/u/dogboy)\
**Post date:** [March 29, 2024, 5:54pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/7 "2024-03-29T17:54:28Z")

</div>

I will have around 10 commands, all are three characters. Only one command will have a parameter to set a a particular value (0 to 360).  
As in CMDxxx, but I suppose I could split it and receive the CMD first then look for the xxx and convert it to a value, but just seems a bit messy.

---

<div class="post-metadata">

**Author:** ![MaximoEsfuerzo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/maximoesfuerzo/32/923798_2.png) [@MaximoEsfuerzo](https://forum.arduino.cc/u/MaximoEsfuerzo)\
**Post date:** [March 29, 2024, 6:20pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/8 "2024-03-29T18:20:13Z")

</div>

If all commands are 3 characters long except CMDxxx which is 6, it is not that complicated to do.  
If the string read has more than 3 characters, you confirm that it contains "CMD" as I suggested in #5 and then extract the number.  
Otherwise (it would have 3 characters), you execute the statements corresponding to those commands.

---

<div class="post-metadata">

**Author:** ![dogboy](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dogboy](https://forum.arduino.cc/u/dogboy)\
**Post date:** [March 29, 2024, 7:55pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/9 "2024-03-29T19:55:22Z")

</div>

Hi Maximo,

Please can you explain this in more detail.  
I half understand what its doing but not 100% sure if I its right.

Thanks

---

<div class="post-metadata">

**Author:** ![dogboy](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dogboy](https://forum.arduino.cc/u/dogboy)\
**Post date:** [March 29, 2024, 8:12pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/10 "2024-03-29T20:12:11Z")

</div>

OK, I now understand that after reading the Arduino docs.  
Only thing I would need to do is to check that when the CMDxxx is received that the xxx is only 3 characters. So the Indexof traps the CMD part but it would also work with CMDxxxxxxxx...., so I need to check the total length of the command received.

Seems pretty straight forward. And works!

Thanks

---

<div class="post-metadata">

**Author:** ![kenb4](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kenb4](https://forum.arduino.cc/u/kenb4)\
**Post date:** [March 29, 2024, 8:19pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/11 "2024-03-29T20:19:56Z")

</div>

As the [saying goes](https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/)

> [@](#):
>
> Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

The first pattern that comes to mind is

```cpp
CMD([0-9]{3})|LED|FOO|BAR

```

but the [Arduino-listed Regexp library](https://www.arduino.cc/reference/en/libraries/regexp/) does not support `|` for alternates (nor `{}` for repetitions, although that is easily worked-around in this case).

It's simpler to just `startsWith` instead of `equals` and some logic

```cpp
if (command.startsWith("CMD")) {
  command = command.substring(3);
  if (command.length()) {
    int num = command.toInt();
    if (num > 360) {
      Serial.println("max is 360");
    } else if (num < 0) {
      Serial.println("not negative");
    } else if (num > 0 || command.startsWith("0")) {
      Serial.println("do CMD: " + command);
    } else {
      Serial.println("invalid CMD: " + command);
    }
  } else { // "".toInt() returns 0 as well   
    Serial.println("missing CMD number");
  }
} else if (command.equals("LED")) {
  Serial.println("OK, turn on LED");
} else {
  Serial.println("no match");
}

```

BTW, `startsWith()` is equivalent to `indexOf() == 0`. You don't want to match `XCMD` for example, where the index is 1.

---

<div class="post-metadata">

**Author:** ![dogboy](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dogboy](https://forum.arduino.cc/u/dogboy)\
**Post date:** [March 29, 2024, 8:23pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/12 "2024-03-29T20:23:15Z")

</div>

I just used

```arduino
else if ((command.indexOf("ATM") > -1) && (command.length()==6)){
      Serial1.println("Now this traps the command ATMxxx");
    }

```

This works perfectly, just one line.

---

<div class="post-metadata">

**Author:** ![kenb4](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kenb4](https://forum.arduino.cc/u/kenb4)\
**Post date:** [March 29, 2024, 8:27pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/13 "2024-03-29T20:27:12Z")

</div>

That matches for example `3_ATMx` ; do you want that?

---

<div class="post-metadata">

**Author:** ![dogboy](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dogboy](https://forum.arduino.cc/u/dogboy)\
**Post date:** [March 29, 2024, 8:28pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/14 "2024-03-29T20:28:36Z")

</div>

Ah, no!

---

<div class="post-metadata">

**Author:** ![dogboy](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dogboy](https://forum.arduino.cc/u/dogboy)\
**Post date:** [March 29, 2024, 8:35pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/15 "2024-03-29T20:35:55Z")

</div>

This 'Seems' to work:

```arduino
else if ((command.startsWith("ATM")) && (command.length()==6)){
      Serial1.println("Traps ATMxxx");
    }

```

Trying it now, seems ok.

---

<div class="post-metadata">

**Author:** ![kenb4](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kenb4](https://forum.arduino.cc/u/kenb4)\
**Post date:** [March 29, 2024, 8:42pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/16 "2024-03-29T20:42:03Z")

</div>

As long as for example seven is always `007` and not just `7` nor `07`: `ATM007` only. If you want to support `ATM7` then it needs more code.

---

<div class="post-metadata">

**Author:** ![dogboy](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@dogboy](https://forum.arduino.cc/u/dogboy)\
**Post date:** [March 29, 2024, 8:44pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/17 "2024-03-29T20:44:54Z")

</div>

Yes, that particular command will always be in the format ATMxxx so 7 will be 007.  
The commands are coming from Labview, so will be tightly controlled.

Thanks to everyone for your replies. Its much appreciated.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [March 29, 2024, 8:56pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/18 "2024-03-29T20:56:59Z")

</div>

@dogboy i think you are making this over complicated. it's pretty routine to read a cmd with some argument of arbitrary length. since it sounds like you want a cmd that specifies a value from 0 to 360 degrees it seems unnecessary to require that those values be specified by one of 360 commands: CMD001, CMD002, ...CMD360.

and what about the possibility of specifying negative values, relative value.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 25, 2024, 8:57pm UTC](https://forum.arduino.cc/t/serial-data-string-equals-wild-cards/1241526/19 "2024-09-25T20:57:26Z")

</div>

Closed on September 25, 2024, 8:57pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
