Using Button and dropdownlist of controlP5 at the same time
in
Contributed Library Questions
•
1 year ago
Hi,
I wan to using controlP5 library to list the Serial Port , and then use Button to send and read data from USB port.
But I dont know how to write the "controlEvent" function. And I use someone's code , change a little. It works, but it has a error message:
error, disabling serialEvent() for //./COM3
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at processing.serial.Serial.serialEvent(Unknown Source)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:772)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1641)
Caused by: java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at button2.serialEvent(button2.java:143)
... 8 more
the sketch's name is button2.
and the code is :
import controlP5.*;
ControlP5 controlP5;
DropdownList p1;
int comnum = 0;
import processing.serial.*;
Serial myPort; // The serial port
//int xPos = 1; // horizontal position of the graph
int index = 0;
int[] xaxis;
int[] yaxis;
int[] zaxis;
int[] press;
public void setup () {
// set the window size:
size(400, 300);
controlP5 = new ControlP5(this);
controlP5.addButton("Start",100,25,100,80,19);
controlP5.addButton("Stop",255,25,120,80,19);
controlP5.addButton("Off",128,25,140,80,19);
controlP5.addButton("On",255,25,160,80,19);
controlP5 = new ControlP5(this);
//
p1 = controlP5.addDropdownList("myList-p1",25,50,100,120);
customize(p1);
//initialize a serial port with first available port which will be selectable later
myPort = new Serial(this, Serial.list()[0], 57600);
//myPort.bufferUntil('\n');
background(0);
}
void draw () {
//background(0);
}
void redraw() {
background(0);
}
//
void customize(DropdownList ddl) {
ddl.setBackgroundColor(color(190));
ddl.setItemHeight(20);
ddl.setBarHeight(15);
ddl.captionLabel().set("Change port");
ddl.captionLabel().style().marginTop = 3;
ddl.captionLabel().style().marginLeft = 3;
ddl.valueLabel().style().marginTop = 3;
println(Serial.list());
for(int i=0;i<Serial.list().length;i++) {
ddl.addItem(Serial.list()[i],i);
}
ddl.setColorBackground(color(60));
ddl.setColorActive(color(255,128));
}
void controlEvent(ControlEvent theEvent) {
// PulldownMenu is if type ControlGroup.
// A controlEvent will be triggered from within the ControlGroup.
// therefore you need to check the originator of the Event with
// if (theEvent.isGroup())
// to avoid an error message from controlP5.
if (theEvent.isGroup()) {
println(int(theEvent.group().value()));
myPort.stop();
myPort = new Serial(this, Serial.list()[int(theEvent.group().value())], 19200);
//myPort.bufferUntil('\n');
redraw();
}
}
public void Stop() {
println("Stop");
}
public void Start() {
println("Start");
}
void Off()
{
myPort.write("ATS18=000140CC\r\n");
}
public void On() {
myPort.write("ATS18=0000000C\r\n");
println("Clear");
}
void serialEvent(Serial myPort) {
// get the ASCII string:
//Get x,y,z Axes
String inStringx = myPort.readStringUntil(',');
String inStringy = myPort.readStringUntil(',');
String inStringz = myPort.readStringUntil(',');
String inStringpress = myPort.readStringUntil(',');
xaxis[index] = Integer.parseInt(inStringx);
yaxis[index] = Integer.parseInt(inStringy);
zaxis[index] = Integer.parseInt(inStringz);
press[index] = Integer.parseInt(inStringpress);
println(xaxis[index]);
index++;
}
1