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.