# Serial connection between Arduino and Processing doesn't work; Port busy

**URL:** <https://discourse.processing.org/t/serial-connection-between-arduino-and-processing-doesnt-work-port-busy/34040>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [December 10, 2021, 5:47pm UTC](https://discourse.processing.org/t/serial-connection-between-arduino-and-processing-doesnt-work-port-busy/34040 "2021-12-10T17:47:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![grudovz](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@grudovz](https://discourse.processing.org/u/grudovz)\
**Post date:** [December 10, 2021, 5:47pm UTC](https://discourse.processing.org/t/serial-connection-between-arduino-and-processing-doesnt-work-port-busy/34040/1 "2021-12-10T17:47:19Z")

</div>

Hi everyone. I’m trying to connect arduino and processing via serial. Whenever I run the processing sketch I get the following error:

```auto
RuntimeException: Error opening serial port COM26: Port busy
Could not run the sketch (Target VM failed to initialize).

```

Below is the arduino sketch and processing code, which I “borrowed” from [here](https://startingelectronics.org/software/processing/find-arduino-port/), to try to programmatically find the correct serial port to connect to.

Arduino:

```auto
int var;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    var = Serial.read();
  }
  Serial.println(var);
  delay(100);
}

```

Processing:

```auto
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 plugged 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];
          }
        }
      }
    }
  }
  // calculate and display serial port name
  if (device_detected) {
    text("Device detected:", 20, 110);
    textFont(fnt, 18);
    text(detected_port, 20, 150);
    ser_port = new Serial(this, detected_port, 9600);
  }
}

```

I am a newbie at this, so not sure what I’m doing wrong. Would appreciate any help 🙂

---

<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:** [December 10, 2021, 9:04pm UTC](https://discourse.processing.org/t/serial-connection-between-arduino-and-processing-doesnt-work-port-busy/34040/2 "2021-12-10T21:04:36Z")

</div>

Hello,

You are running this multiple times and it will be busy after the first time:

```auto
  if (device_detected && runonce) 
    {
    //delay(1000);
    text("Device detected:", 20, 110);
    textFont(fnt, 18);
    text(detected_port, 20, 150);
    println(detected_port);
    ser_port = new Serial(this, detected_port, 9600);
    runonce = false;
    printArray(Serial.list());
    }

```

I added a:  
`boolean runonce = true;`  
and it seems to work.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2c505f7ac7b3f785adeb4ba6931edca6e31e75f5.png)

You will have to modify code to display COM port in draw since this is only displayed once now.

`:)`

---

<div class="post-metadata">

**Author:** ![grudovz](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@grudovz](https://discourse.processing.org/u/grudovz)\
**Post date:** [December 11, 2021, 12:48pm UTC](https://discourse.processing.org/t/serial-connection-between-arduino-and-processing-doesnt-work-port-busy/34040/3 "2021-12-11T12:48:09Z")

</div>

Thank you for responding. I will test this code. Apologies but I have to post another question on the topic, since I seem to have a more fundamental issue and updating here might get confusing. Sorry if this isn’t the preferred way to do things on the forum.
