read()

名称

Wire.read()

説明

Wire.requestFrom()を呼んだ後にスレーブデバイスからマスターデバイスに送信されるデータを受信する。もしくは、マスターデバイスからスレーブデバイスに送信されるデータを受信する。

read()は、Streamユーティリティクラスを継承している。

書式

int TwoWire::receive(void);

引数

なし。

戻り値

受信したデータ。

使用例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <Wire.h>

void setup() {
  Wire.begin();             // Join I2C bus (address is optional for controller device)
  Serial.begin(9600);       // Start serial for output
}

void loop() {
    Wire.requestFrom(2, 6);    // Request 6 bytes from slave device number two

    // Slave may send less than requested
    while(Wire.available()) {
        char c = Wire.read();    // Receive a byte as character
        Serial.print(c);         // Print the character
    }

    delay(500);
}

参照

オリジナルのページ

https://www.arduino.cc/reference/en/language/functions/communication/wire/read/

Last Revision: 2022/02/14

最終更新日

January 4, 2024

inserted by FC2 system