Need Help on Serial Port Behavior

int count=0;
void setup() 
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial3.begin(9600); 

}

void loop() 
{

  if(Serial.available()>0)
  {
    Serial.println();
    Serial.print("Number of Bytes:");
    Serial.println(Serial.available());
    
    
    while(Serial.available())
    {
      Serial.read();
    }
    Serial1.write("+CMTI: ""SM"",4");
    Serial1.flush();
    
    count++;
    Serial.print("Count:");
    Serial.println(count);
    
    delay(100);
  }

  if(Serial3.available()>0)
  {
    while(Serial3.available())
    {
      Serial.write(Serial3.read());
    }
      Serial.println();
  }

  

}

Hi.. Can anyone explain why after running the loop below, there are still some data in the Serial port.
I want loop inside the while loop for once.

    while(Serial.available())
    {
      Serial.read();
    }
    Serial1.write("+CMTI: ""SM"",4");
    Serial1.flush();

Can anyone explain why after running the loop below, there are still some data in the Serial port.

Probably there's not still data in the buffer but again. The write on the Serial1 object need some time (more than 10 Milliseconds) and during that time some data may have arrived.

daozui:
Hi.. Can anyone explain why after running the loop below, there are still some data in the Serial port.
I want loop inside the while loop for once.

I'm not sure what you are trying to achieve but I suspect the problem is that the

while(Serial.available())

empties the serial input buffer long before the full message has time to arrive. The Arduino is very much faster than serial data - even at high baud rates.

Have a look at Serial Input Basics

...R

Thanks.. Putting a delay before goes into the while loop solved the problem.

daozui:
Putting a delay before goes into the while loop solved the problem.

No, it only hides the problem.

    Serial1.write("+CMTI: ""SM"",4");

You know that the resulting string (+CMTI: SM,4) will not contain any " ?

Whandall:
No, it only hides the problem.

Nicely put.

...R

Whandall:
No, it only hides the problem.

    Serial1.write("+CMTI: ""SM"",4");

You know that the resulting string (+CMTI: SM,4) will not contain any " ?

You meant no " would be written into the serial1?

void setup() {
  Serial.begin(250000);
  Serial.print(F("Your code produces \""));
  Serial.write("+CMTI: ""SM"",4");
  Serial.print(F("\" but you probably wanted \""));
  Serial.write("+CMTI: \"SM\",4");
  Serial.println(F("\""));
}
void loop() {}
Your code produces "+CMTI: SM,4" but you probably wanted "+CMTI: "SM",4"

Whandall:

void setup() {

Serial.begin(250000);
  Serial.print(F("Your code produces ""));
  Serial.write("+CMTI: ""SM"",4");
  Serial.print(F("" but you probably wanted ""));
  Serial.write("+CMTI: "SM",4");
  Serial.println(F("""));
}
void loop() {}




Your code produces "+CMTI: SM,4" but you probably wanted "+CMTI: "SM",4"

I got what you meant now. I really have trouble on that. Thanks for pointing it out.

Here you can read the details Escape sequences - cppreference.com.