Go Down

Topic: Serial.read() only ever returns byte; uses int (Read 516 times) previous topic - next topic

jeffwheeler

Jun 25, 2007, 07:17 am Last Edit: Jun 25, 2007, 07:17 am by jeffwheeler Reason: 1
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?

kg4wsv

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

Go Up