Hello Processing community. I am working with multiple Windows and with multiple Serials. I been trying to separate my 2 serial data into 2 different Windows (not 1 windows with multiple panes) in processing. Hopefully I don’t sound to confusing. but I been breaking my head over this. I have gotten so many response with different ideas, but because I am not that good in java I am just not getting it.
here is my code, I have broken everything down with for simplicity.
/*
==========> Arduino Code Simple Analog Code For Now <=============
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}
/*
==========> Processing Code <=============
*/
/* Device will be used for configuration and Diagnostics
Configurtion will display the device Status and Test of each Fuctions of sensors and Data Ports
Diagnostics will display the actual data being processed in real time
*/
// import Libraries
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import processing.serial.*;
ChildApplet0 child;
ChildApplet1 child1;
//ChildApplet2 child2;
//serial port select
Serial myPort = null; // serial is at null until
Serial myPort2 = null; // serial is at null until
String portname = null;
String portname2 = null;
// multiple srial listing
StringList list = new StringList(1); // stored lines
StringList list2 = new StringList(1); // stored lines
//Serial port data
String data; // latest arduino line
int[] datai; // as array of integers
String data2; // latest arduino line
int[] datai2; // as array of integers
//FONTS
PFont myFont; // specify a Font
PFont myFont2; // specify a Font
//INITIALZTION
int w= 800, h =400; // canvas settings, workable sized window desired.
int listlong = 2; // list of lines to append. append function will be called out later void serialEvent
int listlong2 = 2; // list of lines to append. append function will be called out later void serialEvent
//-----------------------------------------------serial1
void openserialport()
{
if (portname == null) return;
if (myPort != null) myPort.stop();
myPort = new Serial(this, portname, 115200);
myPort.bufferUntil('\n');
}
void selectSerialPort()
{
String result = (String) JOptionPane.showInputDialog(null,
"Select the Serial Port that Corresponds to your Arduino board.", "Select Serial Port",
JOptionPane.QUESTION_MESSAGE, null, Serial.list(), 0);
if (result != null) {
portname = result;
openserialport();
}
}
void serialEvent(Serial p1)
{
data = trim(p1.readStringUntil('\n')); // initialize the data as what is read in serial p
if (data != null) { // check if data is not null
//println(data); // print every GOOD line
datai = int( split(data, ", ") ); // create int array (unused example ) //initialize DATA1 and split data incase there is a coma
list.append( data ); // OR store line as String list // append the data that is gathered from the serial port
if ( list.size() >= listlong ) list.remove(0); // erase the oldest? // for ( int i = 0; i }//
}
}
//-------------------------------------------------end serial1
//-------------------------------------------------serial2
void openserialport2()
{
if (portname2 == null) return;
if (myPort2 != null) myPort2.stop();
myPort2 = new Serial(this, portname2, 115200);
myPort2.bufferUntil('\n');
}
void selectSerialPort2()
{
String result2 = (String) JOptionPane.showInputDialog(null,
"Select the Serial Port that Corresponds to your Arduino board.", "Select Serial Port",
JOptionPane.QUESTION_MESSAGE, null, Serial.list(), 0);
if (result2 != null) {
portname2 = result2;
openserialport2();
}
}
void serialEvent2(Serial p2)
{
data2 = trim(p2.readStringUntil('\n')); // initialize the data as what is read in serial p
if (data2 != null) { // check if data is not null
//println(data); // print every GOOD line
datai2 = int( split(data2, ", ") ); // create int array (unused example ) //initialize DATA1 and split data incase there is a coma
list.append( data2 ); // OR store line as String list // append the data that is gathered from the serial port
if ( list.size() >= listlong ) list.remove(0); // erase the oldest? // for ( int i = 0; i }//
}
}
//-------------------------------------end serial2
void settings() {
size(w, h, JAVA2D);
}
void setup() {
surface.setTitle("Main sketch");
child = new ChildApplet0();
child1 = new ChildApplet1();
delay(1000);
}
void draw() {
background(50, 100, 180); // background color for size of canvas (R, G, B)
myFont = createFont("Arial", 30); // fonts call out for type and size for title
fill(234);
textFont(myFont);
text("Instructions ... ", 325, 30);//lets give title to our window
myFont = createFont("Arial",20);
fill(234);
textFont(myFont);
text("To Enable and Disable Console Press ....'C'.", 35, 75);
text("To Active Serial Port for any API Press ..'P'.", 35, 100);
text("Disable Console before Entering Production Mode.", 35, 135);
text("Diagnostics Port is always availabe at anytime.", 35, 165);
}
class ChildApplet0 extends PApplet {
//JFrame frame;
public ChildApplet0() {
super();
PApplet.runSketch(new String[]{this.getClass().getName()}, this);
}
public void settings() {
size(w, h, JAVA2D);
smooth();
}
public void setup() {
surface.setTitle("Device Diagnostics");
//arcball2 = new Arcball(this, 300);
delay(1000);
}
public void draw() {
background(50, 100, 180); // background color for size of canvas (R, G, B)
myFont = createFont("Arial", 30); // fonts call out for type and size for title
fill(234);
textFont(myFont);
text("Diagnostics ... ", 15, 25);//lets give title to our window
for ( int i = 0; i < list.size(); i++ ) text( list.get(i), 50, 110+i*20 ); // running list of arduino lines
myFont = createFont("Arial",16);
fill(234);
textFont(myFont);
int y = 24, dy = 12;
text("Press ' P ' to Select Serial Port", 575, 385); y += dy;
y = height - dy;
text("Current Serial Port: " + portname, 12, y); y -= dy;
}
public void keyPressed()
{
if (key == 'p' || key == 'P') selectSerialPort();
clear();
}
}
/////-----------------------------------------diagnostics data
class ChildApplet1 extends PApplet {
//JFrame frame;
public ChildApplet1() {
super();
PApplet.runSketch(new String[]{this.getClass().getName()}, this);
}
public void settings() {
size(w, h, JAVA2D);
smooth();
}
public void setup() {
surface.setTitle("Console"); //////Console////////
delay(1000);
}
public void draw() {
background(50, 100, 180); // background color for size of canvas (R, G, B)
myFont2 = createFont("Arial", 30); // fonts call out for type and size for title
fill(230);
textFont(myFont2);
text("Console ... ", 15, 25);//lets give title to our window
for ( int i = 0; i < list.size(); i++ ) text( list.get(i), 50, 110+i*20 ); // running list of arduino lines
myFont2 = createFont("Arial",16);
fill(234);
textFont(myFont2);
int y = 24, dy = 12;
text("Press ' P ' to Select Serial Port", 575, 385); y += dy;
y = height - dy;
text("Current Serial Port: " + portname2, 12, y); y -= dy;
}
public void keyPressed()
{
if (key == 'p' || key == 'P') selectSerialPort2();
clear();
}
}