We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hello
I have a issue that Processing isn't reading out all(or too much) of the data it gets.
I am sending data from the arduino(in this case just the numbers 1-6) and want to read them out in processing. the Baudrate has some effect but above and below 9600 it gets worse. I had in the past a delay in the arduino code, it didn't worked out neither. the catch(nullPointerException e) is there to catch strings that are 0, happened too in the past.
if i use other tools to read out the data from the serial such as the arduino serial monitor or Tera Term then there are no missing character/bytes.
SerialEvent doesn't trigger at all(with bufferuntil(10) or ('\n').
I hope someone has some ideas where the bug is crawling around.
walter
the processing code;
``` import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1;
int i,j,h,ii; // horizontal position of the graph
int lastxPos=1;
int lastheight=0;
int xPos1 = 1; // horizontal position of the graph
int lastxPos1=1;
int lastheight1=0;
String a="0";
int n=5;
String[] indata={a,a,a,a,a,a,a};//new String[6];
String[] indata2=new String[7];
String InString;
int[] rawdata=new int[7];
float[] data=new float[7];
PrintWriter output;
//Variables to draw a continuous line.
int lf= 10;
public static final int portIndex=1;
void setup () {
output= createWriter("output");
// List all the available serial ports
println(Serial.list());
// Check the listed serial ports in your machine
// and use the correct index number in Serial.list()[].
println(" Connecting to -> " + Serial.list()[portIndex]);
myPort = new Serial(this, Serial.list()[portIndex], 9600); //
delay(3000);
myPort.write(65);
// A serialEvent() is generated when a newline character is received :
myPort.bufferUntil('\n');
background(7000); // set inital background:
// delay(100);
}
void draw(){
//print(InString);
InString=myPort.readStringUntil('\n');
print(InString);
try{
indata2=split(trim(InString)," ");
for(int ii=0;ii<indata2.length;ii++){ _**here the error is created**_
indata[ii]=indata2[ii];
}}
catch(NullPointerException e){println("error invalid array");}
for(int j=0;j<indata.length;j++){
rawdata[j]=int(trim(indata[j]));
}
// println(rawdata);
// println(interval);
}
the arduino code:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
for(int i=0;i<7;i++){
Serial.print(i);
Serial.print("\r"" ");
}
Serial.print('\n');
}
and a typical output
5 61 2 3 4 5 6
0 1 2 3 4 5 6
0 0 1 2 3 4 5 6 or 0 1 2 3 4 5 6 1 2 3 4 5 6
Answers
https://Forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_1
now serial event seams to work. thank you very mutch just a question how much code is allowed in the SerialEvent void? i need to do some further calculations with my data and it shouldn't use the same data twice(ore skip any data). is it possible to do that in the SerialEvent(){}? or do i need to put in something like a boolean so that it doesn't process the same data twice?
It's not the amount of code but the duration it takes to complete.
Also, do not mutate the sketch's canvas there.
That Serial example already has noLoop() active:
https://Processing.org/reference/noLoop_.html
B/c redraw() is invoked within serialEvent(), draw() is run once for each trigger, as long as they don't trigger absurdly too many times within too short intervals. :-@
https://Processing.org/reference/redraw_.html