Serial.read()

void setup(){
  Serial.begin(115200)};

void loop(){
String stringOne = "";

  if (Serial.available()){
    while (Serial.available()>0){ 
      char a = Serial.read();            
      stringOne += String(a);          
      delay(10);
    }
    if (stringOne == ":GP#"){         
       delay(3000);
       int b = Serial.read();
       Serial.println(b);   
    }
    
}
  
  }

Do you know why the serial doesn't print the b value? I'm sorry I'm new with Arduino :frowning:

because you read the Serial line when there is possibly nothing available and you are not even sure you've read the first input correctly... don't try to second guess the timing of an asynchronous protocol.

I would suggest to study Serial Input Basics to handle this

The serial input basics tutorial will read data into strings (null terminated character arrays) instead of the String class objects that can cause memory problems. That is good, but you cannot use == to compare strings. Use strcmp() instead. Here is a reference to the C++ string functions.

groundFungus:
The serial input basics tutorial will read data into strings (null terminated character arrays) instead of the String class objects that can cause memory problems. That is good, but you cannot use == to compare strings. Use strcmp() instead. Here is a reference to the C++ string functions.

Actually the :GP# command goes; if I write for example after this command

Serial.print("a");

it actually print "a" on the terminal...
The problem is related to Serial.read()

In three seconds, at 11520 characters per second, a 64 character buffer could overflow many times.

TheMemberFormerlyKnownAsAWOL:
In three seconds, at 11520 characters per second, a 64 character buffer could overflow many times.

Therefore, the OP should read the character/data byte as soon as it arrives.

byte n = Serial.available();
if(n != 0)
{
    char x = Serial.read();      //bring out the arrived data from the FIFO type Serial Buffer of UNO/MCU
    Serial.print(x);                 //sending the received character back to OutputBox of Serial Monitor
    myData[i] = x;                 //saving the incoming charcaters in a user defined char type array
    i++;
    if(i == 5)                         //5 charcaters have arrived 
    {
         Serial.println();            //line feed character
         myData[i] = '\0';          //adding null character
         Serial.println(myData); //showing the 5-character string on Serial Monitor
    }            
}

TheMemberFormerlyKnownAsAWOL:
In three seconds, at 11520 characters per second, a 64 character buffer could overflow many times.

So are you suggesting that I need to change the baud?

fedeaero:
So are you suggesting that I need to change the baud?

No.

the suggestion is that you read the tutorial on handling Serial input the right way: deal with the data when it shows up there without second guessing when that will be