Ciao a tutti,
Sto cercando di comunicare con Arduino (una serial Port) da Eclipse!
L´arduino manda istante per istante 3 grandezze via seriale.
Le classi che ho implementato sono le seguenti:
codice:
public class Main {
public static void main(String[] args) throws InterruptedException {
new DisplayFrame("Window 1").setVisible(true);
Thread.sleep(200);
new InterfaceOne("Button 1").setVisible(true);
new DisplayFrame("Window 2").setVisible(true);
}
}
Dove DisplayFrame é :
codice:
import javax.swing.JFrame;
public class DisplayFrame extends javax.swing.JFrame {
public DisplayFrame(String name){
this.setTitle(name);
this.setSize(600,600);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.JPanel panel= new javax.swing.JPanel();
panel.setBounds(100,100,500,500);
processing.core.PApplet sketch = new CircleSketch();
panel.add(sketch);
this.add(panel);
sketch.init();
this.setVisible(true);
}
}
E
codice:
import processing.core.*;
import processing.serial.*;
public class CircleSketch extends PApplet {
Serial myPort;
int linefeed = 10; // Linefeed in ASCII
int numSensors = 2; // we will be expecting for reading data from four sensors
double sensors[]=new double[3]; // array to read the 4 values
double pSensors[]=new double[3]; // array to store the previuos reading, usefur for comparing
// actual reading with the last one
double deLength=0;
double xPos=0;
public void setup() {
int baudRate=9600;
size(400,1023);
smooth();
// List all the available serial ports in the output pane.
// You will need to choose the port that the Wiring board is
// connected to from this list. The first port in the list is
// port #0 and the third port in the list is port #2.
println(Serial.list());
System.out.println("creating myPort...");
myPort = new Serial(this, Serial.list()[1], baudRate);
System.out.println("myPort created...");
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil(linefeed);
background(0); // Dark Blue BackGround
}
public void draw() {
serialEvent(myPort);
}
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil(linefeed);
int cycle=0;
// if you got any bytes other than the linefeed:
if (myString != null) {
//cycle=cycle+1;
/*System.out.println("MYSTRING1:");
System.out.println(myString);*/
//myPort.write(">C");
// trim(stringa)Removes whitespace characters from the beginning
// and end of a String. In addition to standard whitespace characters
// such as space, carriage return, and tab, this function also removes
// the Unicode "nbsp" character.
myString = trim(myString);
System.out.println("MyString_:");
System.out.println(myString);
// split the string at the commas
// and convert the sections into doubles:
pSensors = sensors;
//sensors = double(split(myString, ','));
String[] sensString=myString.split(",");
System.out.println("sensString[0]:");
System.out.println(sensString[0]);
// Replace with a for cycle as soon as possible
sensors[0]=Double.parseDouble(sensString[0]);
//System.out.println("sensors[0]");
//System.out.println(sensors[0]);
sensors[1]=Double.parseDouble(sensString[1]);
sensors[2]=Double.parseDouble(sensString[2]);
//System.out.println(sensors[0]);
double yPos = height - sensors[2];
// draw the line in a pretty color:
stroke(200);
line((float)xPos, height, (float)xPos, (float)yPos);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
// clear the screen by resetting the background:
background(0);
}
else {
// increment the horizontal position for the next reading:
xPos++;
}
// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
//print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
//println();
}
else {
System.out.println("MESSAGE NOT RECEIVED");
}
}
}
Tutto funziona bene, ho peró 2 cose che non capisco:
1) perché tra i vari output ho anche MESSAGE NOT RECEIVED ? Anche se Arduino invia continuamente? Questo é dovuto a una non precisa sincronizzazione di Arduino con Eclipse?
2) All´improvviso nella esecuzione del programma, nella CONSOLE di Eclipse ho:
codice:
MyString_:
0.00,0,0
sensString[0]:
0.00
MyString_:
0.00,0,0
sensString[0]:
0.00
MESSAGE NOT RECEIVED
gnu.io.PortInUseException: serial madness
at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:452)
at processing.serial.Serial.<init>(Unknown Source)
at processing.serial.Serial.<init>(Unknown Source)
at CircleSketch.setup(CircleSketch.java:31)
at processing.core.PApplet.handleDraw(PApplet.java:2280)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243)
at processing.core.PApplet.run(PApplet.java:2176)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Animation Thread" java.lang.RuntimeException: Error inside Serial.<init>()
at processing.serial.Serial.errorMessage(Unknown Source)
at processing.serial.Serial.<init>(Unknown Source)
at processing.serial.Serial.<init>(Unknown Source)
at CircleSketch.setup(CircleSketch.java:31)
at processing.core.PApplet.handleDraw(PApplet.java:2280)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243)
at processing.core.PApplet.run(PApplet.java:2176)
at java.lang.Thread.run(Unknown Source)
MyString_:
0.00,0,0
sensString[0]:
0.00
MyString_:
E´ un´errore che ho solo una volta. Vorrei sapere a cosa é dovuto! Cosa si intende per Serial madness?
Grazie mille per l´aiuto!