當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Arduino Serial.read()用法及代碼示例

說明

讀取傳入的串行數據。

Serial.read() 繼承自 Stream 實用程序類。

用法

Serial.read()

參數

Serial:串口對象。請參閱 Serial main page 上每個板的可用串行端口列表。

返回

傳入串行數據的第一個字節可用(如果沒有數據可用,則為 -1)。數據類型:int

示例代碼

int incomingByte = 0; // for incoming serial data

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
  }
}

相關用法


注:本文由純淨天空篩選整理自arduino.cc大神的英文原創作品 Serial.read()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。