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

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

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, to try to programmatically find the correct serial port to connect to.


int var;

void setup() {

void loop() {
  if (Serial.available()) {
    var =;


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


  // 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()
  // 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])) {
          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 :slight_smile:


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

  if (device_detected && runonce) 
    text("Device detected:", 20, 110);
    textFont(fnt, 18);
    text(detected_port, 20, 150);
    ser_port = new Serial(this, detected_port, 9600);
    runonce = false;

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

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


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.