String portNames = Serial.list();
int len = portNames.length ;
println("portNames has "+len+" elements");
for (int i=0 ; i<len ; i++ )
String portName4 = new String( portNames) ; // usual needs to be portNames for arduino
myPort = new Serial(this, portName4, 115200 );
This is what my code to open a serial port looks like. It took a long time to get it to work, quite a while
ago and I don't remember all the issues I had.
The Serial.list() function returns an array of strings. You are calling that function twice in your code,
which is unwise for two reasons. Firstly, it's wasting time trying to get the information twice, from the
operating system of your PC. Secondly, the second time you use it, in the constructor for the new Serial object,
you are trying to attach an array index to a function call, which may or may not be syntactically legal but
is asking for trouble for number of reasons.
You are better off in my opinion calling Serial.list() once, assigning the result of the function call to an actual
array in your program, and then using one of those array elements as the parameter to the new Serial() constructor.