# Serial does not recognize disconnect

**URL:** <https://discourse.processing.org/t/serial-does-not-recognize-disconnect/41770>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [April 19, 2023, 6:24pm UTC](https://discourse.processing.org/t/serial-does-not-recognize-disconnect/41770 "2023-04-19T18:24:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimeLex](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/timelex/32/12869_2.png) [@TimeLex](https://discourse.processing.org/u/TimeLex)\
**Post date:** [April 19, 2023, 6:24pm UTC](https://discourse.processing.org/t/serial-does-not-recognize-disconnect/41770/1 "2023-04-19T18:24:59Z")

</div>

Hi!  
I was doing some work with the `processing.Serial` library and ran into a problem with `serial.active()`. When I unplug the USB cable to my Arduino while the program is running, `serial.active()` still returns `true`. Since there is nothing in the [javadoc to `processing.Serial` about `Serial.active()`](https://processing.github.io/processing-javadocs/libraries/processing/serial/Serial.html#active--), I can’t be sure if it should even return `false` in this case. So I also tried to check if in `Serial.list()` the serial port of the Arduino (e.g. `COM4`) disappears, but strangely it was shown even after I disconnected the Arduino.  
Do I have to do something different, is this a bug or is it just not possible with the processing library for Serial?

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [April 21, 2023, 12:01pm UTC](https://discourse.processing.org/t/serial-does-not-recognize-disconnect/41770/2 "2023-04-21T12:01:50Z")

</div>

Hello @TimeLex, I’ve just tried what you say, Serial.Active() remained true even after the Arduino was unplugged. Not very useful.

I guess you are trying to make your Processing sketch tolerate the Ard being missing/connected/disconnected/reconnected. Two (at least) annoyances: (This is for Windows.)

1. If your sketch (or putty, or any other app), is connected to the port, and you disconnect, the sketch must myPort.stop() before you reconnect. If you don’t do that the port is unusable until you physically disconnect, programmatically disconnect, start again.

2. Some cheap Chinese Ard have different port no. in different usb sockets.

I’ve had some success at making sketches tolerate Ard disconnect/reconnect but annoyingly I can’t find a simple example today. It’s something like:

- sketch recognises no data arriving.
- myPort.stop()
- repeat:
  - wait a few seconds
  - if the portname is in the Serial.list, reconnect to it.

I was never able to make it tolerate a very brief disconnect/reconnect where the reconnect is before the sketch has detected the failure.

Hope this helps.
