# How does Serial.readString() manage the serial buffer?

**URL:** https://discourse.processing.org/t/how-does-serial-readstring-manage-the-serial-buffer/8206
**Category:** Electronics (Arduino, etc.)
**Created:** [February 8, 2019, 2:37am UTC](https://discourse.processing.org/t/how-does-serial-readstring-manage-the-serial-buffer/8206 "2019-02-08T02:37:04Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Wangmaster](https://avatars.discourse-cdn.com/v4/letter/w/e56c9b/32.png) [@Wangmaster](https://discourse.processing.org/u/Wangmaster)
#### Post date: [February 8, 2019, 2:37am UTC](https://discourse.processing.org/t/how-does-serial-readstring-manage-the-serial-buffer/8206/1 "2019-02-08T02:37:04Z")

</div>

I want to use Processing to read serial data from my Arduino, but first i want to understand how it interacts with the serial buffer.

According to the documentation, Serial.readString() returns all information in the serial buffer. But how does it recognize when to stop reading? For example, in the code below:

```auto
import processing.serial.*;

Serial myPort; // The serial port

void setup() {
  // List all the available serial ports:
  printArray(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
  while (myPort.available() > 0) {
    String inBuffer = myPort.readString();   
    if (inBuffer != null) {
      println(inBuffer);
    }
  }
}

```

If I continuously send the string “Hello” from my arduino, my output in Processing is as such:

```auto
Hello

Hello

Hello

```

My question is, how does Serial.readString() know to wait for the full string before println()? Why does it not println char by char, as soon as the first “H” is received, as is expected when I print to the serial monitor in Arduino?

Please bear with me as i’m completely new to Processing, thanks.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [February 8, 2019, 2:51am UTC](https://discourse.processing.org/t/how-does-serial-readstring-manage-the-serial-buffer/8206/2 "2019-02-08T02:51:09Z")

</div>

there is a wide range:  
from read byte

```auto
  if ( myPort.available() > 0) { // If data is available,
    val = myPort.read(); // read it and store it in val
  }

```

to handle full lines

```auto
void serialEvent(Serial myPort) { // if want to see what the arduino is answering:
  String arduinoline = myPort.readStringUntil(10);
  if ( arduinoline != null ) print(arduinoline);
}

```

[https://processing.org/reference/libraries/serial/Serial\_readStringUntil\_.html](https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html)  
[https://processing.org/reference/libraries/serial/serialEvent\_.html](https://processing.org/reference/libraries/serial/serialEvent_.html)

---

<div class="post-metadata">

### Author: ![Wangmaster](https://avatars.discourse-cdn.com/v4/letter/w/e56c9b/32.png) [@Wangmaster](https://discourse.processing.org/u/Wangmaster)
#### Post date: [February 8, 2019, 3:04am UTC](https://discourse.processing.org/t/how-does-serial-readstring-manage-the-serial-buffer/8206/3 "2019-02-08T03:04:13Z")

</div>

I see there are serial methods of controlling how the buffer is read, thanks for the resources.

I’m still curious how serial.readString() knows when to terminate the string? Having a function which recognizes the end of a character stream sounds useful but I want to know how that works. Does it rely on a delay after the serial stream? If so how much is the delay?

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [February 8, 2019, 3:22am UTC](https://discourse.processing.org/t/how-does-serial-readstring-manage-the-serial-buffer/8206/4 "2019-02-08T03:22:28Z")

</div>

readString does not terminate unless you tell it to wait by

> myPort.bufferUntil(lf);

if you call it from draw it will give you that part what is available now

if you call it inside event  
it might actually read bytes when they come in,  
so there is the buffering recommended when strings are wanted.

also that depends on the system , used baud rate… v.s. sketch speed…

why you not show a screen shot what is the incoming  
like from arduino IDE monitor ( terminal )  
and tell more what you want to do.

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [February 8, 2019, 4:11am UTC](https://discourse.processing.org/t/how-does-serial-readstring-manage-the-serial-buffer/8206/5 "2019-02-08T04:11:32Z")

</div>

I am not sure if you are asking about how to use the available functions to read data immediately as it arrives or if you want to explore the details of the operation done by the Processing Serial library. If it is the second, you can always explore the source code available in [Github](https://github.com/processing/processing/blob/master/java/libraries/serial/src/processing/serial/Serial.java#L538). You can study the `SerialEvent()` as it shows how the handling of streaming data is done.

The part you will need to understand is mainly within the body of that specific function. However, before you go into this part of the source code, you need to understand [readStringUntil()](https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html) as in when to use it and how to use it with the [available functions](https://processing.org/reference/libraries/serial/index.html).

A challenge for you, if you are interested, is to use a [Thread](https://en.wikipedia.org/wiki/Thread_(computing)) to simulate incoming serial data.

Kf
