int Serial.available()

Description

Get the number of bytes (characters) available for reading over the serial port.

Parameters

None

Returns

The number of bytes are available to read in the serial buffer, or 0 if none are available. If any data has come in, Serial.available() will be greater than 0. The serial buffer can hold up to 128 bytes.

Example

int incomingByte = 0;	// for incoming serial data

void setup() {
	Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps
}

void loop() {

	// send data only when you receive data:
	if (Serial.available() > 0) {
		// read the incoming byte:
		incomingByte = Serial.read();

		// say what you got:
		Serial.print("I received: ");
		Serial.println(incomingByte, DEC);
	}
}

Note:

Serial.available has a documented bug in it in Arduino v.0007. This will be patched in v.0008, but see this thread if you wish to patch Serial.available in Arduino 0007.

See also

Reference Home