Serial Reading Query

With regards to: http://forum.arduino.cc/index.php?topic=396450
Cause I can’t seem to post there =/

Hi, I have a query which i hope you could answer.

Firstly, I know the ‘A more complete system’ should eradicate this problem.

Assume the text that my serial source is sending over to my arduino is a full line,
abcdefghijklmnopqrstuvwxyz
I understand that serial buffer can only contain max of 64 bytes.

Is there every an instance that the Serial.read() is faster than the speed of the computer that is sending the data?

For example,
if (Serial.available > X) → X is the number of bytes.

Is there such a case where
X becomes 0 before the full a-z is read?
Like, my arduino has read abcdefghijkl and serial.available becomes 0?
Probably cause the sending got delayed or something
Under normal circumstances, will this ever happen?

Or is this where baud rate plays a part in ensuring that ‘there is always data in the buffer if there is input from the source’

Hope you can understand my query.

And thank you so much for this tutorial / guide. I’m most definetly using < > feature to read till the end.

kutysam:
Is there every an instance that the Serial.read() is faster than the speed of the computer that is sending the data?

Serial.read() is very very much faster than data can arrive, even at a high baud rate.

Have a look at the examples in Serial Input Basics

...R

Is there every an instance that the Serial.read() is faster than the speed of the computer that is sending the data?

About 99.999999% of the time, Serial.read() will happen faster than data arrives (or should unless you write blocking code).

PaulS:
About 99.999999% of the time, Serial.read() will happen faster than data arrives (or should unless you write blocking code).

In that case, in such a code,
when i send apple, shouldn't Serial.available be always 1 for the next 5 'loops'?
I.e. Serial.available for loop 0 - 4 be 1

instead of
I.e. Serial.available()
loop0 - 5,
loop1 - 4,
loop2 - 3,
loop3 - 2,
loop4 - 1,

void loop() {
        if (Serial.available() > 0) {
                incomingByte = Serial.read();
        }
}

In my case, I'm experiencing the latter! and i can't seem to understand how can serial.available() be >1 when
"Serial.read() is very very much faster than data can arrive, even at a high baud rate."

Look at this example

unsigned int loopNumber;
char rxed[32];
byte sAva[32];
unsigned int lNum[32];
byte cIndex;

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

void loop() {
  byte sAvail = Serial.available();
  if (sAvail) {
    if (cIndex == 0) {
      loopNumber = 1;
    }
    char gotChar = Serial.read();
    rxed[cIndex] = gotChar;
    sAva[cIndex] = sAvail;
    lNum[cIndex] = loopNumber;
    cIndex++;
    if (cIndex >= sizeof(rxed) || gotChar == 10) {
      for (byte i = 0; i < cIndex; i++) {
        Serial.print(F("available "));
        Serial.print(sAva[i]);
        Serial.print(F(" loop "));
        Serial.print(lNum[i]);
        Serial.print(F(" char "));
        if (rxed[i] >= 0x20) {
          Serial.write('\'');
          Serial.write(rxed[i]);
          Serial.write('\'');
        } else {
          Serial.print(F("0x"));
          Serial.print(rxed[i], HEX);
        }
        Serial.println();
      }
      cIndex = 0;
    }
  }
  loopNumber++;
}

The output for ‘apple’ on 250000 baud with NL and CR

available 1 loop 1 char 'a'
available 1 loop 10 char 'p'
available 1 loop 19 char 'p'
available 1 loop 28 char 'l'
available 1 loop 38 char 'e'
available 1 loop 47 char 0xD
available 1 loop 56 char 0xA

You can see 9 empty loops between the chars, so available above 1 is never seen.

The output for ‘apple’ on 9600 baud with NL and CR

available 1 loop 1 char 'a'
available 1 loop 310 char 'p'
available 1 loop 620 char 'p'
available 1 loop 927 char 'l'
available 1 loop 1236 char 'e'
available 1 loop 1546 char 0xD
available 1 loop 1855 char 0xA

In my case, I'm experiencing the latter!

How can you tell with the code you posted?
Consider how long it takes to report the value returned by "available()" by printing it ...

kutysam:
i can't seem to understand how can serial.available() be >1 when "Serial.read() is very very much faster than data can arrive, even at a high baud rate."

Serial.read() reads a single byte from the Serial Input buffer (assuming there is at least one byte in it) but the time to read all the data in the buffer depends on what other things your program is doing between calls to Serial.read().

Have you studied the examples in the link I gave you in Reply #1?

...R