We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › choosing a serial port after you've launched
Page Index Toggle Pages: 1
choosing a serial port after you've launched (Read 1386 times)
choosing a serial port after you've launched
Dec 3rd, 2009, 5:06pm
 
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.
Re: choosing a serial port after you've launched
Reply #1 - Dec 7th, 2009, 2:38am
 
Before changing your port, you need to stop() the original (assuming one has been created already).
Code:
serial_port.stop();  
serial_port = new Serial(this, portName, 57600);"


http://processing.org/reference/libraries/serial/Serial_stop_.html
Re: choosing a serial port after you've launched
Reply #2 - Dec 7th, 2009, 11:56am
 
Yes! So simple. Smiley That's perfect, I just added that one line of code and it works.  Thank you very much!
Page Index Toggle Pages: 1