Serial.read()

설명

들어오는 시리얼 데이터를 읽는다. read()는 Stream utility class 로부터 상속받는다.

문법

Serial.read()

Arduino Mega only:

Serial1.read()

Serial2.read()

Serial3.read()

매개변수

  • 없음

반환값

  • 들어온 데이터의 첫 바이트 사용가능(또는 사용할 데이터 없으면 -1). 데이터 유형: int.

예제 코드

int incomingByte = 0; // for incoming serial data void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps } void loop() { // send data only when you receive data: if (Serial.available() > 0) { // read the incoming byte: incomingByte = Serial.read(); // prints the received data Serial.print("I received: "); Serial.println((char)incomingByte, DEC); } }
  • Type "HELLO" on Serial Monitor and click Send button:
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • 시리얼 모니터에 결과:
COM6
Send
I received: H I received: E I received: L I received: L I received: O I received:
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ 주의 및 경고:

Serial functions are not only used for the communication between an Arduino board and Serial Monitor of Arduino IDE but also used for the communication between:

  • An Arduino board and other Arduino board
  • An Arduino board and other sensors/devices
  • An Arduino board and computer (any Serial software on computer)* An Arduino board and other Arduino board
  • An Arduino board and other sensors/devices
  • An Arduino board and computer (any Serial software on computer)

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

※ OUR MESSAGES