# Serial Library: Clarity needed on SerialEvent() comment

**URL:** <https://discourse.processing.org/t/serial-library-clarity-needed-on-serialevent-comment/45663>\
**Category:** Libraries\
**Created:** [January 26, 2025, 10:01pm UTC](https://discourse.processing.org/t/serial-library-clarity-needed-on-serialevent-comment/45663 "2025-01-26T22:01:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 26, 2025, 10:01pm UTC](https://discourse.processing.org/t/serial-library-clarity-needed-on-serialevent-comment/45663/1 "2025-01-26T22:01:36Z")

</div>

Hello folks!

Can someone help provide clarity on a comment in the Serial library?

The comment is here:  
_[processing/java/libraries/serial/src/processing/serial/Serial.java at master · processing/processing · GitHub](https://github.com/processing/processing/blob/master/java/libraries/serial/src/processing/serial/Serial.java#L564C1-L570C44)_

and states:

> // serialEvent() is invoked in the context of the current (serial) thread  
> // which means that serialization and atomic variables need to be used to  
> // guarantee reliable operation (and better not draw() etc…)  
> // serialAvailable() does not provide any real benefits over using  
> // available() and read() inside draw - but this function has no  
> // thread-safety issues since it’s being invoked during pre in the context  
> // of the Processing applet

What does that mean?

I can use _available()_ and _read()_ inside draw instead of _serialEvent()_ interchangeably in most cases.  
Does this suggest that that is safer to do so and avoids thread-safety issues?

`:)`

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [February 1, 2025, 1:05pm UTC](https://discourse.processing.org/t/serial-library-clarity-needed-on-serialevent-comment/45663/2 "2025-02-01T13:05:13Z")

</div>

hi glv,

may be i’m wrong but i understand it like that:

serialEvent() come from a thread, so one can be worry it is asynchronous and run in parallel to draw(), (eg writing to serial in draw() while some code read from serial at same time in serialEvent())

but as it is processed during pre (i guess pre-draw() ) we can’t mess with simultaneous access to serial port.

to me, it means you’re right you can use _available()_ and _read()_ inside draw instead of _serialEvent()_ interchangeably 🤔

(witch is a little bit strange, we can imagine a low framerate sketch but listening as fast as possible )

edit:  
not sure, i think this is true if you still check serialAvailable() in serialEvent()

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 1, 2025, 9:33pm UTC](https://discourse.processing.org/t/serial-library-clarity-needed-on-serialevent-comment/45663/3 "2025-02-01T21:33:05Z")

</div>

> [@glv](#):
>
> serialAvailable()

It is not clear to me what `serialAvailable()` is referring to.

A search for _serialAvailable_ in the GitHub page comes up with:

```auto
Method serialAvailableMethod;
volatile boolean invokeSerialAvailable = false;
serialAvailableMethod = findCallback("serialAvailable");

```

```auto
serialEventMethod = findCallback("serialEvent");
serialAvailableMethod = findCallback("serialAvailable");

```

```auto
 public void pre() {
    if (serialAvailableMethod != null && invokeSerialAvailable) {
      invokeSerialAvailable = false;
      try {
        serialAvailableMethod.invoke(parent, this);
      } catch (Exception e) {
        System.err.println("Error, disabling serialAvailable() for "+port.getPortName());
        System.err.println(e.getLocalizedMessage());
        serialAvailableMethod = null;
      }
    }
  }

```

I also found this:  
_[P3D crash with serialEvent · Issue #5398 · processing/processing · GitHub](https://github.com/processing/processing/issues/5398)_

Which states and refers to my quote:

> We discussed this pitfall when the Serial library was re-written, see [here]  
> ([processing/java/libraries/serial/src/processing/serial/Serial.java at master · processing/processing · GitHub](https://github.com/processing/processing/blob/master/java/libraries/serial/src/processing/serial/Serial.java#L564)).

> [@MazeArt](https://github.com/MazeArt) Could you try changing `serialEvent` to (the undocumented) `serialAvailable`, and check if this works? (If it does, then this was indeed the issue, and you might want to then move all your code inside `draw` and make use of `available()`, instead of `serialEvent`).

Interesting!

`:)`

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 1, 2025, 10:26pm UTC](https://discourse.processing.org/t/serial-library-clarity-needed-on-serialevent-comment/45663/4 "2025-02-01T22:26:20Z")

</div>

> [@th75](#):
>
> (witch is a little bit strange, we can imagine a low framerate sketch but listening as fast as possible )

Everything is in the buffer so as long as you have a _while()_ in there you are good to go!

You may receive multiple _myStrings_ in that _while() loop_ so have to buffer and process on the receiving end also.

I did not have any issues with it as long as you can receive everything and process it in each frame.

```auto
void draw() {
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil(lf);
    // Your code
    }
  }
}

```

`:)`
