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
    Send
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  
  • The result on Serial Monitor:
  • COM6
    Send
    I received: HELLO
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

※ Nota:

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)

Ver También

ARDUINO BUY RECOMMENDATION

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

※ OUR MESSAGES

  • We are AVAILABLE for HIRE. See how to hire us to build your project
  • Any suggestion, correction, and translation? please email us at ArduinoGetStarted@gmail.com, We appreciate it
  • We mainly keep improving the references in English. See English version of this page for the latest update.