COM Port Error: Port Not Found

I used a code here 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?

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.

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?

Edit: I linked a different hyperlink. This is the code I used, not the previous one.

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??

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

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.

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

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.

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()?

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

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

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.

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

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

to

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);
  }

Even with this code, the error still appears.

  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;}

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

/*--------------------------------------------------------------
  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);
    }
}

why did you put

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

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

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:

/*--------------------------------------------------------------
 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).