Serial string sum comparison

Hello! Anybody here could help me out when comparing a string with an internal address at the arduino side?

Using a dip-switch to define the address (0-15) - it's working getting INT (DEC) inside Arduino:

pinMode(9, INPUT_PULLUP); // Pin Dip-Switch(1)
pinMode(10, INPUT_PULLUP); // Pin Dip-Switch(2)
pinMode(11, INPUT_PULLUP); // Pin Dip-Switch(3)
pinMode(12, INPUT_PULLUP); // Pin Dip-Switch(4)
Serial.println("Boot OK");
address = byte((!digitalRead(9)*8)+(!digitalRead(10)*4)+(!digitalRead(11)*2)+!digitalRead(12));

Now I would like to compare a string received by another end ie: "T0x\r" where 'x' would be the internal address. I'm trying this but no working (sending from another side "T03\r)"

comrx = Serial.readStringUntil('\r');
Serial.println(comrx);

if(comrx == "T0" + (address)) ///// --> HERE IS THE COMPARISON THAT I'M TRYNG TO DO
{
 delay(50);
 digitalWrite(enablePin, HIGH);
 delay(10);
 Serial.println("Temp=");
 delay(200);
 digitalWrite(enablePin, LOW);
}

Thank yoU!!

I don't know what "address" is, but to compare it with a String, it needs to be a String.

Better still, don't use Strings (just my opinion)

  • see the ASCII chart for what char values 0-15 represent
  • since '"TO"' is a c-string, the "+" is not how strings are appended together
  • since both comrx and '"TO"' are c-strings, "==" is not how they are compared (use strcmp())

consider the two approaches demonstrated in the code below to compare strings or extract the integer value from "comrx" that can more easily be compared

  TO3  TO0    3
  TO3  TO1    2
  TO3  TO2    1
  TO3  TO3    0
  TO3  TO4   -1
  TO3  TO5   -2
  TO3  TO6   -3
  TO3  TO7   -4
  TO3  TO8   -5
  TO3  TO9   -6
  TO3 TO10    2
  TO3 TO11    2
  TO3 TO12    2
  TO3 TO13    2
  TO3 TO14    2
  TO3 TO15    2
 n = 3
#include <stdio.h>
#include <string.h>

int
main ()
{
    const char *comrx = "TO3";

    for (int i = 0; i <= 15; i++)  {
        char s [10];
        sprintf (s, "TO%d", i);
        printf (" %4s %4s %4d\n", comrx, s, strcmp (comrx, s));
    }


    int  n;
    sscanf (comrx, "TO%d", & n);
    printf (" n = %d\n", n);

    return 0;
}

Thank you for your help! But I got the following message on the code when trying to compile your example:

error line: printf (" %4s %4s %4d\r", comrx, s, strcmp (comrx, s));

cannot convert 'String' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'

if(Serial.available() > 0)
{
comrx = Serial.readStringUntil('\r');
Serial.println(comrx);

for (int i = 0; i <= 15; i++)  
 {
  char s [10];
  sprintf (s, "TO%d\r", i);
  printf (" %4s %4s %4d\r", comrx, s, strcmp (comrx, s));
 }

int  n;
sscanf (comrx, "TO%d", & n);
Serial.print (n,HEX);

return 0;

}

Please post a complete sketch that causes the error

What do mean by "another end"? Are you receiving a string from the InputBox of Serial Monitor via the UART Port?

Thank you for your help. Basically I'll have a master unit asking for up to 16 arduinos the local temperature on a RS-485 BUS. I'm trying to send a poll sequence like T01\r .... up to T15\r to the local arduinos send back the temp status to the master arduino. So, I need to compare the number Txx\r (XX) received by the COM PORT and if its matches, the local arduino addressed will report the temp to the master unit. Here is the code, thank you

#include <stdio.h>
#include <string.h>

int i;
int enablePin = 6;
int address;
int addcomp;
char addressch;
String comrx;

void setup()
{
Serial.begin(19200); // initialize serial at baudrate 9600:
Serial.setTimeout(30); // set serial timeout
pinMode(enablePin, OUTPUT);
pinMode(13, OUTPUT);
pinMode(9, INPUT_PULLUP); // Pin Dip-Switch(1) - address MSB
pinMode(10, INPUT_PULLUP); // Pin Dip-Switch(2)
pinMode(11, INPUT_PULLUP); // Pin Dip-Switch(3)
pinMode(12, INPUT_PULLUP); // Pin Dip-Switch(4) - address LSB
Serial.println("Boot OK");

// convert bits from dip switches to an internal address 0-15
address = byte((!digitalRead(9)*8)+(!digitalRead(10)*4)+(!digitalRead(11)*2)+!digitalRead(12));
Serial.println(address);
Serial.println(address,HEX);
}

