Pages: [1]   Go Down
Author Topic: Serial Available confuse..  (Read 382 times)
0 Members and 1 Guest are viewing this topic.
universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
???
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://arduino.cc/en/Serial/Available
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25636
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 596
Posts: 33288
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25636
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Pages: [1]   Go Up
Jump to: