HardwareSerial::peek()

概要

Serial.peek()/HardwareSerial::peek()は、シリアル通信の受信用バッファを変更せずに受信用バッファから文字を読み出します。リファレンスはこちら}。

ソースコード

HardwareSerial::peek()は、hardware/arduino/avr/cores/arduino/HardwareSerial.cppに定義されています。以下に全ソースコードを示します。

1
2
3
4
5
6
7
8
int HardwareSerial::peek(void)
{
  if (_rx_buffer_head == _rx_buffer_tail) {
    return -1;
  } else {
    return _rx_buffer[_rx_buffer_tail];
  }
}

入力はありません。戻り値はintです。

1
2
int HardwareSerial::peek(void)
{

受信バッファのheadとtailが同じ場合は、何も受信していないので、-1を返します。

3
4
5
6
// if the head isn't ahead of the tail, we don't have any characters
if (_rx_buffer_head == _rx_buffer_tail) {
  return -1;
} else {

そうでない場合(headとtailが異なる場合)は、一番古いデータ(buffer[tail]に格納されています)を返却します。

5
6
7
8
  } else {
    return _rx_buffer[_rx_buffer_tail];
  }
}

受信バッファの操作は行いません。

バージョン

Arduino AVR Boards 1.8.6

最終更新日

March 21, 2023

inserted by FC2 system