# String compareTo() problem

**URL:** <https://community.particle.io/t/string-compareto-problem/51742>\
**Category:** Firmware\
**Tags:** boron\
**Created:** [August 18, 2019, 3:26pm UTC](https://community.particle.io/t/string-compareto-problem/51742 "2019-08-18T15:26:56Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![shanefowler25](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@shanefowler25](https://community.particle.io/u/shanefowler25)\
**Post date:** [August 18, 2019, 3:26pm UTC](https://community.particle.io/t/string-compareto-problem/51742/1 "2019-08-18T15:26:56Z")

</div>

Hello,

I am having issues with the String compareTo method. I am trying to compare the string of the scan from a barcode scanner to another string and then move my servo motor and turn a led on. However, I seem to be having issues with the string comparison part of my code. Can someone lend me a hand? My code is as follows:

```cpp
Servo servo;
int led1 = D6;

void setup() {
    Serial.begin(9600);
    Serial1.begin(9600);
    servo.attach(A0);
    
    digitalWrite(led1, LOW);
    servo.write(134);
}

void loop() {
    String scan = Serial1.readString();
    String barcode = "X00216DEMN";
    Serial.print(scan);
    
    if(scan.compareTo(barcode) == 0){
        servo.write(32);
        digitalWrite(led1, HIGH);
    }
}

```

---

<div class="post-metadata">

**Author:** ![armor](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/armor/32/18398_2.png) [@armor](https://community.particle.io/u/armor)\
**Post date:** [August 18, 2019, 4:18pm UTC](https://community.particle.io/t/string-compareto-problem/51742/2 "2019-08-18T16:18:59Z")

</div>

A couple of observations:

1. Generally better to use c string than String object.
2. It is unnecessary to redeclare barcode in the loop - do this \>1000/second
3. Your compare is probably not matching because there is an unprinted char at the end of scan. This is where using strncmp() is better in that you only compared the number of barcode chars you need/want.

---

<div class="post-metadata">

**Author:** ![shanefowler25](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@shanefowler25](https://community.particle.io/u/shanefowler25)\
**Post date:** [August 18, 2019, 8:27pm UTC](https://community.particle.io/t/string-compareto-problem/51742/3 "2019-08-18T20:27:24Z")

</div>

@armor Thank you for the response. This may be a dumb question, but how do I go about including the c string library? I don’t see it when I search libraries and if I try to declare a string variable I get an error when I verify the code.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [August 18, 2019, 8:38pm UTC](https://community.particle.io/t/string-compareto-problem/51742/4 "2019-08-18T20:38:02Z")

</div>

> [@shanefowler25](#):
>
> c string library

There is no library for C strings. C strings are just an array of characters terminated with a zero byte.  
And C string manipulation functions are available by default.

---

<div class="post-metadata">

**Author:** ![shanefowler25](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@shanefowler25](https://community.particle.io/u/shanefowler25)\
**Post date:** [August 18, 2019, 8:48pm UTC](https://community.particle.io/t/string-compareto-problem/51742/5 "2019-08-18T20:48:37Z")

</div>

Okay thank you, then how am I able to read the input I get form my barcode scan? Serial.read returns an int type and Serial.readString returns a String type. Is there a way to convert these to char?

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [August 18, 2019, 9:45pm UTC](https://community.particle.io/t/string-compareto-problem/51742/6 "2019-08-18T21:45:00Z")

</div>

You could use the [c\_str() method](https://docs.particle.io/reference/device-os/firmware/boron/#c_str-):

```cpp
const char *scan = Serial1.readString().c_str();

```

I would however recommend avoiding using the String class when possible:

```cpp
char scanBuffer[128]; // Hardware Serial1 buffer is 128 bytes on the boron
int counter = 0;
while (Serial1.available() > 0)
{
  // Read into the buffer
  scanBuffer[counter++] = Serial1.read();
}
scanBuffer[counter] = '\0'; // Terminator

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [August 19, 2019, 6:33am UTC](https://community.particle.io/t/string-compareto-problem/51742/7 "2019-08-19T06:33:55Z")

</div>

For the convenience of `readString()` you could use an alternative provided by the `Stream` class which is meant for dealing with arrays and hence can also deal with C strings [`Serial1.readBytes()`](https://docs.particle.io/reference/device-os/firmware/photon/#readbytes-).

```cpp
  char scanBuffer[128];
  int count;
  count = Serial1.readBytes(scanBuffer, sizeof(scanBuffer)-1);
  scanBuffer[count] = '\0';

```

But, this function (as well as `readString()`) need to timeout in order to move on, while @nrobinson2000 version would move on as soon no more data is to be read.  
There are also `readStringUntil()` and `readByteUntil()`) which would allow you to terminate as soon a terminator byte was found.

---

<div class="post-metadata">

**Author:** ![armor](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/armor/32/18398_2.png) [@armor](https://community.particle.io/u/armor)\
**Post date:** [August 19, 2019, 10:15am UTC](https://community.particle.io/t/string-compareto-problem/51742/8 "2019-08-19T10:15:21Z")

</div>

The standard c-library is already included with Particle.h. I would suggest a google search for strncmp to see a reference example of how to implement it. It is something like this:

char scan[10];

then your Serial1.read() will be into the scan[] char array but since it reads 1 byte at a time you will need to place inside a while(Serial1.available() && i \< 10){scan[i] = Serial1.read(); i++;} and increment the index i after setting it to 0 before the while int i = 0;

if ((strncmp(“X00216DEMN”, scan, 10) == 0)  
{  
}

---

<div class="post-metadata">

**Author:** ![shanefowler25](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@shanefowler25](https://community.particle.io/u/shanefowler25)\
**Post date:** [August 19, 2019, 10:45pm UTC](https://community.particle.io/t/string-compareto-problem/51742/9 "2019-08-19T22:45:53Z")

</div>

Thank you @nrobinson2000 your code implemented worked for me and thank you @ScruffR for giving me an alternative. Also, thanks @armor I tried your code as well and it also worked for me. The only problem I’m running into now is when I scan a barcode, the code is printed on my terminal multiple times and then the application seems to be stalled, i.e if I try to scan the same barcode again then nothing happens.

Does anyone know why this seems to happen?

![55%20PM](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/a/4/a461aa226d161034c9b8dfb4d5f4bd6d78951738.png)

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [August 19, 2019, 10:52pm UTC](https://community.particle.io/t/string-compareto-problem/51742/10 "2019-08-19T22:52:43Z")

</div>

Your reader is probably reading the same card multiple times and will fill up the input buffer and any subsequent scan will probably not be aligned correctly.

After getting your first scan, you should flush out all data that’s left in the RX buffer to start off with a clean read.

BTW, to help you with your _new_ code, you should probably show it again 😉

---

<div class="post-metadata">

**Author:** ![shanefowler25](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@shanefowler25](https://community.particle.io/u/shanefowler25)\
**Post date:** [August 19, 2019, 11:00pm UTC](https://community.particle.io/t/string-compareto-problem/51742/11 "2019-08-19T23:00:32Z")

</div>

@ScruffR You read my mind haha. My code is as follows:

```cpp
Servo servo;

char scan[12];
int i = 0;
char barcode[] = "X00216DEMN";

void setup() {
    Serial.begin(9600);
    Serial1.begin(9600);
    servo.attach(A0);
    servo.write(134);
}
void loop() {
    while(Serial1.available() && i < 12){
        scan[i] = Serial1.read();
        i++;
    }
    Serial.print(scan);
    if (strncmp(barcode, scan, 12) == 0){
        servo.write(32);
        delay(3000);
        servo.write(134);
    }
}

```

The barcode scanner I am using operates @5V could that be an issue as to why it’s getting printed multiple times? I have the VCC wire of the scanner wired to the VBUS pin on my Boron device.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [August 19, 2019, 11:09pm UTC](https://community.particle.io/t/string-compareto-problem/51742/12 "2019-08-19T23:09:35Z")

</div>

> [@shanefowler25](#):
>
> The barcode scanner I am using operates @5V

If that scanner puts 5V on the RX pin of your Boron that's no good idea as none of the GPIOs on the Boron are 5V tolerant. But it wouldn't expain the multiple reads.  
Multiple reads are common for NFC readers tho' - the software should deal with such instances gracefully.

However, once you got the voltage issue sorted, this line would do the flushing of the RX buffer

```cpp
  while(Serial1.read() >= 0); 

```

You may want to wrap that also in some sort of timeout routine which also informs the user that the card needs to be removed from the scanner and when a new scan attempt can be made.

BTW, you need to reset `i` once you have dealt with your scan, otherwise your `i < 12` condition will never ever become true again 😉  
Also, (I guess this was an oversight of @armor in his suggested code) when you want to store a 12 (or 10) character string you need an extra byte in the array to _ **also** _ store the terminating `'\0'` character which you'd probably need to add manually (as our code suggestions above featured 🕶) since the scanner won't send that (judging by the output you showed above).  
Otherwise printing that string would just blurt out some extra bytes up until the first occurance of a `'\0'` byte in RAM following the end of the array.

---

<div class="post-metadata">

**Author:** ![shanefowler25](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@shanefowler25](https://community.particle.io/u/shanefowler25)\
**Post date:** [August 19, 2019, 11:34pm UTC](https://community.particle.io/t/string-compareto-problem/51742/13 "2019-08-19T23:34:10Z")

</div>

@ScruffR I looked at the documentation for the scanner and it does not say anything about putting 5V on any of the pins when in use. I manually added the terminating ‘\0’ like in the suggestions above.

Where would I implement the while(Serial1.read() & = 0) code? Would I swap that out for the Serial.available() part? I originally had done that and when I did a scan this was my output?

![12%20PM](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/d/8/d8c4caf227f9f64b84b30156a235c83c711b8d50.png)

This is the code in my loop:

```cpp
void loop() {

    while(Serial1.read() >= 0 && i < 12){
        scan[i] = Serial1.read();
        i++;
    }
    scan[i] = '\0';
    
    Serial.print(scan);
    
    if (strncmp(barcode, scan, 12) == 0){
        servo.write(32);
        delay(3000);
        servo.write(134);
    }
}

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [August 20, 2019, 12:11am UTC](https://community.particle.io/t/string-compareto-problem/51742/14 "2019-08-20T00:11:46Z")

</div>

Just for curiosity, why are you going for 12 bytes when your `barcode` string is only 10 characters long?

Try this

```cpp
// convenience macro for literal comparison
#define STRNCMP(str, literal) strncpy(str, literal, strlen(literal))

// keeping your constants in one place makes tweaking them easier
const int scanIndicator = D7;
const int servoLatency = 250;
const int servoClosed = 134;
const int servoOpen = 32; 
const char barcode[] = "X00216DEMN";

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  
  pinMode(scanIndicator, OUTPUT); // for feedback regarding the scan    
  servo.attach(A0);
  servo.write(servoClosed);
  delay(servoLatency); 
  servo.detach(); // disengage servo after movement to save power

  while(Serial1.read() >= 0) Particle.process(); // flush the RX buffer of any data already present

  digitalWrite(scanIndicator, HIGH); // ready for first scan
}

void loop() {
  // do other stuff independent of your actual scan

  if (Serial1.available() < strlen(barcode)) return; // if not enough data arrived so far bail out
  
  char scan[64];
  digitalWrite(scanIndicator, LOW); // indicate a scan is being processed

  Serial1.readBytes(scan, strlen(barcode)); // we know we have enough data, so read
  scan[strlen(barcode)] = '\0'; // terminate the string  
  Serial.println(scan); // print what we got 
    
  if (STRNCMP(scan, barcode) == 0) {
    servo.attach(A0);
    servo.write(servoOpen);
    delay(3000); // I'd rather use a timer instead of blocking delay 
    servo.write(servoClosed);
    delay(servoLatency);
    servo.detach();
  }

  while(Serial1.read() >= 0) Particle.process(); // flush the RX buffer while allowing for more data  
                                                     // to stream in as long the scanner keeps chatting
  digitalWrite(scanIndicator, HIGH); // ready for next scan
}

```

---

<div class="post-metadata">

**Author:** ![armor](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/armor/32/18398_2.png) [@armor](https://community.particle.io/u/armor)\
**Post date:** [August 20, 2019, 8:29am UTC](https://community.particle.io/t/string-compareto-problem/51742/15 "2019-08-20T08:29:22Z")

</div>

My bad about the char scan array size - should have been bigger than the 10 chars barcode shown in the compare to value but I was trying to give guidance not an exact solution! Printing out the c string wasn’t in the example!
