Hey there. I am trying to figure out how I can offer up a user interface to let you choose which serial device you want to connect to after you've launched the sketch. I'm doing just fine with getting an enumerated list of available ports, and I'm using controlP5 just fine to draw up a list of radio buttons, and I can even grab the radio button user input and pass it along to a function just fine. I just can't figure out how to get processing to change which serial device it's using after setup() has run. Is this possible?
My application is to support running multiple instances of a hardware controller that I'm building (http://beatseqr.com) and my thought is that you'd run two copies of this processing sketch and be able to choose the right serial name after launching the compiled app.
this is the serial stuff in setup(), as per completely normal operation if you only have one serial device:
Code:portName = Serial.list()[serial_device_number];
serial_port = new Serial(this, portName, 57600);
serial_port.clear();
// catch and clear the first message in case it's garbage
serial_read_value = serial_port.readStringUntil(59);
serial_read_value = null;
// println("from setup: serial_port.available = "+serial_port.available());
and then right below that, I'm drawing a list of controlP5 radios like this:
Code: controlP5 = new ControlP5(this);
Radio r = controlP5.addRadio("radio",100,160);
// r.setLabel("Click on your beatseqr serial device name");
for(int i=0;i<Serial.list().length;i++) {
r.add(Serial.list()[i],i);
}
The controlEvent for the radio buttons does this:
Code:void controlEvent(ControlEvent theEvent) {
// println("hi there" + theEvent.controller().id()+" / "+
// theEvent.controller()+" / "+
// int(theEvent.controller().value())
// );
change_serial(int(theEvent.controller().value()));
}
and I *thought* I'd be able to just redo the serial setup in setup(), but it doesn't work:
Code:void change_serial(int device_number)
{
println(device_number);
portName = Serial.list()[device_number];
serial_port = new Serial(this, portName, 57600);
serial_port.clear();
// catch and clear the first message in case it's garbage
serial_read_value = serial_port.readStringUntil(59);
serial_read_value = null;
println("from setup: serial_port.available = "+serial_port.available() + " for port name" + portName);
}
almost certainly causing the problem in the line:
Code: serial_port = new Serial(this, portName, 57600);
because it's not new, it's an existing thing, but I don't know how to change the thing.