output problem

Hello there, I have some output problem:

If you type "hello" into the serial monitor, it should output "hello".
And then I type "hi!" into the serial monitor, it should output "hi!", but I have no idea why it shows "hi!lo". Btw null char and /n will not be stored in this program.

thanks!

  if (stringComplete)
  {
    {
      Serial.print("[buf] array size = ");
      Serial.println(i);
   
    
    Serial.print("your input: ");
    Serial.println(buf);
    }
    
    stringComplete = false;
    i = 0;
    
  }

image1.PNG

image1.PNG

In a second case you have just remaining from first "hello". Post your full code to get an advise how to fix it.

Btw null char and /n will not be stored in this program.

That's the reason; you need to add a null character after the received data. Or fill the buffer with null characters once you don't need the received data anymore.

You can do a search for Robin2's updated serial input basics tutorial to get some ideas.

The problem is in the code you didn't post.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data.

...R

Check out my tutorial Serial I/O for the Real World using SafeStrings for reading and parsing words without having to worry about un-terminated char[]