void loop() {

if(Serial.available() > 0)
{
comrx = Serial.readStringUntil('\r'); // supposed to receive from COM PORT: "Txx\r" where xx = 00 to 15
Serial.println(comrx);

for (int i = 0; i <= 15; i++)  {
    char s [10];
    sprintf (s, "TO%d", i);
    printf (" %4s %4s %4d\n", comrx, s, strcmp (comrx, s));
}

int  n;
sscanf (comrx, "TO%d", & n);
printf (" n = %d\n", n);

return 0;

// IF STRING "Txx\r" where xx is the INTERNAL ADDRESS, the unit should report this string out to the COM PORT (RS-485)
if
{
delay(50);
digitalWrite(enablePin, HIGH);
delay(10);
Serial.println("Temp=");
delay(200);
digitalWrite(enablePin, LOW);
}

}

}

How about using strstr?

Why do users choose to ignore the advice on posting code ?

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code . Use code tags when posting code here to make it easier to read and copy for examination

I'm newbie here, sorry! lol!!

#include <stdio.h>
#include <string.h>

int i;
int enablePin = 6;
int address;
int addcomp;
char addressch;
String comrx;

void setup()
{
  Serial.begin(19200);        // initialize serial at baudrate 9600:
  Serial.setTimeout(30);      // set serial timeout
  pinMode(enablePin, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(9, INPUT_PULLUP);      // Pin Dip-Switch(1) - address MSB
  pinMode(10, INPUT_PULLUP);     // Pin Dip-Switch(2)
  pinMode(11, INPUT_PULLUP);     // Pin Dip-Switch(3)
  pinMode(12, INPUT_PULLUP);     // Pin Dip-Switch(4) - address LSB
  Serial.println("Boot OK");

  // convert bits from dip switches to an internal address 0-15
  address = byte((!digitalRead(9) * 8) + (!digitalRead(10) * 4) + (!digitalRead(11) * 2) + !digitalRead(12));
  Serial.println(address);
  Serial.println(address, HEX);
}

void loop() {

  if (Serial.available() > 0)
  {
    comrx = Serial.readStringUntil('\r'); // supposed to receive from COM PORT: "Txx\r" where xx = 00 to 15
    Serial.println(comrx);

    for (int i = 0; i <= 15; i++)  {
      char s [10];
      sprintf (s, "TO%d", i);
      printf (" %4s %4s %4d\n", comrx, s, strcmp (comrx, s));
    }

    int  n;
    sscanf (comrx, "TO%d", & n);
    printf (" n = %d\n", n);

    return 0;


    // IF STRING "Txx\r" where xx is the INTERNAL ADDRESS, the unit should report this string out to the COM PORT (RS-485)
    if
  {
    delay(50);
      digitalWrite(enablePin, HIGH);
      delay(10);
      Serial.println("Temp=");
      delay(200);
      digitalWrite(enablePin, LOW);
    }

  }


}

Voilà!

#include <stdio.h>
#include <string.h>

int i;
int enablePin = 6;
int address;
int addcomp;
int addtemp;
String comrx, temprx;

void setup()
{
  Serial.begin(19200);        // initialize serial at baudrate 9600:
  Serial.setTimeout(30);      // set serial timeout
  pinMode(enablePin, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(9, INPUT_PULLUP);      // Pin Dip-Switch(1) - address MSB
  pinMode(10, INPUT_PULLUP);     // Pin Dip-Switch(2)
  pinMode(11, INPUT_PULLUP);     // Pin Dip-Switch(3)
  pinMode(12, INPUT_PULLUP);     // Pin Dip-Switch(4) - address LSB
  Serial.println("Boot OK");

  // convert bits from dip switches to an internal address 0-15
  address = byte((!digitalRead(9) * 8) + (!digitalRead(10) * 4) + (!digitalRead(11) * 2) + !digitalRead(12));
  Serial.println(address);
  Serial.println(address, HEX);
}

void loop() 

{
  if (Serial.available() > 0)
   {
    comrx = Serial.readStringUntil('\r'); // supposed to receive from COM PORT: "Txx\r" where xx = 00 to 15
    Serial.println(comrx);
    temprx = comrx.substring(0, 1);
    if (temprx == "T")
     {
      temprx = comrx.substring(1, 3);
      Serial.println(temprx);
      addtemp = temprx.toInt();
      if (addtemp == address)
       {
        delay(50);
        digitalWrite(enablePin, HIGH);
        delay(10);
        Serial.print("ID ");
        Serial.print(address);
        Serial.print(" Temp=");
        Serial.print("\r");
        delay(200);
        digitalWrite(enablePin, LOW);
       }
      }
     }
}

Parsing fixed strings lengths are OK with .substring option!

Thank you everyone who tried to help me out of this! :slight_smile:

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