if (Serial)

描述

指定的串列埠是否可用。

在所有 86Duino 板子上,使用 if (Serial) 可以知道 USB CDC 虚拟序列埠是否已经被开启。对于其他硬体序列埠 (UART),例如使用 if (Serial1) 则总是回传 true。

语法

适用所有板子:

if (Serial)
if (Serial1)

适用 86Duino ONE:

if (Serial2)
if (Serial3)
if (Serial485)

适用 86Duino EduCake:

if (Serial2)
if (Serial3)
if (Serial232)

参数

回传

布林值:指定的序列埠假如是可用的状态时,则回传 true。假如 USB CDC 虚拟序列埠是不可用的状态,则回传 false。

范例

void setup() {
 //初始化序列埠,然后等待它开启完成:
  Serial.begin(9600);
  while (!Serial) {
    ; // 等待序列埠与板子连接。
  }
}

void loop() {
 //连接后则继续执行程式
} 

See also

Serial
available()
begin()
end()
find()
findUntil()
flush()
parseFloat()
parseInt()
peek()
print()
println()
read()
readBytes()
readBytesUntil()
setTimeout()
write()
serialEvent()


语法参考主页面

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.