Serial Communication

Hello All,

I have the following code that I am running on ESP8266.

const byte numChars = 64;
char receivedChars[numChars];

boolean newData = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}
void serialFlush(){
  while(Serial.available() > 0) {
    char t = Serial.read();
  }
}   

void loop() {
  // put your main code here, to run repeatedly:

    while(!Serial.available())
    {
      Serial.println(Serial.available());
      delay(1000);
    }
    Serial.println(Serial.available());
    recvWithStartEndMarkers();
    Serial.println(receivedChars);
    serialFlush();
   

    delay(5000);
}
void recvWithStartEndMarkers() {
    //server.send(200, "text/html", "<h1>Received 1</h1>");
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
    
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();
        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                //Serial.print(receivedChars[ndx]);
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }//recvInProgress
        else if (rc == startMarker) {
            recvInProgress = true;
        }//else
    }//while
    //Serial.println(receivedChars);
}

All the code does is that it reads data from a serial port (through serial monitor) and rewrites it to the serial monitor.
When I run the code, it works without any issue for the first time. This is, I type something in between carrots and it rewrites it back to the serial monitor.
But, If I enter new string for the second time, it only return the first string. I don't seem to find the issue with the code. So I welcome and appreciate any suggestions.

Regards,

while (Serial.available() > 0 && newData == false) {

When the '>' end character is received, newData is set true. Then after the received data is printed, newData not set to false so the while will not ever execute again.

while(!Serial.available())
    {
      Serial.println(Serial.available());
      delay(1000);
    }

What is that code doing besides wasting time blocking? I think that you need to more carefully study Robin2's code to understand how it works.

Thanks groundFungus,

I am very new to C++ and I have probably deleted the newData = false unintentionally.
Your response solved the issue.

Thanks and Regards,