Serial data lost when data captured serialEvent() is used inside draw()

edited February 2014 in Arduino

Hello,

I have a weird (I'd say) issue with data captured via serialEvent(): I'm using an Arduino to send me a long list of numbers that I then want to write to a file on the computer using Processing. If I write these numbers into the file from the serialEvent() routine, all numbers show up in the file; if I write those numbers to the file, from inside the draw() routine, I end up with missing data (about 10%).

I use the SerialCallResponse algorithm to send from Arduino, and my code in Processing that "looses" data looks like this:

/* Note: I'm sending 128 values from Arduino then wait from a "255" signal from Processing that I can start sending data again */

import processing.serial.*;
int sentBytes = 128;                                 // count how many bytes I've sent in a "line" (line = 128 numbers/bytes--)
int[] serialInArray = new int[sentBytes];    // Where we'll put what we receive
int serialCount = 0;                                   // A count of how many bytes we receive
boolean firstContact = false;                     // Whether we've heard from the microcontroller
PrintWriter file;

void setup() {
  myPort = new Serial(this, Serial.list()[0], 115200);
  file = createWriter("numbers.txt");
  noLoop();
}

void draw() 
{
  for (int i = 0; i< sentBytes; i++)
  {
    file.print(serialInArray[i]);
    // separate numbers by a space
    file.print(" ");
  }
  //separate lines by a "newline"
  file.print('\n');
  file.flush();
}

void serialEvent(Serial myPort) {
  // read a byte from the serial port:
  int inByte = myPort.read();
  // if this is the first byte received, and it's a 255, clear the serial buffer and note that you've
  // had first contact from the microcontroller. Otherwise, add the incoming byte to the array:
  if (firstContact == false) {
    if (inByte == 255) { 
      myPort.clear();          // clear the serial port buffer
      firstContact = true;     // you've had first contact from the microcontroller
      myPort.write(255);       // ask for more
    } 
  } 
  else {
    // Add the latest byte from the serial port to array:
    serialInArray[serialCount] = inByte;
//    file.print(serialInArray[serialCount]);
//    file.print(" ");
    serialCount++;

    // If we have just counter over 128 bytes:
    if (serialCount > sentBytes - 1 ) {
      println();
//      file.print("\n");
//      file.flush();
      // Reset serialCount:
      serialCount = 0;
      redraw();
      myPort.write(255);
    }
  }
}

In this case, I miss some 128-numbers lines from my file.

= = = = =

If I do this:

void setup() {
  myPort = new Serial(this, Serial.list()[0], 115200);
  file = createWriter("numbers.txt");
//  noLoop();
}

void draw() {}

void serialEvent(Serial myPort) {
  // read a byte from the serial port:
  int inByte = myPort.read();
  // if this is the first byte received, and it's a 255, clear the serial buffer and note that you've
  // had first contact from the microcontroller. Otherwise, add the incoming byte to the array:
  if (firstContact == false) {
    if (inByte == 255) { 
      myPort.clear();          // clear the serial port buffer
      firstContact = true;     // you've had first contact from the microcontroller
      myPort.write(255);       // ask for more
    } 
  } 
  else {
    // Add the latest byte from the serial port to array:
    serialInArray[serialCount] = inByte;
    file.print(serialInArray[serialCount]);
    file.print(" ");
    serialCount++;

    // If we have just counter over 128 bytes:
    if (serialCount > sentBytes - 1 ) {
      file.print("\n");
      file.flush();
      // Reset serialCount:
      serialCount = 0;
      //redraw();
      myPort.write(255);
    }
  }
}

So the same data is put in serialInArray, which is then written to the file. But if I write it from draw(), I lose some entire arrays, if I write it to my file directly from the serialEvent() routine, then I get the correct result, with the numbers.txt file containing all the data sent from Arduino.

Note that I've also tried without the noLoop()/redraw() approach, and it makes no difference.

Any clues?

Thanks!

Sign In or Register to comment.