available()

描述

透過串列埠,取得可供讀取的 byte (字元) 個數。這些可供讀取的資料已經被串列埠讀取進來並且儲存在記憶體中 (最多 4096 bytes)。available() 是繼承於 Stream 類別。

語法

適用所有板子:

Serial.available()
Serial1.available()

適用 86Duino ONE:

Serial2.available()
Serial3.available()
Serial485.available()

適用 86Duino EduCake:

Serial2.available()
Serial3.available()
Serial232.available()

參數

無

回傳

可供讀取的 byte 數

範例

int incomingByte = 0;   // 給串列資料使用的變數

void setup() {
        Serial1.begin(9600);     // 打開串列埠,並且設定鮑率為 9600 
}

void loop() {

        // 只在有收到資料的時候送值
        if (Serial1.available() > 0) {
                // 讀取收到的值
                incomingByte = Serial1.read();

                // 告訴對方你收到了什麼
                Serial1.print("I received: ");
                Serial1.println(incomingByte, DEC);
        }
}

其他範例:

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);

}

void loop() {
  // 從 port 0 收資料,從 port 1 送資料:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.print(inByte, BYTE);

  }
  // 從 port 1 收資料,從 port 0 送資料:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.print(inByte, BYTE);
  }
}

See also

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


語法參考主頁面

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.