# COM Port Error: Port Not Found

**URL:** <https://discourse.processing.org/t/com-port-error-port-not-found/11504>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [May 22, 2019, 6:57pm UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504 "2019-05-22T18:57:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidbambalan](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@davidbambalan](https://discourse.processing.org/u/davidbambalan)\
**Post date:** [May 22, 2019, 6:57pm UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/1 "2019-05-22T18:57:50Z")

</div>

I used a code [here](http://startingelectronics.org/software/processing/find-arduino-port/) to detect on which port the Arduino is connected. However, I cannot run this code: `mySerial = new Serial(this, detected_port, 9600);` as it says `Error opening serial port COM#: Port not found`. How do I set `mySerial`?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 23, 2019, 12:52am UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/2 "2019-05-23T00:52:23Z")

</div>

at first i would make sure that your environment to test that ?old? code is working.

- did you run a manually coded port on that connected arduino?

- did you enable the usual  
//println(Serial.list());  
again, to see at the console what’s up.

* * *

a short test here gives the idea that that code is running  
( win7 / 64bit // processing 3.5.3 / Arduino Leonardo /)  
but can not be verified as it does not do much and has very poor feedback.  
( like for the button operation can not see if working or not )

but i did not see here the ERROR you mentioned.

---

<div class="post-metadata">

**Author:** ![davidbambalan](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@davidbambalan](https://discourse.processing.org/u/davidbambalan)\
**Post date:** [May 23, 2019, 1:05am UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/3 "2019-05-23T01:05:39Z")

</div>

I tried writing `mySerial = new Serial (this, detected_port, 9600)` in the `draw()` event as it should always update the serial port to which it reads. It shows that error. Can you show me your code?

---

<div class="post-metadata">

**Author:** ![davidbambalan](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@davidbambalan](https://discourse.processing.org/u/davidbambalan)\
**Post date:** [May 23, 2019, 1:08am UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/4 "2019-05-23T01:08:36Z")

</div>

Edit: I linked a different hyperlink. This is the [code](http://startingelectronics.org/software/processing/find-arduino-port/) I used, not the previous one.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 23, 2019, 1:09am UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/5 "2019-05-23T01:09:39Z")

</div>

i used the copy from that website,  
but i not understand your test?  
did you try to execute that mentioned line  
from inside draw() ?  
60 times per sec ?  
if it is open it can be opened again i suppose??

---

<div class="post-metadata">

**Author:** ![davidbambalan](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@davidbambalan](https://discourse.processing.org/u/davidbambalan)\
**Post date:** [May 23, 2019, 1:10am UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/6 "2019-05-23T01:10:25Z")

</div>

Please check my edit, I linked a different page, sorry.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 23, 2019, 1:13am UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/7 "2019-05-23T01:13:53Z")

</div>

no, i will not check,  
i asked you to first

- print the COM ports
- make a standard serial connection ( manually /hard coded/ selected port index )
- and use your arduino ( like print all lines the arduino is sending… )

after that can try that “enhanced” graphic port operation idea.

---

<div class="post-metadata">

**Author:** ![davidbambalan](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@davidbambalan](https://discourse.processing.org/u/davidbambalan)\
**Post date:** [May 23, 2019, 1:20am UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/9 "2019-05-23T01:20:12Z")

</div>

So, I’ll use the enhanced graphic port solution. Since the aforementioned solutions don’t work anyway.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 23, 2019, 1:24am UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/10 "2019-05-23T01:24:20Z")

</div>

i not understand the logic in your last post,

- if the print / select manually a port / declare the serial connection / use it to get the info arduino is sending /

does NOT work, the graphic operation can also not work??

* * *

my argumentation is that you need to make sure that arduino / usb connection / library…  
all works as should,  
and the graphic port select can only be the second step.

---

<div class="post-metadata">

**Author:** ![davidbambalan](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@davidbambalan](https://discourse.processing.org/u/davidbambalan)\
**Post date:** [May 23, 2019, 1:27am UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/11 "2019-05-23T01:27:03Z")

</div>

Apparently, I cannot place the `mySerial = new Serial(this, COM#, 9600);` in the `draw()` event as it becomes an error, even hard-coding it. Maybe it only works in `setup()`?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 23, 2019, 1:31am UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/12 "2019-05-23T01:31:01Z")

</div>

you can not execute it more than one time, and no way 60 times per second…  
( but that all has nothing to do with the operation code … )  
so please go back to the simplest example for a arduino connection  
and start from there.  
[https://processing.org/reference/libraries/serial/index.html](https://processing.org/reference/libraries/serial/index.html)

---

<div class="post-metadata">

**Author:** ![davidbambalan](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@davidbambalan](https://discourse.processing.org/u/davidbambalan)\
**Post date:** [May 23, 2019, 1:33am UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/13 "2019-05-23T01:33:14Z")

</div>

But is there an event that I can call whenever an Arduino connects via USB or when it disconnects?

---

<div class="post-metadata">

**Author:** ![Whahahahaha](https://avatars.discourse-cdn.com/v4/letter/w/3be4f8/32.png) [@Whahahahaha](https://discourse.processing.org/u/Whahahahaha)\
**Post date:** [May 23, 2019, 11:02am UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/14 "2019-05-23T11:02:15Z")

</div>

you can use the example you linked above, but you have to put

> mySerial = new Serial(this, detected\_port, 9600);

inside the if-statement at the end of the code, and not separate in the draw() loop.  
the last if-statement will only run if a new device actually has been found.

---

<div class="post-metadata">

**Author:** ![Whahahahaha](https://avatars.discourse-cdn.com/v4/letter/w/3be4f8/32.png) [@Whahahahaha](https://discourse.processing.org/u/Whahahahaha)\
**Post date:** [May 23, 2019, 11:08am UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/15 "2019-05-23T11:08:30Z")

</div>

actually, another addition you need is a check if you’ve already initialised the com-device (Arduino).  
you can do this by changing

```auto
if (device_detected) {
    text("Device detected:", 20, 110);
    textFont(fnt, 18);
    text(detected_port, 20, 150);
  }

```

to

```auto
if (device_detected && ser_port /*(or MySerial)*/ == null) {
    text("Device detected:", 20, 110);
    textFont(fnt, 18);
    text(detected_port, 20, 150);
    Ser_port /*or MySerial*/= new Serial(this,detected_port,9600);
  }

```

---

<div class="post-metadata">

**Author:** ![davidbambalan](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@davidbambalan](https://discourse.processing.org/u/davidbambalan)\
**Post date:** [May 23, 2019, 11:39am UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/16 "2019-05-23T11:39:33Z")

</div>

Even with this code, the error still appears.

```auto
  if ((Serial.list().length > num_ports) && !device_detected)
  {
    device_detected = true;
    boolean str_match = false;
    if (num_ports == 0) {detected_port = Serial.list()[0];}
    else
    {
      for (int i = 0; i < Serial.list().length; i++)
      {
        for (int j = 0; j < num_ports; j++)
        {
          if (Serial.list()[i].equals(port_list[j])) {break;}
          if (j == (num_ports - 1))
          {
            str_match = true;
            detected_port = Serial.list()[i];
          }
        }
      }
    }
    mySerial = new Serial(this, detected_port, 9600);
  }
  else if ((Serial.list().length == 0)){device_detected = false;}

```

---

<div class="post-metadata">

**Author:** ![Whahahahaha](https://avatars.discourse-cdn.com/v4/letter/w/3be4f8/32.png) [@Whahahahaha](https://discourse.processing.org/u/Whahahahaha)\
**Post date:** [May 23, 2019, 12:28pm UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/17 "2019-05-23T12:28:38Z")

</div>

can you please post all the code you’re using right now?

---

<div class="post-metadata">

**Author:** ![davidbambalan](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@davidbambalan](https://discourse.processing.org/u/davidbambalan)\
**Post date:** [May 23, 2019, 12:39pm UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/18 "2019-05-23T12:39:47Z")

</div>

```auto
/*--------------------------------------------------------------
  Program: port_find

  Description:   
                
  Date: 1 November 2012
 
  Author: W.A. Smith, http://startingelectronics.org
--------------------------------------------------------------*/
import processing.serial.*;

Serial ser_port; // for serial port
PFont fnt; // for font
int num_ports;
boolean device_detected = false;
String[] port_list;
String detected_port = "";

void setup() {
    size(400, 200); // size of application window
    background(0); // black background
    fnt = createFont("Arial", 16, true); // font displayed in window
    
    println(Serial.list());
    
    // get the number of detected serial ports
    num_ports = Serial.list().length;
    // save the current list of serial ports
    port_list = new String[num_ports];
    for (int i = 0; i < num_ports; i++) {
        port_list[i] = Serial.list()[i];
    }
}

void draw()
{
    background(0);
    // display instructions to user
    textFont(fnt, 14);
    text("1. Arduino or serial device must be unplugged.", 20, 30);
    text(" (unplug device and restart this application if not)", 20, 50);
    text("2. Plug the Arduino or serial device into a USB port.", 20, 80);
    
    // see if Arduino or serial device was plugged in
    if ((Serial.list().length > num_ports) && !device_detected) {
        device_detected = true;
        // determine which port the device was plugge into
        boolean str_match = false;
        if (num_ports == 0) {
            detected_port = Serial.list()[0];
        }
        else {
            // go through the current port list
            for (int i = 0; i < Serial.list().length; i++) {
                // go through the saved port list
                for (int j = 0; j < num_ports; j++) {
                    if (Serial.list()[i].equals(port_list[j])) {
                        break;
                    }
                    if (j == (num_ports - 1)) {
                        str_match = true;
                        detected_port = Serial.list()[i];
                    }
                }
            }
        }
        ser_port = new Serial(this, detected_port, 9600);
    }
    // calculate and display serial port name
    if (device_detected && ser_port == null) {
        text("Device detected:", 20, 110);
        textFont(fnt, 18);
        text(detected_port, 20, 150);
        ser_port = new Serial(this, detected_port, 9600);
    }
}

```

---

<div class="post-metadata">

**Author:** ![Whahahahaha](https://avatars.discourse-cdn.com/v4/letter/w/3be4f8/32.png) [@Whahahahaha](https://discourse.processing.org/u/Whahahahaha)\
**Post date:** [May 23, 2019, 12:41pm UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/19 "2019-05-23T12:41:50Z")

</div>

why did you put

> [@davidbambalan](#):
>
> ser\_port = new Serial(this, detected\_port, 9600);

in there twice?  
the one in line 66 is causing the problem and is unnecessary.

---

<div class="post-metadata">

**Author:** ![davidbambalan](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@davidbambalan](https://discourse.processing.org/u/davidbambalan)\
**Post date:** [May 23, 2019, 2:16pm UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/20 "2019-05-23T14:16:00Z")

</div>

Oh my bad. But when I commented that out, it errors the second `ser_port = new Serial(this, detected_port, 9600);`.

---

<div class="post-metadata">

**Author:** ![Whahahahaha](https://avatars.discourse-cdn.com/v4/letter/w/3be4f8/32.png) [@Whahahahaha](https://discourse.processing.org/u/Whahahahaha)\
**Post date:** [May 24, 2019, 3:19pm UTC](https://discourse.processing.org/t/com-port-error-port-not-found/11504/21 "2019-05-24T15:19:25Z")

</div>

hm, that’s weird. I don’t have that problem…  
are you sure you can properly connect to the Arduino normally?  
like @kll said, at least try the simplest Arduino connection first.  
as a clarification, this is now the code’s using that works:

```auto
/*--------------------------------------------------------------
 Program: port_find
 
 Description:   
 
 Date: 1 November 2012
 
 Author: W.A. Smith, http://startingelectronics.org
 --------------------------------------------------------------*/
import processing.serial.*;

Serial ser_port; // for serial port
PFont fnt; // for font
int num_ports;
boolean device_detected = false;
String[] port_list;
String detected_port = "";
boolean connected = false;
void setup() {
  size(400, 200); // size of application window
  background(0); // black background
  fnt = createFont("Arial", 16, true); // font displayed in window

  println(Serial.list());

  // get the number of detected serial ports
  num_ports = Serial.list().length;
  // save the current list of serial ports
  port_list = new String[num_ports];
  for (int i = 0; i < num_ports; i++) {
    port_list[i] = Serial.list()[i];
  }
}

void draw()
{
  background(0);
  // display instructions to user
  textFont(fnt, 14);
  text("1. Arduino or serial device must be unplugged.", 20, 30);
  text(" (unplug device and restart this application if not)", 20, 50);
  text("2. Plug the Arduino or serial device into a USB port.", 20, 80);

  // see if Arduino or serial device was plugged in
  if ((Serial.list().length > num_ports) && !device_detected) {
    device_detected = true;
    // determine which port the device was plugge into
    boolean str_match = false;
    if (num_ports == 0) {
      detected_port = Serial.list()[0];
    } else {
      // go through the current port list
      for (int i = 0; i < Serial.list().length; i++) {
        // go through the saved port list
        for (int j = 0; j < num_ports; j++) {
          if (Serial.list()[i].equals(port_list[j])) {
            break;
          } else {
            str_match = true;
            detected_port = Serial.list()[i];
          }
        }
      }
    }
  }
  // calculate and display serial port name
  if (device_detected && ser_port == null) {

    ser_port = new Serial(this, detected_port, 9600);
    connected = true;
  }
  if (connected) {
    text("Device detected:", 20, 110);
    textFont(fnt, 18);
    text(detected_port, 20, 150);
  }
}

```

like the example says, start the sketch, then connect the Arduino (not the other way around).

[Next page](https://discourse.processing.org/t/com-port-error-port-not-found/11504.md?page=2)
