Serial.available() returns wrong value.

Arduino Uno.

I’m trying to get string from serial, but Serial.available() function returns wrong value to me.
For example: when I send string “123456789” to arduino, Serial.available() function at first time it returns 1, second time it returns 1 and third time it returns 7 (end of input string).

What I’m doing wrong?
Please excuse me for screenshots in non English language.

int NumberOfBytes = 0;
char chBuffer[60];
char chSubString[60];

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


int GetLen(char *arr)
{
    int res = 0;
    while(arr[res] != '\0')
    {
      res++;
    }
    return res;
}

void SubString(char *arr, int begNumber, int charCount)
{
  int res = begNumber;
  int arrCount = 0;
  while((arr[res] != '\0')&&(res <= (begNumber+charCount-1)))
    {
      chSubString[arrCount] = arr[res];
      arrCount++;
      res++;
    }
    chSubString[arrCount+1] = '\0';
}


void loop() 
{
  NumberOfBytes = Serial.available();
  if (NumberOfBytes > 0)
  {
    Serial.readBytes(chBuffer, NumberOfBytes);
    Serial.print("NumberOfBytes ->");
    Serial.println(NumberOfBytes);
    Serial.print("chBuffer ->");
    Serial.println(chBuffer);
    
    Serial.print("GetLen ->");
    Serial.print(GetLen(chBuffer));
    Serial.println("<-");
    
    SubString(chBuffer, 2, 4);
    Serial.print("SubString ->");
    Serial.print(chSubString);
    Serial.println("<-");
 
  }
}

Take a look to this: http://arduino.cc/en/Serial/Available

there you can read this:

Returns: the number of bytes available to read

Serial data arrives ssslllooowwwlllyyy. The Arduino reads it rapidly. Why does the fact that the whole string doesn't arrive all at once surprise you?

PaulS: Serial data arrives ssslllooowwwlllyyy. The Arduino reads it rapidly. Why does the fact that the whole string doesn't arrive all at once surprise you?

I did not realize that arduino does not wait end of string. Thank you wery much! You really helped me - now I read UART manual at least. Please sorry for my bad English.

Sometimes you can use the Serial object's readBytesUntil() method to simplify reading an input stream of text that has a known termination character (e.g., the newline, '\n'). A typical use can be shown with the following example:

#define MAXCHARS 50

void setup() {

 Serial.begin(115200);

}

void loop() {
 char message[MAXCHARS];
 int charCount;

 if (Serial.available() > 0) {
   charCount = Serial.readBytesUntil('\n', message, MAXCHARS - 1);
   message[charCount] = '\0';    // Make it a string
   Serial.print("characters read = ");
   Serial.print(charCount);
   Serial.print("   message = ");
   Serial.println(message);
  }
}

noknown:

PaulS: Serial data arrives ssslllooowwwlllyyy. The Arduino reads it rapidly. Why does the fact that the whole string doesn't arrive all at once surprise you?

I did not realize that arduino does not wait end of string. Thank you wery much! You really helped me - now I read UART manual at least. Please sorry for my bad English.

The Arduino cannot wait till the end of the string since it knows nothing about any string. The serial interface delivers one character after another, potentially forever, there are no magic delimiters sent out-of-channel to break the stream up. Due to USB serial conversion there's no guarantee an intersymbol gap isn't simply an artifact of the USB drivers.

If you know there's a 9 character string coming, you can elect to do this:

  if (Serial.available () >= 9)
  {
     // read the string, etc
  }