read()

描述

讀取序列埠收到的資料。read() 是繼承於 Stream 類別。

語法

適用所有板子:

Serial.read()
Serial1.read()

適用 86Duino ONE:

Serial2.read()
Serial3.read()
Serial485.read()

適用 86Duino EduCake:

Serial2.read()
Serial3.read()
Serial232.read()

參數

回傳

讀取傳進來的 byte 值 (假如沒有收到任何資料,則回傳 -1) – int 型別

範例

int incomingByte = 0;   // 儲存值的變數

void setup() {
        Serial.begin(9600);     // 設定 Serial 為 9600 鮑率
}

void loop() {

        // 只有在收到值的時候才送資料
        if (Serial.available() > 0) {
                // 讀取傳送進來的 byte:
                incomingByte = Serial.read();

                // 說你收到了什麼
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
        }
}

See also

Serial
available()
begin()
end()
find()
findUntil()
flush()
parseFloat()
parseInt()
peek()
print()
println()
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.