Indicates if the specified Serial port is ready.
On the boards with native USB, if (Serial) (or if(SerialUSB) on the Due) indicates whether or not the USB CDC serial connection is open. For all other boards, and the non-USB CDC ports, this will always return true.
Use the following function to check if the serial connection is open:
if (Serial)
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 true if the specified serial port is available. This will only return false if the query is made to a USB CDC serial connection before it is ready. Data type: bool.
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
}
void loop() {
//proceed normally
}
This function adds a delay of 10ms in an attempt to solve "open but not quite" situations. Don’t use it in tight loops.