Serial Communication Error
in
Integration and Hardware
•
2 years ago
Hi,
I'm creating an Arduino Data Logger/ Multimeter. I'm getting an error in Processing when I try to get serial data from the Arduino.
My Arduino Code:
The Processing Code:
And finally the ERROR:
I'm creating an Arduino Data Logger/ Multimeter. I'm getting an error in Processing when I try to get serial data from the Arduino.
My Arduino Code:
- #include <Boards.h>
#include <Firmata.h>
void setup() {
// initialize the serial communication:
Serial.begin(9600);
}
void loop() {
String A_Data;
A_Data = analogRead(A0);
A_Data += ",";
A_Data += analogRead(A1);
A_Data += ",";
A_Data += analogRead(A2);
A_Data += ",";
A_Data += analogRead(A3);
A_Data += ",";
A_Data += analogRead(A4);
A_Data += ",";
A_Data += analogRead(A5);
Serial.println(A_Data);
delay(100);
}
- 216,217,218,218,215,215
218,219,219,219,217,215
214,215,215,216,214,213
216,217,216,216,214,214
214,215,215,215,213,212
214,215,215,215,213,211
213,214,215,214,213,212
212,213,213,214,211,210
The Processing Code:
- import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
int chCount = 0;
String[] ChVa;
float[] ChVo;
PFont font;
void setup () {
// set the window size:
size(600, 480);
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
font = loadFont("CenturySchoolbook-32.vlw");
textFont(font);
}
void draw () {
// everything happens in the serialEvent()
}
void serialEvent (Serial myPort)
{
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null)
{
// trim off any whitespace:
inString = trim(inString);
//split string into array
ChVa = split(inString, ',');
//convert string array to integer array and scale
for (int i = 0; i < 6; i++)
{
int tmp = int(ChVa[i]);
ChVo[i] = tmp*0.0049;
}
//Display Data
background(0);
//text("Ch0 : " + inV + "V",15,30);
text("Ch0 : " + ChVo[0],15,30);
text("Ch1 : " + ChVo[1],15,60);
text("Ch2 : " + ChVo[2],15,90);
text("Ch3 : " + ChVo[3],15,120);
text("Ch4 : " + ChVo[4],15,150);
text("Ch5 : " + ChVo[5],15,180);
}
}
And finally the ERROR:
- Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
[0] "COM6"
error, disabling serialEvent() for //./COM6
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 _6_Ch_Display.serialEvent(_6_Ch_Display.java:81)
... 8 more
1