Go Down

Topic: Serial Available confuse.. (Read 468 times) previous topic - next topic

creativen

what is the difference between
if (Serial.available())
and
if (Serial.available() > 0)
and
if (Serial.available() > 18)
and
if (Serial.available() <10)
and
if (Serial.available() == 3)
???

Nick Gammon


AWOL

Quote
if (Serial.available())
and
if (Serial.available() > 0)

No practical difference at all.
Serial.available does not return negative values, so any non-zero value means data is available to read.

I'll leave you to figure out the rest.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

creativen

I have read that link but it is not complete to explain every possibility between 0,18,< or > or == ...

I have read a book that code like this:

Code: [Select]

if (Serial.available() > 18) {
char led = Serial.read();
if (led == 'S'){
temp1 = Serial.read();
temp2 = Serial.read();
temp3 = Serial.read();
temp4 = Serial.read();
temp5 = Serial.read();
temp6 = Serial.read();
temp7 = Serial.read();
temp8 = Serial.read();
temp9 = Serial.read();
}
}


what its purpose to put 18 there?

Grumpy_Mike

None what so ever. The code waits untilat least 19 bytes are in the buffer and then reads ten of them.
However in the contex of the whole program it might make sense.

AWOL

Quote
I have read that link but it is not complete to explain every possibility

Given that the serial buffer can contain up to 64 characters, all possibilities would take too much page space to enumerate, so they probably listed a few, and left the reader to figure out the rest.

Quote
what its purpose to put 18 there?

To ensure there are at least 19 characters to read before going about reading them.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up