Pages: [1]   Go Down
Author Topic: Serial.read() only ever returns byte; uses int  (Read 485 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I noticed that Serial.read() only returns one byte, as designed (and according to the docs: http://www.arduino.cc/en/Serial/Read), but still uses an int memory location, of which the first byte will always be empty.

Shouldn't it always return a byte? Or, is this to allow for certain compatibilities somewhere?
« Last Edit: June 25, 2007, 12:17:48 am by jeffwheeler » Logged

0
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It needs something larger than a byte for the same reason getchar() from stdio needs to return something larger than a char - so you've got a way to represent some non-character data, namely the EOF condition.

A byte can represent 256 values, namely the 256 extended ASCII values.  The leaves no room for an EOF flag, so we need a larger type that will hold 256 values for a byte, plus one more value for EOF.

-j
Logged

Pages: [1]   Go Up
Jump to: