Processing COM port scanner

Hi everyone,

I am trying to make processing detect an arduino automatically and use it in my sketch. I have many other COM ports in use so my arduino would initially be sending a constant "H" character. The processing code should be able to go through each COM port and detect which one is sending a "H" character.

hear is the code ive done so far (sorry i'm not a great programmer)

import processing.serial.*;

 String [] PortList;
 String[] ArduinoData;
 String input;
 String[] serialString ;
Serial  myPort;
Serial realport;
int found = 0;
int port_number = 0;
int port_live ;
String test;
void setup()  { 
  size(800, 450, P3D); 
  noStroke(); 
  colorMode(RGB, 1); 
//   println(Serial.list());
while (found==0){
  println(Serial.list());  
  myPort = new Serial(this, Serial.list()[port_number],9600 );

   findSerialPort();
   println("im looping");
    println(found);
   
} 
println("i left the loop");
myPort = new Serial(this, Serial.list()[port_number],9600 );

//myPort.clear(); 
 // Open the port that the Arduino board is connected to (in this case #0)
 // Make sure to open the port at the same speed Arduino is using (9600bps)

} 

void draw()  { 
  background(0.5);

text(input, 50, 30); 
  
 //  text(input,100,100) ;

//   rect( 10,  10, 30, 30);
} 
void serialEvent(Serial myPort) {
   input = myPort.readString();
   test = input;
  println(test);

   
}

 int findSerialPort() {
  
//  serialString = Serial.list();  
//   
//  println(serialString);   
   
//myPort.clear(); 
//input = myPort.readString();
//delay(200);
//println(input);

if (test == "H")
{ found = 1;
println("found it");
port_live = port_number;

}
else {
  delay(50);
  port_number ++;
println(port_number);
println(Serial.list()[port_number]);
}
return found;
//  myPort = new Serial(this, Serial.list()[port_number],9600 );
}

when i run this code, processing goes through all the ports, but for some reason is unable to quit the while loop which causes an "array index out of bound exception" error (because the port number exceeds the physical ports available"

Also, i know the "H" character is reaching processing because it is printed a couple of times before the error occurs.

I'd really appreciate some help on this matter.

Thank you

findSerialPort() returns a value. The call in setup() ignores the return value. Why?

You know how many ports there are in the array. Why would you walk past the end of the list?

As well as what @PaulS says ...

When your PC opens the serial port the Arduino will reset and it will be a few seconds (8 or 10 maybe) before it can send anything. Is your Processing program allowing for that?

Also, when your PC program does find the Arduino it is important that it keeps that serial port open.

...R

Hi PaulS, isn't the return value used as thecondition for the while loop? so if the returned found value changes, shouldn't the program quit the while loop and continue with the rest of the setup function?