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:
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Ln 11, Col 1
Arduino Uno on COM15
2
  • The result on Serial Monitor:
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
I received: HELLO
Ln 11, Col 1
Arduino Uno on COM15
2

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