Search code examples
arduinoprocessing

Processing "RuntimeException: Error opening serial port COM3: Port busy" with no Arduino IDE running


I am trying to connect Arduino with my processing program using serial connection, but I get this error when running the sketch.

In the end I want my program to automatically detect on which port is the Arduino connected, but first I wanted to try and communicate with it knowing the port? But I dindn't manage to do it.

Here is my Processing program:

import processing.serial.*;
Serial port;

void setup() {
  size(400,400);
}

void draw() {
    port = new Serial(this, "COM3", 115200);
     println(port);
     if(port.available() > 0) {
       println("yes");
       if(port.readStringUntil('\n').equals("test")) {
          println("y");
          port.write("test_nc");
        }
     }
}

And my Arduino program

boolean isConnected = false;
boolean lookForPC = true;

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

void loop() {
  if (lookForPC) {
    while (!isConnected) { //looking for pc serial connection
      Serial.println("test");
      delay(500);
      if (Serial.available() > 0) {
        if (Serial.readStringUntil('\n').equals("test_nc")) {
          Serial.println("ok");
          isConnected = true;
        }
      }
    }
  }
  Serial.println("done");
}

I have read that the problem may come from the arduino IDE listening to the Serial port at the same time but when Processing is the only program running I get the same error.

I've tried rebooting my pc and changing USB port for my Arduino, but I always get this error even when Arduino is not running.

Thank you.


Solution

  • I'm not certain that I understand what you are trying to do. The code that you posted makes no sense to me. Perhaps explain in words what you want to accomplish. In the meantime, please see if you can run this simple demo (substitute com port for your system). To avoid the 'busy' error I first uploaded the Arduino code and then closed the editor and switched to Processing. I believe that using a serialEvent() function is preferable to using draw() for serial events.

    Processing code:

    import processing.serial.*;
    
    Serial port;
    String inStr;  // Input string from serial port
    
    void setup() {
      surface.setVisible(false);
      printArray(Serial.list());
      port = new Serial(this, Serial.list()[3], 9600);
    }
    
    void draw() {
    }
    
    void serialEvent(Serial p) {
      inStr = p.readStringUntil('\n');
      if (inStr != null) {
        inStr = trim(inStr);
        println(inStr);
      }
    }
    

    Arduino code:

    void setup() {
     Serial.begin(9600);
    }
    
    void loop() {
      Serial.println("Processing is great.");
    }