Char in if statement

I am using the string compare function in an IF statement simplified code is below:

void setup(){
Serial2.begin(115200,SERIAL_8N1); //open modem serial port
}
void loop() {
char pdp = Serial2.read();
if (strcmp (pdp,"ERROR") == 0) {
	//execute this
}

But I am getting this error

exit status 1

'pdp' was not declared in this scope

How can I fix this? I have tried adding pdp = 'test'; in the set-up but then I get the error:

exit status 1
invalid conversion from 'char' to 'const char*' [-fpermissive]

The above just reads in 1 character, and ONLY if one is available. You need to use Serial.available() first. You will need to read in the entire C-string before comparison.

This is not valid for a C-string:
pdp = 'test';

use
char pdp[] = "test";
to create a 5-element, zero terminated C-string.

Check out the Serial Input Basics tutorial in the forum.

I actually did have a while loop structure for the serial.available but I removed it while simplifying the code. Which I guess made it over simplified. But adding the things you mentioned below.

void setup(){

char pdp[] = "test";
Serial2.begin(115200,SERIAL_8N1); //open modem serial port
}

void loop() {

 while (!Serial2.available()) continue;
 while (Serial2.available()) {
       char pdp = Serial2.read();
}
if (strcmp (pdp,"ERROR") == 0) {
	//execute this
}

It is still wrong, in several places. Time to study the Serial Input Basics tutorial.

This line creates a new, local variable named pdp and stuffs in a single character.
char pdp = Serial2.read();

This 'pdp' is local to setup() and can't be seen by loop().

This 'pdp' is local to the body of the 'while' loop and can't be seen by the rest of loop().

This 'pdp' is not declared in this scope. You have two OTHER 'pdp's declared in two other scopes.

1 Like

This declaration of pdp is a character string (or array), is local to setup(), and is not used:

void setup(){

char pdp[] = "test";
Serial2.begin(115200,SERIAL_8N1); //open modem serial port
}

This declaration of pdp is a single character and is local to the while loop and therefore cannot be accessed outside of the while loop:

 while (Serial2.available()) {
       char pdp = Serial2.read();
}

This if statement references a variable pdp which is not declared in this scope:

if (strcmp (pdp,"ERROR") == 0) {
	//execute this
}
1 Like

@feynman137 Your simplified code is missing a closing brace.

Or you didn't post a complete sketch.

a7

Yea looks like I accidently cut-off the last bracket when simplifying the code. It didn't work anyway though. I have been thinking about all the suggestions here and I read a few tutorials. This one was super helpful: link

From the tutorial I took the code below that actually does function:

const unsigned int MAX_MESSAGE_LENGTH = 12;

void setup() {

	Serial2.begin(115200,SERIAL_8N1); //open modem serial port
	Serial.begin(115200);  // Open main serial port
	Serial.println("serial ports are open");
  
}
void loop() {
  delay(5000);
  // put your main code here, to run repeatedly:
 Serial2.write("AT\r\n");
 while (Serial2.available() > 0){
   //Create a place to hold the incoming message
   static char pdp[MAX_MESSAGE_LENGTH];
   static unsigned int pdp_pos = 0;
   
   //Read the next available byte in the serial receive buffer
   char inByte = Serial2.read();
   
   //Message coming in (check not terminating character) and guard for over message size
   if ( inByte != '\n' && (pdp_pos < MAX_MESSAGE_LENGTH - 1) )
   {
     //Add the incoming byte to our message
     pdp[pdp_pos] = inByte;
     pdp_pos++;
   }
   
   //Full message received...
   else
   {
     //Add null character to string
     pdp[pdp_pos] = '\0';
        //Print the message (or do other things)
     Serial.println(pdp);
     //Reset for the next message
      pdp_pos = 0;
     
     if (strcmp(pdp,"ERROR\r")==0) {
      Serial.println("true");
     }
     else {
      Serial.println("false");
     }
   }
      
}
}

I am stuck again though due to a complexity I haven't mentioned here yet though. I am looking for an "ERROR" message from the serial2 connection. My microcontroller is issuing AT commands and the client will respond with 4 or so messages. If a message has error that will be one entire message ("ERROR") of 4 or so messages. The other messages are config info or specifics like signal strength.

If I see an error message I want to reboot the client. But the problem I am having is I will see many messages. So I can't allow my program to exit the if loop looking for error after the first message, because I will never scan messages 2, 3 ..etc. for "ERROR".

Is there a way that I could make the program scan messages for "ERROR" for x seconds or y messages? And if just one message is found to have "ERROR" perform an executable like set a gpio high.

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