# Reading text from serial and compare the input always fails

**URL:** <https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509>\
**Category:** Programming\
**Created:** [February 27, 2025, 9:10am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509 "2025-02-27T09:10:53Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![cprins\_1908](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cprins_1908/32/1137222_2.png) [@cprins\_1908](https://forum.arduino.cc/u/cprins_1908)\
**Post date:** [February 27, 2025, 9:10am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/1 "2025-02-27T09:10:53Z")

</div>

Hallo there, for our projects based on several types of arduino I want make standard serial interface.  
Since this must also work on older versions I want to avoid the String functions since they are slow.

For now I just read the serial interface byte by byte and store this in a char array.  
After I have all the characters I want to compare them with some other char arrays

So for example I send ID? via the serial bus and it gives me the ID number back

However the comparison always fails  
some how the char arrays are not equal.

here is my code

```arduino
char inputbuf[32];
char Test[]="ID?";
char Test2[]="Test";
int counter;

void setup() {
  Serial.begin(9600); 
  inputbuf[0]='\0';
  counter=0;
}

void loop() {
 while (Serial.available()>0)
 { 
  inputbuf[counter] = Serial.read();
  counter++;
 }
 if (counter>0)
 {
  inputbuf[counter]='\0'; // also without this line the comparison fails
  Serial.println(inputbuf); // this shows ID?
  if (strcmp(inputbuf,Test)==0)
  {
   Serial.println(inputbuf); // this is never reached
  } 

  inputbuf[0]='\0';
  counter=0; 
 } 
}

```

---

<div class="post-metadata">

**Author:** ![bastjan](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bastjan/32/219200_2.png) [@bastjan](https://forum.arduino.cc/u/bastjan)\
**Post date:** [February 27, 2025, 9:18am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/2 "2025-02-27T09:18:00Z")

</div>

inputbuf[counter+1]='\0';

Try this

---

<div class="post-metadata">

**Author:** ![cprins\_1908](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cprins_1908/32/1137222_2.png) [@cprins\_1908](https://forum.arduino.cc/u/cprins_1908)\
**Post date:** [February 27, 2025, 9:37am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/3 "2025-02-27T09:37:57Z")

</div>

Thanks that helps.  
After the last read the counter is already increased but apperantly the '\0' should be 1 position further

---

<div class="post-metadata">

**Author:** ![cprins\_1908](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cprins_1908/32/1137222_2.png) [@cprins\_1908](https://forum.arduino.cc/u/cprins_1908)\
**Post date:** [February 27, 2025, 9:41am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/4 "2025-02-27T09:41:09Z")

</div>

oops i replied to soon this also didn't work

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 27, 2025, 9:44am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/5 "2025-02-27T09:44:24Z")

</div>

If the Serial input is coming from the Serial monitor then what have you got the Line Ending set to ?

Have you tried printing the length of the string before comparing it ?

---

<div class="post-metadata">

**Author:** ![docdoc](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/docdoc/32/220907_2.png) [@docdoc](https://forum.arduino.cc/u/docdoc)\
**Post date:** [February 27, 2025, 9:45am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/6 "2025-02-27T09:45:19Z")

</div>

> [@cprins\_1908](#):
>
> After I have all the characters I want to compare them with some other char arrays

Not exactly, but it depends on your "serial protocol". Are all the commands the same size in terms of characters ora can be variable lengths? Do you use a "line terminator" character?  
Based on your description, the answer is "no, the commands are variable and use line terminating characters \r\n sent from the Serial monitor input".  
To make things easier, my advise is to use single-character commands, for example just "I" (followed by "\r\n" automatically added) for Info request, or "T" for test.

But if you want to keep your syntax, your sample code to receive a command and compare with some fixed command strings could be something like this one:

```arduino
#define MAXBUF 32
char inputbuf[MAXBUF];
int counter = 0;
bool cmdReceived = false;

// Fixed commands list (note 
#define CMD_ID "ID?"
#define CMD_TEST "TEST"

void setup() {
  Serial.begin(9600); 
}

void bufReset() {
  inputbuf[0]='\0';
  counter=0;
  cmdReceived = false;
}

void bufAdd(char c) {
  // Ignore '\r' and '\n' and avoid buffer overflow
  if (c != '\r' && c != '\n' && counter+1 < MAXBUF) {
    // Convert to uppercase if needed
    if (c >= 'a' && < <= 'z') c = c - 'a' + 'A';
    inputBuf[counter++] = c;
    inputBuf[counter] = '\0';
  }
  // If LF has been received, the command is complete
  cmdReceived = (c == '\n');
}
void loop() {
  if (Serial.available()>0) {
    bufAdd(Serial.read());
  }
  if (cmdReceived) {
    Serial.print("Received: ");Serial.println(inputbuf);
    if (strcmp(inputbuf,CMD_ID)==0)
    {
      Serial.println("The ID is 1");
    }
    else if (strcmp(inputbuf,CMD_TEST)==0)
    {
      Serial.println("Test OK");
    }
    else
      Serial.println("Unkown command");

    // Reset buffer
    bufReset();
  } 
}

```

Please note the changes I made, including "strcasecmp()" function to compare ignoring upper/lower case, and the usage of two useful functions for an easier handling of the buffer...

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [February 27, 2025, 10:00am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/7 "2025-02-27T10:00:53Z")

</div>

I suspect that this is the most referred to tutorial: [Serial Input Basics - updated](https://forum.arduino.cc/t/serial-input-basics-updated/382007/).

You can study it to get ideas.

---

<div class="post-metadata">

**Author:** ![cprins\_1908](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cprins_1908/32/1137222_2.png) [@cprins\_1908](https://forum.arduino.cc/u/cprins_1908)\
**Post date:** [February 27, 2025, 11:54am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/8 "2025-02-27T11:54:56Z")

</div>

I found the issue.  
It was indeed due to the length of the two char arrays.  
I solved it by using the strncomp function

```arduino
if (strncmp(command, "Test",4) == 0)

```

---

<div class="post-metadata">

**Author:** ![docdoc](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/docdoc/32/220907_2.png) [@docdoc](https://forum.arduino.cc/u/docdoc)\
**Post date:** [February 28, 2025, 10:28am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/9 "2025-02-28T10:28:27Z")

</div>

> [@cprins\_1908](#):
>
> I solved it by using the strncomp function

Well, strncmp() compares the strings given the number of chars to be compared, so it isn't wrong, but if you have a command like "Test1234-this and that" it will satisfy the condition because it compares just the first 4 characters: is it really what you want to get?

Post your entire (new) code, so I can get the whole context...

---

<div class="post-metadata">

**Author:** ![cprins\_1908](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cprins_1908/32/1137222_2.png) [@cprins\_1908](https://forum.arduino.cc/u/cprins_1908)\
**Post date:** [February 28, 2025, 1:08pm UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/10 "2025-02-28T13:08:53Z")

</div>

This is the working code

```arduino
# include "FlashAsEEPROM.h"

const int MAX_COMMAND_LENGTH = 32;
char command[MAX_COMMAND_LENGTH];
int commandIndex = 0;

const String IDNr = "ODDeviceName_";
const byte SerialNrAddress = 0;

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  // Read incoming serial data
  while (Serial.available() >0) 
  {
    char inChar = Serial.read();
    
    if (commandIndex < MAX_COMMAND_LENGTH - 1) 
    {
      command[commandIndex] = inChar;
      commandIndex++;
    }
  }
  if (commandIndex>0) 
  {
    command[commandIndex] = '\0'; // Null-terminate the string
    processCommand();

    commandIndex = 0;
  }
}

void processCommand() 
{
  if (strncmp(command, "?",1) == 0) 
  {
    Serial.println("Data");
    return;
  }
  else if (strncmp(command, "Test",4) == 0) 
  {
    Serial.println("Test command received. System is operational.");
    return;
  }
  else if (strncmp(command, "ID?",3) == 0) 
  {
    byte SerieNr = EEPROM.read(SerialNrAddress);
    char buf[4];
    sprintf(buf,"%03d",SerieNr);
    Serial.println("ID="+IDNr + buf);    
    return;
  }
  else if (strncmp(command, "SN=", 3) == 0) 
  {
    byte serialNumber = atoi(command + 3);
    if (!EEPROM.isValid()) 
    {
     EEPROM.write(SerialNrAddress, serialNumber);
     return;     
    }
    EEPROM.commit(); 
    serialNumber = EEPROM.read(SerialNrAddress);
    char buf[4];
    sprintf(buf,"%03d",serialNumber);
    Serial.print("Serial number set to: ");
    Serial.println(buf);
    return;
  }
  else 
  {
    Serial.println("Unknown command");
    return;
  }
}

```

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [February 28, 2025, 1:17pm UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/11 "2025-02-28T13:17:36Z")

</div>

Maybe [strstr](https://man7.org/linux/man-pages/man3/strstr.3.html) makes life easier.

And as mentioned Robin's updated [Serial Input Basics](https://forum.arduino.cc/t/serial-input-basics-updated/382007/) is always a good read.

---

<div class="post-metadata">

**Author:** ![docdoc](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/docdoc/32/220907_2.png) [@docdoc](https://forum.arduino.cc/u/docdoc)\
**Post date:** [February 28, 2025, 5:41pm UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/12 "2025-02-28T17:41:07Z")

</div>

> [@cprins\_1908](#):
>
> This is the working code

Ok, so the behavior of that piece of code is any command **starting** with the given string is accepted, including "?xyz" as "?", "TestJoe" as "Test" and so on. Is this what you really need?  
And I still can't get why using just "strcmp()" (as per my code example, shich I can assure you it works correctly), why you said you needed to use "strncmp()"?

---

<div class="post-metadata">

**Author:** ![cprins\_1908](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cprins_1908/32/1137222_2.png) [@cprins\_1908](https://forum.arduino.cc/u/cprins_1908)\
**Post date:** [March 3, 2025, 7:44am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/13 "2025-03-03T07:44:05Z")

</div>

I am aware of the fact this code doesn't handle all the incoming data in a correct manner. But in this case we only use the devices for automating some measurment setup's.  
For this we also make our own PC software so we can control the commands we are sending.

regarding the strcmp function I guess I have to take into account the '\n' or '\0' characters as well when comparing the two strings.

---

<div class="post-metadata">

**Author:** ![docdoc](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/docdoc/32/220907_2.png) [@docdoc](https://forum.arduino.cc/u/docdoc)\
**Post date:** [March 3, 2025, 8:24am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/14 "2025-03-03T08:24:54Z")

</div>

> [@cprins\_1908](#):
>
> I have to take into account the '\n' or '\0' characters

Sure, the strings to be compared must have the correct format, with the terminating '\0' characters.

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [March 3, 2025, 9:25am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/15 "2025-03-03T09:25:18Z")

</div>

> [@cprins\_1908](#):
>
> I want to avoid the String functions

but strcmp **is** a string function

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 3, 2025, 9:45am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/16 "2025-03-03T09:45:42Z")

</div>

True, strcmp() is a string (lowercase s) function but it is not a String (uppercase S) function and thus using it is part of the strategy of avoiding using Strings

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [March 3, 2025, 10:53am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/17 "2025-03-03T10:53:03Z")

</div>

You are right.

---

<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:** [August 30, 2025, 10:53am UTC](https://forum.arduino.cc/t/reading-text-from-serial-and-compare-the-input-always-fails/1358509/18 "2025-08-30T10:53:37Z")

</div>

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