How To Test for Lost Serial Port?

I am using the processing serial library. In processing I am monitoring serial data. I want to detect when a USB device which identifies on a Windows computer as a COM port is turned off and therefor the COM port goes away.

I found a method in the documentation I think should do it but no joy.
active()
Return true if this port is still active and hasn’t run into any trouble.

Below is my code.
Would like some help.

/* Trying to use Serial.active()
 *I would like to detect loss of COM port like when a USB to serial adapter is disconnected.
 *I read about the active method here:
 *https://processing.github.io/processing-javadocs/libraries/processing/serial/Serial.html
 * However I can not get it to work. I run the sketch below and disconnect the USB adatpers 
 * and do not see the message when the port is not active.
 *If I disconnect the USB adapter before running the sketch I get Runtime
*/

import processing.serial.*;
Serial myPort;    // The serial port
String inString = "Not initilized" ;  // Input string from serial port
int cr = 13;      // ASCII carrage return 

void setup() { 
size(400,200); 
//Setup Serial input
  // List all the available serial ports: 
  printArray(Serial.list());  //Change the port number for your instrument.
  myPort = new Serial(this, Serial.list()[1], 115200); //COM30. 
  myPort.bufferUntil(cr);  //ST365 data is CR delimited.
}//setup

void draw(){
  if(myPort.active() != true){
   println("You can't have that port"); 
  }  
}//draw

/*Event handlers*/
//The serial port receiver. 
void serialEvent(Serial p) { 
  inString = p.readString();
  println(inString);
}// Serial receive 

I run the code and disconnect the USB COM port and never see the expected message “You can’t have that port”
I am running Processing 3.5.3 on a Windows 7 computer.

1 Like

You can try using Serial.list to see all available ports. And just check wether one disconnects/disappears if you unplug the USB.

Hello Lexyth,

Thanks for trying to help.

I probably should have phrased my question differently. I would like help understanding why the method “myPort.active()” did not work. Perhaps I have an error of understanding or the processing library only has a stub implementation that always returns “true” or ???

I want to programmatically test if the port to which mySerial was set at creation has become inactive (USB COM port disconnected).

I took Lexyth’s advise and wrote a function to test for the lost port.
The function is called in draw so I am polling for loss of port at the draw rate.

Code:

/* Trying to use Serial.active()
 *I would like to detect loss of COM port like when a USB to serial adapter is disconnected.
 *I read about the active method here:
 *https://processing.github.io/processing-javadocs/libraries/processing/serial/Serial.html
 * However I can not get it to work. I run the sketch below and disconnect the USB adatpers 
 * and do not see the message when the port is not active.
 *If I disconnect the USB adapter before running the sketch I get Runtime
 *question asked: https://discourse.processing.org/t/how-to-test-for-lost-serial-port/15437
 *20191114 Implimented function to read port list to see if port is gone.  Remove magic number for port.
*/

import processing.serial.*;
Serial myPort;    // The serial port
String myPortName = ""; 
String inString = "Not initilized" ;  // Input string from serial port
int cr = 13;      // ASCII carrage return 
int myPortIndex = 1; //Set for your desired port.

void setup() { 
size(400,200); 
//Setup Serial input
  // List all the available serial ports: 
  printArray(Serial.list());  //Print the current port numbers and names for your computer.
  myPortName = Serial.list()[myPortIndex];
  
  myPort = new Serial(this, myPortName, 115200); //Open the indexed port. 
  myPort.bufferUntil(cr);  //ST365 data is CR delimited.
}//setup

void draw(){
  background(0);
//  if(myPort.active() != true){
  if(isPortActive(myPortName) == true){
    text("Port is: "+ myPortName, 10, 10);
  }else {
    text("Your port is gone.", 10, 10); 
  }
}//draw

/*Event handlers*/
//The serial port receiver. 
void serialEvent(Serial p) { 
  inString = p.readString();
  println(inString);
}// Serial receive 

/* Functions */
boolean isPortActive(String aPort){
  String[] m1 = match(aPort, Serial.list()[myPortIndex]);  
  if (m1 != null ){
    return (true);
  }else{
    return(false);
  } 
}//isPortActive
2 Likes

I should have written, “I want an event to tell me if the port has become inactive (USB COM port disconnected).”

I though that function was a Solution, so i didn‘t write further :sweat_smile:

Anyway, to get an Event from when a Port is disconnected is apparently not a part of the Serial library…

And as for getting one yourself… it probably requires you to get it from the System directly, which is very annoying…(struggeled a lot to get an Event from the System and still failed after days…).

But maybe someone Else is more experienced with that…

As for getting an Event, you could just continuisly check if the Port changes and then trigger an Event yourself. That‘s a lot easier :sweat_smile:

You could just add a method in the Code you posted above, that is called when the isPortActive method returns false.

void portDeactivated () {
//put the Code you want executed in this case here. 
}
1 Like

Here is the Processing Serial.active() implementation:

which looks like it is a getter for a property on this JSSC SerialPort object: