Serial.readBytesUntil()

Quick question regarding the terminator character when using Serial.readBytesUntil(): Does the terminator get appended to the buffer, or discarded?
Cheers.

Does the terminator get appended to the buffer, or discarded?

Wouldn't it have been faster to test it?

It would, alas I am in an arduino-less place right now with a bit of time to spare, and decided to tinker with a little bit of code.

I am in an arduino-less place right now with a bit of time to spare, and decided to tinker with a little bit of code.

The source code is available, wherever you are.

The until value is not added to the string returned (or removed from the buffer).

Cheers, much appreciated :slight_smile:

serial.readBytesUntil() is a blocking function and serial data arrives slowly by Arduino standards. The examples in serial input basics do not block.

...R

Quick question regarding the terminator character when using Serial.readBytesUntil(): Does the terminator get appended to the buffer, or discarded?

In a simple example like below, the comma delimiter is removed from the rx buffer when it is read and is not added to the captured character String. If two terminating bytes are used, one will be removed and the other will become a trash byte that has to be dealt with.

//zoomkat 3-5-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//send on, or off, from the serial monitor to operate LED

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial LED on/off test with , delimiter"); // so I can keep track
}

void loop() {

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      Serial.println(readString); //prints string to serial port out
      //do stuff with the captured readString
      if(readString.indexOf("on") >=0)
      {
        digitalWrite(ledPin, HIGH);
        Serial.println("LED ON");
      }
      if(readString.indexOf("off") >=0)
      {
        digitalWrite(ledPin, LOW);
        Serial.println("LED OFF");
      }       
      readString=""; //clears variable for new input
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}