# How To Test for Lost Serial Port?

**URL:** <https://discourse.processing.org/t/how-to-test-for-lost-serial-port/15437>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [November 12, 2019, 9:41pm UTC](https://discourse.processing.org/t/how-to-test-for-lost-serial-port/15437 "2019-11-12T21:41:03Z")\
**Posts on this page:** 1\
**Showing post:** 16

<div class="post-metadata">

**Author:** ![Ghostsss](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ghostsss/32/16483_2.png) [@Ghostsss](https://discourse.processing.org/u/Ghostsss)\
**Post date:** [July 14, 2022, 11:59am UTC](https://discourse.processing.org/t/how-to-test-for-lost-serial-port/15437/16 "2022-07-14T11:59:41Z")

</div>

I managed to get it to work. (based on info from previous comments)

Here is the code for anyone to refer in future.

```auto
import processing.serial.*;

Serial myPort;
String currentPort;

void setup(){
  myPort = new Serial(this, Serial.list()[1], 9600);
  currentPort=Serial.list()[1]; // Save current port
}

void draw(){
  if(serialPortCheck()==true){
    println("com port found");
  }else{
    println("com port lost");
  }
  serialReadData();
}

boolean serialPortCheck(){
  boolean connected = false;
  String[] str = Serial.list(); // Get the current list of available ports
  for(int i=0; i<str.length; i++){
    //println("com:"+currentPort+" str[i]:"+str[i]);
    if(currentPort.equals(str[i])==true){ //check if currentPort is still in the list of comPorts
      connected=true; // com port found so still connected                   
      break;
    }
  }  
  return connected;
}

void serialReadData(){
  if(myPort.available()>0){
    println(myPort.readString());
  }
}

```

---

_[View the full topic](https://discourse.processing.org/t/how-to-test-for-lost-serial-port/15437)._
