Serial.readBytesUntil()
Descripción
Serial.readBytesUntil() lee los caracteres del buffer serie en una matriz. La función termina si:
- Se detecta el carácter terminador,
- La longitud determinada ha sido leído,
- O el tiempo de espera se ha alcanzado (ver Serial.setTimeout()).
Serial.readBytesUntil() devuelve el número de caracteres leídos en el buffer. Un 0 significa que no se encontraron datos válidos.
Serial.readBytesUntil() hereda de la clase Stream.
Sintaxis
Serial.readBytesUntil(character, buffer, length)
Parámetros
- character: el caracter a buscar(char)
- buffer: el buffer para almacenar los bytes (char[] o byte[])
- length: tel número de bytes a leer (int)
Retornos
- Byte
Ejemplo
const int BUFFER_SIZE = 100;
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.readBytesUntil('\n', buf, BUFFER_SIZE);
// prints the received data
Serial.print("I received: ");
for(int i = 0; i < rlen; i++)
Serial.print(buf[i]);
}
}
- Select Newline at the ending selection of Serial Monitor
- Type "HELLO" on Serial Monitor
- Click Send button:
COM6
Autoscroll
Clear output
9600 baud
Newline
- The result on Serial Monitor:
COM6
I received: HELLO
Autoscroll
Clear output
9600 baud
Newline
※ 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.
Additionally, some links direct to products from our own brand, DIYables.