Serial.available()

名称

Serial.available()

説明

シリアルポートから読み取り可能なバイト数(文字数)を取得する。これは、すでにArduinoボードに到着していて、シリアル通信用の受信バッファに格納されているデータである。シリアル通信用の受信バッファは64バイトまで格納することができる。

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

書式

メソッド定義int HardwareSerial::available(void);
利用方法Serial.available();

引数

Serialシリアルポートオブジェクト。

各ボードで利用可能なシリアルポートオブジェクトは、Serialを参照。

戻り値

読み出し可能なバイト数。

使用例

以下のコードは、シリアルポートで受信した文字を返す。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
int incomingByte = 0; // for incoming serial data

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

void loop() {
  // reply 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 Mega向けの例: 以下のコードは、あるポートで受信したデータを、他のポートに送信する。これは、例えば、シリアルデバイスをArduinoボードを介して、PCに接続するときに利用できる。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.print(inByte, DEC);
  }
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.print(inByte, DEC);
  }
}

参照

言語 begin()

言語 end()

言語 read()

言語 peek()

言語 flush()

言語 print()

言語 println()

言語 write()

言語 SerialEvent()

言語 Stream.available()

オリジナルのページ

https://www.arduino.cc/reference/en/language/functions/communication/serial/available/

Last Revision: 2019/02/21

実装の解析

HardwareSerial::available()

最終更新日

January 4, 2024

inserted by FC2 system