# Serial.list() does not update when connected port is removed

**URL:** <https://discourse.processing.org/t/serial-list-does-not-update-when-connected-port-is-removed/47224>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [September 25, 2025, 12:45am UTC](https://discourse.processing.org/t/serial-list-does-not-update-when-connected-port-is-removed/47224 "2025-09-25T00:45:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bonner45](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@bonner45](https://discourse.processing.org/u/bonner45)\
**Post date:** [September 25, 2025, 12:45am UTC](https://discourse.processing.org/t/serial-list-does-not-update-when-connected-port-is-removed/47224/1 "2025-09-25T00:45:23Z")

</div>

It appears that when you connect to a serial port and then remove the cable serial list does not update.

here is a test code.

```auto

import processing.serial.*;

Serial myPort; // Create object from Serial class

void setup() 
{
  size(200, 200);

  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  println(Serial.list());
  delay(500);
}

```

I found several related issues but did not get any answer.

> <https://github.com/processing/processing-website/issues/360>
>
> \### Issue description
> if conncted to a port say "COM5". if "COM5" is disconncte…d for any reason, the serial.list() will still show that "COM5" exists even if connction is stopped using serial.stop() after getting "COM5" device disconncted.
> 
> again, if conncted to port "COM5" for instance, if connection is stopped using serial.stop() and then if "COM5" device is disconnceted, serial.list() will then update its element and "COM5" isn't there anymore. which is a normal behavior.
> 
> test code to varify this issue
> \`import processing.serial.\*;
> Serial comPort;
> 
> void setup() {
> size(500, 400);
> surface.setTitle("TEST");    
> comPort = new Serial(this, "COM5", 115200);
> background(150);
> frameRate(60);
> }
> 
> void draw() {
> background(150);  
> println(Serial.list().length);  
> }\`
> 
> check for number of serial ports exists first and then try disconncting the device
> Serial.list().length will still show that port was not removed...
> 
> \### URL(s) of affected page(s)
> 
> 
> 
> \### Proposed fix

> [@Serial.list() not updating correctly](https://discourse.processing.org/t/serial-list-not-updating-correctly/38036):
>
> I have 2 original arduino nano every boards. If I run following code (CODE1): import processing.serial.\*; Serial myPort; void setup(){ } void draw(){ println(frameCount); // just to see that code is running printArray(Serial.list()); } Then when I connect a Arduino board, a COM port appears in the print section. And If I disconnect an Arduino board (removing usb cable) the COM port disappears in the print section. And in windows device manager also, the board appears and disappears c…

I don’t know if this is a processing issue or if it’s just the way it is (i know unplugging a connected serial port is definitely not recommended) 😎

---

<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:** [September 25, 2025, 9:05pm UTC](https://discourse.processing.org/t/serial-list-does-not-update-when-connected-port-is-removed/47224/2 "2025-09-25T21:05:08Z")

</div>

Hello,

This code was written collaboratively with Google Gemini.

It uses this library:  
_[jSerialComm](https://fazecast.github.io/jSerialComm/)_

You must properly add the JAR file to your sketch:

_[Libraries - Happy Coding](https://happycoding.io/tutorials/processing/libraries)_

Environment:

- W10
- Processing 4.4.7

This was tested and will detect a disconnect:

```processing
// This sketch uses the jSerialComm library to detect when a serial device
// is connected or disconnected. It displays the connection status on the screen.

// Requires the jSerialComm-2.11.2.jar file from here:
// https://fazecast.github.io/jSerialComm/

import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortEvent;
import com.fazecast.jSerialComm.SerialPortDataListener;

// Variables for the serial port object
private SerialPort serialPort;

// Standard Processing setup function
public void setup() {
    size(400, 300, P2D);
    surface.setTitle("Serial Disconnect Detector");

    // Find and select the first available port
    SerialPort[] commPorts = SerialPort.getCommPorts();
    if (commPorts.length == 0) {
        System.out.println("No serial ports found.");
        return;
    }

    // Select the first port found
    serialPort = commPorts[0];

    // Configure and open the port
    serialPort.setBaudRate(9600);
    if (!serialPort.openPort()) {
        serialPort = null;
        return;
    }

    // Add an event listener to detect disconnection
    serialPort.addDataListener(new SerialPortDataListener() {
      @Override
      public int getListeningEvents() {
        return SerialPort.LISTENING_EVENT_PORT_DISCONNECTED;
      }
      
      @Override
      public void serialEvent(SerialPortEvent event) {
        if (event.getEventType() == SerialPort.LISTENING_EVENT_PORT_DISCONNECTED) {
            System.out.println("Port " + event.getSerialPort().getSystemPortName() + " WAS DISCONNECTED.");
            // Clean up: set the port variable to null
            serialPort = null;
        }
      }
    });
}

// Standard Processing draw function. This loop updates the screen.
public void draw() {
    background(240);
    textAlign(CENTER, CENTER);
    if (serialPort != null && serialPort.isOpen()) {
        fill(0, 150, 0);
        textSize(24);
        text("Connected", width/2, height/2);
    } else {
        fill(150, 0, 0);
        textSize(24);
        text("Disconnected", width/2, height/2);
    }
}

```

I did not explore beyond this.

I have used this library in the past for serial communications.

`:)`

---

<div class="post-metadata">

**Author:** ![bonner45](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@bonner45](https://discourse.processing.org/u/bonner45)\
**Post date:** [September 26, 2025, 1:38am UTC](https://discourse.processing.org/t/serial-list-does-not-update-when-connected-port-is-removed/47224/3 "2025-09-26T01:38:22Z")

</div>

Thanks @glv. That’s what i was looking for. I was testing that library but had not gotten that far.
