bufferuntil('\n) won’t trigger serialevent processing with Eclipse

1.9k Views Asked by At

I want to do the simplest thing to plot a graph from the serial port of Arduino with the Processing software. I use Eclipse.

I did as the tutorials say about the plugins. I also copied the code from the Arduino site which is this:

import processing.serial.*;

Serial myPort;        // The serial port
int xPos = 1;         // Horizontal position of the graph

void setup () {

  // Set the window size:
  size(400, 300);

  // 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 the initial background:
  background(0);
}

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);

    // Convert to an int and map to the screen height:
    float inByte = float(inString);
    inByte = map(inByte, 0, 1023, 0, height);

    // Draw the line:
    stroke(127, 34, 255);
    line(xPos, height, xPos, height - inByte);

    // At the edge of the screen, go back to the beginning:
    if (xPos >= width) {

      xPos = 0;
      background(0);
    }
    else {
       // Increment the horizontal position:
       xPos++;
    }
  }
}

There is a problem that the bufferUntil('\n') does not trigger the serialevent.

I know that there was a bug. In case you try to set an 8-bit int to a 32-bit int, it goes to hell.

The Processing IDE works great though. Eclipse does not trigger at all. Is there a solution?

1

There are 1 best solutions below

3
On

Note that bufferUntil('\n') takes an integer value. You're giving it a char. At the very least try bufferUntil(10) just to see if there's some oddness going on there, but it might be worth simply printing the values see coming in on myPort and see what comes by when you send a newline.