0
votes

I'm having problems with Processing3, where when I attempt to connect to a certain COM port, the app crashes, with the Port Busy error.

Here's the code snippet:

boolean found = false;
text(Arrays.toString(Serial.list()), 20, 500);
println("Still checking...");
for (String port : Serial.list()) {
  myPort = new Serial(this, port);
  myPort.clear();
  myPort.write("init\n");
  if (myPort.readStringUntil(lf) == "connected") {
    found = true;
    break;
  }
}
if (!found) {
  myPort = emptyPort;
  text("Waiting for device...", 20, 40);
}

This is part of the draw() loop, the error is thrown at line 5 in this example. Until that particular port becomes available, everything else is running just fine.

This is the setup() code from the connected arduino:

Serial.begin(256000);
while (!Serial.available()) {}
while (true) {
  String recv = Serial.readStringUntil('\n');
  if (recv == "init") {
    Serial.println("connected");
    break;
  } else {
    while (!Serial.available()) {}
  }
}
Serial.println("600,400");

Testing from the Serial Monitor in Arduino IDE produces no such error.

2
Did you ever get this figured out?Kevin Workman
@KevinWorkman Oh yeah, I was waiting for SO to let me accept my own answer, and in the meantime, kinda forgot about it.. Moved onto other issues.Jan Novák

2 Answers

0
votes

This seems wrong:

for (String port : Serial.list()) {
  myPort = new Serial(this, port);

Here you're connecting to every serial port. You should probably just connect to one, right?

Also, you said you're doing this from the draw() function? That also seems wrong. You don't want to connect to the port 60 times per second. You want to connect to it once from the setup() function and then just use that connection for the rest of the program.

If I were you, I would go back and look at some code examples.

From the reference:

// Example by Tom Igoe

import processing.serial.*;

// The serial port:
Serial myPort;       

// List all the available serial ports:
printArray(Serial.list());

// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[0], 9600);

// Send a capital A out the serial port:
myPort.write(65);

This example just connects to the first COM port it finds, then sends a single value. Note that this code is "once and done" and isn't repeated, similar to what would happen if you put this all in the setup() function.

Get something simple like this working before trying to move onto something more complicated. If you get stuck, then post a MCVE instead of a bunch of disconnected snippets. Good luck.

0
votes

Instead of replacing myPort with emptyPort, you should close myPort and free up the COM port for repeated connection by doing myPort.stop().