Skip to Content
Circuitera

Distinguish Serial.read() returning −1 from byte 255

Serial.read() returns an int.

0 of 3 complete · Progress saved on this device

Work out the example

Serial.read() returns an int. A negative result signals an empty receive buffer. Valid data bytes span 0 through 255. Converting the result directly to a byte makes −1 wrap to 255.

What value does converting −1 to a uint8_t produce?

Decide what to check next

Keep the received result in an int until after the sentinel check. A 0xFF byte can be valid binary data, so use a separate framing or integrity check if your protocol needs one.

Compare with the serial result

The demonstration prints empty and then 255 without waiting for a physical sender.

Open the exact Uno example

The demonstration prints empty and then 255 without waiting for a physical sender.

void setup() {
  Serial.begin(9600);
  int values[]={-1,255};
  for (int value : values) {
    if (value < 0) Serial.println("empty");
    else Serial.println(static_cast<byte>(value));
  }
}
void loop() {}

Verified with [email protected] on Uno R3 (2026-12-05 UTC). This check did not use physical hardware.

Continue learning

Sources