# Conditioning the sketch on finding the COM port trouble

**URL:** https://discourse.processing.org/t/conditioning-the-sketch-on-finding-the-com-port-trouble/28910
**Category:** Electronics (Arduino, etc.)
**Created:** [March 27, 2021, 7:21pm UTC](https://discourse.processing.org/t/conditioning-the-sketch-on-finding-the-com-port-trouble/28910 "2021-03-27T19:21:05Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [March 27, 2021, 7:21pm UTC](https://discourse.processing.org/t/conditioning-the-sketch-on-finding-the-com-port-trouble/28910/1 "2021-03-27T19:21:05Z")

</div>

I have a sketch that talks to an Arduino. I would like it to stop and warn me when the arduino is not present so I tried:

```auto
void setup() {
  size(1900, 950, JAVA2D);

  printArray(Serial.list());

  if (Serial.list().length > 0)
  {
    serialConnect =true;
    myPort = new Serial(this, Serial.list()[0], 115200);
    myPort.bufferUntil(lf); 
    //myPort = new Serial (this, "/dev/tty.wchusbserial5d10", 115200);
    myPort.clear();
    IncomingSTR= "";
  } else
  { 
    textSize(26); 
    text ("CONNECT RECEIVER"+"\n", 10, 40);
  
    delay (3000);
    exit();
    
  }

```

When the Arduino is connected the sketch is fine.  
When there is no ardu, I am still finding a COM port and the sketch gives me a bare screen and no warnings.  
In the print bottom I get

=======================================================  
[0] “COM27”  
RuntimeException: Error opening serial port COM27: Port not found

In device manager, the “Ports COM and LPT” is not even there… when there is no Arduino.  
So where the heck is processing finding a port?

Thanks  
Mitch

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [March 27, 2021, 7:50pm UTC](https://discourse.processing.org/t/conditioning-the-sketch-on-finding-the-com-port-trouble/28910/2 "2021-03-27T19:50:26Z")

</div>

I’m not sure in your case but some drivers create a virtual com port so maybe that’s what it is trying to open. If you connect an arduino, does it appear as COM27 or show up as another port?

A potential “fix” can be to use try/catch and see if you can actually open the port

```auto
import processing.serial.*;

void setup() {
  size(1900, 950, JAVA2D);
  printArray(Serial.list());

  try {
    Serial myPort = new Serial(this, "COM27", 115200);
    myPort.clear();
  } catch (Exception e) { 
    e.printStackTrace();
    delay (3000);
    exit();
  }
}

```

ps could you add all the imports and variable declarations when you post something like this, so we can run the sketch without editing? 🙂

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [March 27, 2021, 9:19pm UTC](https://discourse.processing.org/t/conditioning-the-sketch-on-finding-the-com-port-trouble/28910/3 "2021-03-27T21:19:39Z")

</div>

Thanks a lot friend. Yes the ardu goes on COM27 when connected.

Its my first time playing with “try” and I tried

```auto
import processing.serial.*;
Serial myPort;

int lf = 10; 

void setup() {
  size(1900, 950, JAVA2D);
  printArray(Serial.list());

  try {
    Serial myPort = new Serial(this, "COM27", 115200);
    myPort.clear();
  } catch (Exception e) { 
    e.printStackTrace();
    delay (3000);
    println("NoPORT");
    //exit();
  }
}

void draw() { 
  background(0);

    if (myPort.available() > 0) {
      String IncomingSTR = myPort.readStringUntil(lf);
      println (IncomingSTR);
    }
    
}

```

I am getting a null pointer when the Ardu is connected and when is not.

When is not I do get the printout println(“NoPORT”); but not the one above.

I would want not to get a null pointer and give the user a chance to connect the Ardu.

but the first problem, first:  
When the Ardu is connected, I get a null pointer and it gets stuck on  
if (myPort.available() \> 0) { line

The situation is quite messy because of windows. I found in device manager several “Ghosts” comm ports that get picked occasionally by processing as real ports…

![ghost_Ports](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/adbb72f3d114cc047e87c339e3eaa404e6ca764e.jpeg)

To be concise: I would love to write a condition in Setup that will connect the sketch to the only active port in windows. (This time Com27) and would ignore the rest.

Sometimes I get in the (Serial.list()) ports that are greyed out but still indexed as [0] and one… halting the sketch. Then I delete them in device manager and re-boot and then I pick the [0] in the sketch and it works fine…

Any advice, thank you,  
Mitch

Thanks  
Mitch

---

<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: [March 28, 2021, 12:20am UTC](https://discourse.processing.org/t/conditioning-the-sketch-on-finding-the-com-port-trouble/28910/4 "2021-03-28T00:20:33Z")

</div>

Hello,

There are many solutions to cleaning this up and starting fresh.

This is just one.

> **[How to Delete Or Clear COM Ports In Use - Device Manager Hidden](https://theitbros.com/how-to-delete-com-ports-in-use/)**
>
> In this article we will show you how to delete or clear COM port in use on Windows 10 machine

I used to run a script to do this.

`:)`

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [March 28, 2021, 1:41pm UTC](https://discourse.processing.org/t/conditioning-the-sketch-on-finding-the-com-port-trouble/28910/5 "2021-03-28T13:41:03Z")

</div>

> [@laptophead](#):
>
> I am getting a null pointer

It’s because `try - catch` failed and `myPort` remained empty. By the way I couldn’t get what you mean, but does the code run if the Arduino is connected, and crashes when it’s not connected?

If you want to give time for the user to connect USB, I suggest moving the initialization part to `draw` instead. Pseudo code would be

```auto
boolean isConnected = false;
void draw() {
  if(isConnected == false) {
    try {
      // serial connection
      ...
      if(success) {
        isConnected = true;
      }
      else {
        // delay
        ...
      }
    } catch (e) {
      // delay
      ...
    }
  }
  else {
    // do the usual process of serial communication and drawing
    ...
  }
}

```

and this gives flexibility to go back to the connection process once the Arduino is accidentally disconnected (in theory. I don’t know if it actually works like that, though). Also for where I wrote `delay`, it would be even better if you avoid `delay` function and check the elapsed time and say

```auto
if(currentTime - lastTime > 5000) {
  // do something after 5 sec passed
}

```

so that there is no interruption in the animation frame (you can display, like “reconnecting in 2 sec”

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [March 29, 2021, 7:01pm UTC](https://discourse.processing.org/t/conditioning-the-sketch-on-finding-the-com-port-trouble/28910/6 "2021-03-29T19:01:03Z")

</div>

I was saying I am getting a null pointer even when the Ardu was connected.

OK, I tried your code, but ‘success’ is not defined. Should we make a bool just for that?

Here

```auto
import processing.serial.*;
Serial myPort;
boolean isConnected = false;
int lf = 10; 

void setup() {
  size(1900, 950, JAVA2D);
  printArray(Serial.list());
  
}

void draw() { 
  background(0);

  if (isConnected == false) 
  {
    try {
      Serial myPort = new Serial(this, Serial.list()[0], 115200);
      myPort.clear();
      if (success) {
        isConnected = true;
      } else {
        delay (3000);
        println("NoPORT");
      }
    } 
    catch (Exception e) {
      delay (3000);
      println("NoPORT");
    }
  } else {
    if (myPort.available() > 0) {
      String IncomingSTR = myPort.readStringUntil(lf);
      println (IncomingSTR);
    }
  }
}

```

---

<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: [March 29, 2021, 11:45pm UTC](https://discourse.processing.org/t/conditioning-the-sketch-on-finding-the-com-port-trouble/28910/7 "2021-03-29T23:45:41Z")

</div>

> [@laptophead](#):
>
> `Serial myPort = new Serial(this, Serial.list()[0], 115200);`

Change above to:

```auto
myPort = new Serial(this, Serial.list()[0], 115200);

```

Reference:  
_[Variable Scope / Examples / Processing.org](https://processing.org/examples/variablescope.html)_

---

<div class="post-metadata">

### Author: ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)
#### Post date: [April 1, 2021, 9:47am UTC](https://discourse.processing.org/t/conditioning-the-sketch-on-finding-the-com-port-trouble/28910/8 "2021-04-01T09:47:09Z")

</div>

Hi @laptophead , Have you noticed that with the ch340 driver and chip the port number depends on which USB socket on the PC you have connected to? (The ftdi chip gives you the same number for the same Arduino whatever usb socket.) I know it’s annoying to adjust your actions to suit the equipment, but you could simplify things by always using the same usb socket.

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [April 3, 2021, 2:32am UTC](https://discourse.processing.org/t/conditioning-the-sketch-on-finding-the-com-port-trouble/28910/9 "2021-04-03T02:32:12Z")

</div>

Yes, Richard,  
Here is what I did,

Went in the device manager and made the Arduino COM port the lowest number among other devices.  
This way the [0] in serial list always gets picked.  
I rebooted several times and windows seems to keep my COM assignments.

Good enough. Keep life simple
