# How to check if com Port is already being used by another application?

**URL:** <https://discourse.processing.org/t/how-to-check-if-com-port-is-already-being-used-by-another-application/37946>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [July 14, 2022, 11:29am UTC](https://discourse.processing.org/t/how-to-check-if-com-port-is-already-being-used-by-another-application/37946 "2022-07-14T11:29:09Z")\
**Posts on this page:** 3\
**Page:** 1

<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:29am UTC](https://discourse.processing.org/t/how-to-check-if-com-port-is-already-being-used-by-another-application/37946/1 "2022-07-14T11:29:10Z")

</div>

How do I check if com Port is already being used by another application or if it is free to be used ?

```auto
import processing.serial.*;

Serial myPort;

void setup(){
  myPort = new Serial(this, Serial.list()[0], 9600); // How to check if this port is free or not ??
}

void draw(){
  serialPortCheck();
  serialReadData();
}

void serialPortCheck(){
  if(myPort.active()==false){
    println("Error in com Port");
  }
}

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

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [July 14, 2022, 2:17pm UTC](https://discourse.processing.org/t/how-to-check-if-com-port-is-already-being-used-by-another-application/37946/2 "2022-07-14T14:17:19Z")

</div>

Hi  
Port options

```auto
// display serial port list 
println(Serial.list());
printArray(Serial.list());
// get the first available port (use EITHER this OR the specific port code below)
String portName = Serial.list()[0];

// get a specific serial port (use EITHER this OR the first-available code above)
String portName = "COM5";

// open the serial port
port = new Serial(this, portName, 115200);

```

May in this links some hints if you looking for something else

> **[Find active serial port from processing - Processing Forum](https://forum.processing.org/one/topic/find-active-serial-port-from-processing.html)**
>
> Processing Forum

> [@How To Test for Lost Serial Port?](https://discourse.processing.org/t/how-to-test-for-lost-serial-port/15437/16):
>
> I managed to get it to work. (based on info from previous comments) Here is the code for anyone to refer in future. 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 = fals…

---

<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:** [July 15, 2022, 10:37am UTC](https://discourse.processing.org/t/how-to-check-if-com-port-is-already-being-used-by-another-application/37946/3 "2022-07-15T10:37:02Z")

</div>

Hello,

I am sharing some code I experimented with:

```auto
// Serial Port State
// v1.0.0
// GLV 2022-07-15

import processing.serial.*;

Serial myPort, myPort2;

void setup(){
  
  printArray(Serial.list());
  
  //myPort = new Serial(this, Serial.list()[4], 9600);
  //try
  // {
  // myPort2 = new Serial(this, Serial.list()[4], 9600);
  // }
  //catch(Exception e)
  // {
  // println(e);
  // }
  }

void draw()
  {
  }

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

void keyPressed()
  {
  if(key == 'c')
    {
    println("connect");  
    try
      {
      myPort2 = new Serial(this, Serial.list()[4], 9600);
      }
    catch(Exception e)
      {
      println(e);
      }
    }
    
  if(key == 's')
    {
    println("stop");  
    try
      {
      myPort2.stop();
      }
    catch(Exception e)
      {
      println(e);
      }
    }
  
  if(key == 'i')
    {
    println("info:"); 
    if (myPort2 != null)
      println(myPort2.active());
    }
  
  //if(key == 'n')
  // {
  // myPort2 = null;
  // println("null", myPort2);
  // }
    
  if (key == 'r')
    {
    if(myPort2.available()>0)
      {
      println(myPort2.readString());
      }
    }
    
  if(key == 'l')
    {
    printArray(Serial.list()); 
    }     
  }    

```

Using try\catch was useful to determine state of port as “Port busy” or “Port not found”.

I was able to disconnect the Arduino (unplug) and connect to it again (after plugging back in) and receive data in Processing sketch without restarting the sketch… that is a positive sign.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/e/9e197cdb9bffdcbc78494cda304baed7583bba46.png)

`:)`
