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
}
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.