创客百科

姿势共享,有节操无门槛参与的创客百科,创客动力之源 \ (^_^) /

用户工具

站点工具


en:arduino:language:serial_ifserial

Serial

if (Serial)

Description

Indicates if the specified Serial port is ready.

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

This was introduced in Arduino 1.0.1.

Syntax

All boards:
if (Serial)

Arduino Leonardo specific:
if (Serial1)

Arduino Mega specific:
if (Serial1)
if (Serial2)
if (Serial3)

Parameters

none

Returns

boolean : 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.

Example:

void setup() { 
 //Initialize serial and wait for port to open:
  Serial.begin(9600); 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
} 
 
void loop() { 
 //proceed normally
} 

See also

  • begin()
  • end()
  • available()
  • read()
  • peek()
  • parseInt()
  • flush()
  • print()
  • println()
  • write()
  • SerialEvent()

Reference Home

本页面的其他翻译:
en/arduino/language/serial_ifserial.txt · 最后更改: 2016/12/25 22:15 (外部编辑)