Im most bad in serial communication. Sometimes I can start Processing app without error and sometimes following error shows up :
error, disabling serialEvent() for //./COM4
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at processing.serial.Serial.serialEvent(Unknown Source)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
at trial2.serialEvent(trial2.java:81)
... 8 more
Appreciate any comments regarding serial. Thank You in advance.
regards
---------
Snippet of Arduino output app, x,y,z are double variabletypes:
//Output the caculations Serial.print(x); Serial.print(","); Serial.print(y); Serial.print(","); Serial.println(z); Serial.flush();
Snippet of Processing app included in serial communication:
Setup void func:
println(Serial.list()); frameRate(50);
// Open whatever port is the one you're using. myPort = new Serial(this, Serial.list()[1], 115200); myPort.bufferUntil(lf);
//to hold the caculated values double x; double y; double z;
void setup(){ Serial.begin(115200); }
void loop(){
//read the analog values from the accelerometer int xRead = analogRead(xPin); int yRead = analogRead(yPin); int zRead = analogRead(zPin);
//convert read values to degrees -90 to 90 - Needed for atan2 int xAng = map(xRead, minVal, maxVal, -90, 90); int yAng = map(yRead, minVal, maxVal, -90, 90); int zAng = map(zRead, minVal, maxVal, -90, 90);
//Caculate 360deg values like so: atan2(-yAng, -zAng) //atan2 outputs the value of -π to π (radians) //We are then converting the radians to degrees x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI); y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI); z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);
//Output the caculations Serial.print(x); Serial.print(","); Serial.print(y); Serial.print(","); Serial.println(z); Serial.flush();
// delay(10);//just here to slow down the serial output - Easier to read }
Full listing of the Processing app:
import processing.serial.*;
Serial myPort; // The serial port PFont myFont; // The display font String inString; // Input string from serial port int lf = 10; // ASCII linefeed int environx,environy,environz; int rotx=90,roty=0,rotz=0; //holds rotation parameters int linezero; // holds (1/6)- 36 of displayheight int line1,line2,line3,line4,line5,line6; float line90,line270,xin,yin,zin; int lengde=0,teller=0; int plotstart=0; boolean bflag1=false,bflag2=false,bflag3=false,bflag4=false,bflag5=false,bflag6=false,bflag7=false;
Part of Arduino code. The x,y,z are double datatypes.
//Output the caculations Serial.print(x); Serial.print(","); Serial.print(y); Serial.print(","); Serial.println(z);
delay(10);
.......
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
[0] "COM1"
[1] "COM4"
error, disabling serialEvent() for //./COM4
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at processing.serial.Serial.serialEvent(Unknown Source)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at trial2.serialEvent(trial2.java:75)
... 8 more
java.lang.NullPointerException
at processing.mode.java.runner.Runner.findException(Runner.java:682)
at processing.mode.java.runner.Runner.reportException(Runner.java:627)
at processing.mode.java.runner.Runner.exception(Runner.java:570)
at processing.mode.java.runner.EventThread.exceptionEvent(EventThread.java:367)
at processing.mode.java.runner.EventThread.handleEvent(EventThread.java:255)
at processing.mode.java.runner.EventThread.run(EventThread.java:89)
Im trying to send strings between an accelerometer connected to arduino, and processing. Some times it works (i confirm receipt by printing them out in processing) and sometimes dont work. Anyone could have a sneakpeak into code regarding
communication to see what might cause the unstabillity?
The Arduino sketch is furthest below.
Thanks in advance. Regards
import processing.serial.*; Serial port; int environx,environy,environz; int rotx=90,roty=0,rotz=0; //holds rotation parameters int linezero; // holds (1/6)- 36 of displayheight int line1,line2,line3,line4,line5,line6; float line90,line270; boolean flag1,flag2,flag3,flag4,flag5,flag6,flag7,flag8,flag9,flag10=false; // flag 1-3 = accel x,y,z flag4 = Kalman filter flag 5 = PID flag 7-9 = gyro x,y,z float xposa,yposa,zposa=0; // start of accelerator graph int xposg,yposg,zposg=0; // start of gyroscope graph float xin,yin,zin =0.0; //input from serial port int lf=10; // linefeed in ascii String input;
port = new Serial(this, Serial.list()[1], 9600); port.clear(); input = port.readStringUntil(lf); input = null; // don't generate a serialEvent() unless you get a newline character: port.bufferUntil('\n'); // port.write(65);
// get the ASCII string: // input = port.readStringUntil('*'); // String input = port.readString(); while (port.available() > 0) { input = port.readStringUntil(lf); if (input != null) { // Print message received println( "Receiving:" + input); // trim off any whitespace // input = trim(input); int[] vals = int(splitTokens(input,", ")); xin=vals[0]; yin=vals[1]; zin=vals[2]; println( "xin= "+xin); // println( "y= "+yin); // println( "z= "+zin); // println(line2); // println(xposa); point(191.0+xposa,(float)line2-xin); // println((float)line2+(float)xin);
// start at beginning of graph and clear graph, if end of graph if (xposa >= displayWidth-64.0) { xposa = 191; // rect(193,line2-1,environx-192-64,line2-1); }
// increment the horizontal position: xposa++; yposa++; zposa++;
//Analog read pins const int xPin = 0; const int yPin = 1; const int zPin = 2; String accel; String string; //The minimum and maximum values that came from //the accelerometer while standing still //You very well may need to change these int minVal = 265; int maxVal = 402;
//to hold the caculated values double x; double y; double z;
void setup(){ Serial.begin(9600); }
void loop(){
//read the analog values from the accelerometer int xRead = analogRead(xPin); int yRead = analogRead(yPin); int zRead = analogRead(zPin);
//convert read values to degrees -90 to 90 - Needed for atan2 int xAng = map(xRead, minVal, maxVal, -90, 90); int yAng = map(yRead, minVal, maxVal, -90, 90); int zAng = map(zRead, minVal, maxVal, -90, 90);
//Caculate 360deg values like so: atan2(-yAng, -zAng) //atan2 outputs the value of -π to π (radians) //We are then converting the radians to degrees x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI); y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI); z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);
I try to get a program written in Arduino to communicate with Processing application in win7 64 bit environment.
I did download processing for 32 bit environment and uses that.
I try to run example from J. Blum writing code for Arduino and Processing. Ive checked serial output from Arduino its ok, so guess the fault is in programming on Processing site.
Can You kindly tell if the error I get is due to serial handling or other topics so I know the correct thread to post further requests?
Thank You
heres the error:
[0] "COM1"
[1] "COM4"
error, disabling serialEvent() for //./COM4
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at processing.serial.Serial.serialEvent(Unknown Source)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
Caused by: java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:989)
at java.lang.Float.valueOf(Float.java:388)
at java.lang.Float.<init>(Float.java:489)
at processing.core.PApplet.parseFloat(PApplet.java:8573)
at processing.core.PApplet.parseFloat(PApplet.java:8568)
at accelshow.serialEvent(accelshow.java:133)
... 8 more