read()

描述

讀取藉由 I2C 介面傳送過來的資料。如果是主端應該在呼叫 requestFrom() 後才呼叫 read()

read() 是從 Stream 繼承而來。

語法


Wire.read()

參數

無參數

回傳

byte:從伺服器傳送來的資料,如果沒有資料可以讀取,會回傳 -1

範例

#include <Wire.h>

void setup()
{
  Wire.begin();        // 初始化 I2C
  Serial.begin(9600);  // 初始化序列埠
}

void loop()
{
  Wire.requestFrom(2, 6);    // 向 I2C 位址為 2 的從端請求六個位元組的資料

  while(Wire.available())
  { 
    char c = Wire.read();    // 讀取從端傳送的資料
    Serial.print(c);         // 透過序列埠印出讀取到的資料
  }

  delay(500);
}

函式庫參考主頁面

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.