This page is also available in 3 other languages

if(Serial)

설명

Indicates if the specified Serial port is ready.

On the Leonardo, if (Serial) indicates whether or not the USB CDC serial connection is open. For all other instances, including if (Serial1) on the Leonardo, this will always return true.

This was introduced in Arduino IDE 1.0.1.

문법

All boards:

if (Serial)

Arduino Leonardo specific:

if (Serial1)

Arduino Mega specific:

if (Serial1)
if (Serial2)
if (Serial3)

매개변수

Nothing

반환

bool : returns true if the specified serial port is available. This will only return false if querying the Leonardo’s USB CDC serial connection before it is ready.

예제 코드

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
}