Serial.readBytes()

설명

Serial.readBytes()직렬 포트에서 아래 구문의 매개변수중 하나인 buffer로 문자를 읽습니다. 이 함수는 결정된 길이를 읽었거나 시간 초과되면 종료됩니다 (Serial.setTimeout() 참조).

Serial.readBytes() 시리얼 버퍼에있는 문자 수를 반환합니다. 0은 유효한 데이터가 없음을 의미합니다.

Serial.readBytes()Stream 유틸리티 클래스에서 상속합니다.

문법

Serial.readBytes(buffer, length)

매개변수

  • buffer: 바이트를 저장할 버퍼. char또는 배열 byte (char[] or byte[]).
  • length: 읽을 바이트 수 (int).

반환값

  • 시리얼 버퍼에 있는 바이트 수입니다 (size_t).

예제 코드

const int BUFFER_SIZE = 50; char buf[BUFFER_SIZE]; void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps } void loop() { // check if data is available if (Serial.available() > 0) { // read the incoming bytes: int rlen = Serial.readBytes(buf, BUFFER_SIZE); // prints the received data Serial.print("I received: "); for(int i = 0; i < rlen; i++) Serial.print(buf[i]); } }
  • Type "HELLO" on Serial Monitor and click Send button:
COM6
Send
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • 시리얼 모니터에 결과:
COM6
Send
I received: HELLO
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ 주의 및 경고:

  • Serial.readBytes() may read a stream of bytes in multiple times (resulting in multiple fragments). To read a stream of bytes at one time, there are two ways:
    • Based on the delimiter: Use Serial.readBytesUntil() with delimiter.
    • Based on the fixed length: Wait until the fixed number of bytes is available. for example:
    const int BUFFER_SIZE = 5; char buf[BUFFER_SIZE]; void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps } void loop() { // check if data is available if (Serial.available() >= BUFFER_SIZE) { // read the incoming bytes: Serial.readBytes(buf, BUFFER_SIZE); // TODO: PROCESS THE INCOMING DATA HERE } }
    • Make sure that the read length does NOT exceed the buffer size. The below is the best practice:
    const int BUFFER_SIZE = 5; char buf[BUFFER_SIZE]; // application buffer void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps } void loop() { // check if data is available int rxlen = Serial.available(); // number of bytes available in Serial buffer if (rxlen > 0) { int rlen; // number of bytes to read if (rxlen > BUFFER_SIZE) // check if the data exceeds the buffer size rlen = BUFFER_SIZE; // if yes, read BUFFER_SIZE bytes. The remaining will be read in the next time else rlen = rxlen; // read the incoming bytes: rlen = Serial.readBytes(buf, rlen); // TODO: PROCESS THE INCOMING DATA HERE } }
    • 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)

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.

※ OUR MESSAGES