Get the number of bytes (characters) available for reading from the serial port. This refers to data that has already been received and is currently stored in the serial receive buffer (which holds 64 bytes).
Serial.available() inherits from the Stream utility class.
Use the following function to get the number of available bytes in the input serial buffer:
Serial.available()
The function admits the following object:
Serial: serial port object. See the list of available serial ports for each board on the Serial main page.
The function returns the number of bytes available to read.
The following code returns a character received through the serial port.
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// reply 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);
}
}
Multiple serial example: This code sends data received in one serial port of the Arduino board to another. This can be used, for example, to connect a serial device to the computer through the Arduino board.
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
Serial1.print(inByte, DEC);
}
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.print(inByte, DEC);
}
}