loosing or gaining bytes through serial

edited September 2017 in Arduino

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

Sign In or Register to comment.