Serial does not recognize disconnect

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(), 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?

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.

1 Like