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

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.


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.

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) :smiling_face_with_sunglasses:

Hello,

This code was written collaboratively with Google Gemini.

It uses this library:
jSerialComm

You must properly add the JAR file to your sketch:

Libraries - Happy Coding

Environment:

  • W10
  • Processing 4.4.7

This was tested and will detect a disconnect:

// 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.

:)

1 Like

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

1 Like