Serial.readBytes()
Descripción
Serial.readBytes() lee caracteres desde el búfer del puerto serie. La función termina si la longitud determinada se ha leído, o el tiempo de espera (ver Serial.setTimeout()) de ha alcanzado.
Serial.readBytes() devuelve el número de caracteres colocados en el búfer. Un 0 significa que no se encontraron datos válidos.
Serial.readBytes() hereda de la clase Stream.
Sintaxis
Serial.readBytes(buffer, length)
Parámetros
- buffer: el buffer para almacenar los bytes (char [ ] o byte [ ])
- length: el número de bytes a leer (int)
Retornos
- Byte
Ejemplo
- Type "HELLO" on Serial Monitor and click Send button:
- The result on Serial Monitor:
※ Nota:
- 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:
- Make sure that the read length does NOT exceed the buffer size. The below is the best practice:
※ ARDUINO BUY RECOMMENDATION
Arduino UNO R3 | |
Arduino Starter Kit |
Additionally, some links direct to products from our own brand, DIYables.