[SOLVED] Trouble with serial writing

Hello,

I'm having trouble with serial writing. Here is my code:

char buffer[10];

void setup() {
        Serial.begin(115200);
}

void loop() {
        if (Serial.available() > 0) {
                Serial.readBytesUntil('a',buffer,5);

                Serial.println(buffer);
        }
}

When I read, for example, "123a4" with the function readBytesUntil, I receive two messages: "123" and "423", ie, the Arduino is reading after the terminator and, for worse, it is keeping the buffer after reading until the terminator. What I want is something to read until the terminator and stop (and ensure that the buffer will be clean for the next reading)! I already tried to use the following statements, but it didn't work:

while (Serial.available ())
  Serial.read ();

Could someone help, please?

Perhaps you need to add code to clear the buffer between reads

if (Serial.available() > 0) {
                Serial.readBytesUntil('a',buffer,5);

                Serial.println(buffer);
                for (byte n = 0; n < 10; n++) {
                    buffer[n] = 0;
                 }
        }

...R

Robin2:
Perhaps you need to add code to clear the buffer between reads

if (Serial.available() > 0) {

Serial.readBytesUntil('a',buffer,5);

Serial.println(buffer);
                for (byte n = 0; n < 10; n++) {
                    buffer[n] = 0;
                 }
        }




...R

It did work for the buffer problem (thanks for that!), but I still have the problem with the reading after the terminator (now I have the messages "123" and "4" if I read "123a4").

The Serial method does read to the specified character, so you code is doing as it should, but not as you want. Try this mod:

   int bytesRead;
   char buffer[6];      // I misspelled this first time...

    if (Serial.available() > 0) {
      bytesRead = Serial.readBytesUntil('\n',buffer,5);
      buffer[bytesRead] = '\0';      // Make it a string
      Serial.println(buffer);
   }

Note that the code reads up to the newline character ('\n'), which is sent when the user clicks Enter or presses the Enter key. If you want to treat a char array as a string, you need to append a null termination character ('\0') to the end of the data. If you need to break out substrings, you can use indexof() and look for 'a'.

print and println expect null terminated c strings.

From the documentation.

Serial.readBytesUntil() returns the number of characters read into the buffer. A 0 means no valid data was found.

So a better fix is

void loop() {
        int len;
        if (Serial.available() > 0) {
                len=Serial.readBytesUntil('a',buffer,5);
                buffer[len]='\0'; // 0 is "null" in the context, bet i use the wrong  slash.
                Serial.println(buffer);
        }
}

Mark

You all helped me a lot. I used all you said and it works!

I just did this:

int bytesRead;
char buffer[10];
String strg;

void setup() {
        Serial.begin(115200);
}

void loop() {

  if (Serial.available() > 0) {
    bytesRead = Serial.readBytes(buffer,5);
    buffer[bytesRead] = '\0';
    strg = buffer;

    Serial.println(strg.substring(0,strg.indexOf('a')));
  }
}

Thanks a lot!