Using two different readStringUntil "characters"

The solution I gave on that link expects the Arduino C code to send the multi-data as if they were a row of tab-separated values (TSV): :face_with_monocle:

That is, each value is followed by a \t character; except last 1, which is followed by an \n instead: :nerd_face:

Serial.print(temperature);
Serial.write('\t');
Serial.print(masse);
Serial.write('\t');
Serial.print(humidite);
Serial.write('\t');
Serial.print(comptage);
Serial.write('\t');
Serial.println(son); // implies '\n'

At the Processing Java side, each data row is received whole via Serial::readString(), then split as an array via PApplet::splitTokens(), and then converted to float[] via PApplet::float(): :coffee:

  1. Processing.org/reference/libraries/serial/Serial_readString_.html
  2. Processing.org/reference/splitTokens_.html
  3. Processing.org/reference/floatconvert_.html
float[] vals = {};

void serialEvent(final Serial s) {
  vals = float(splitTokens(s.readString()));
  redraw = true;
}

Full code from the original post: :sunglasses:

/**
 * Efficient Serial Multi-Value Reading (v1.1.2)
 * GoToLoop (2015-Feb-18)
 *
 * Forum.Processing.org/two/discussion/14988/
 * drawing-of-graphs-from-i2c-imu#Item_3
 *
 * Forum.Processing.org/two/discussion/16618/
 * processing-with-arduino-void-serialevent#Item_1
 *
 * Discourse.processing.org/t/
 * using-two-different-readstringuntil-characters/10769/6
 */

import processing.serial.Serial;

static final int PORT_INDEX = 0, BAUDS = 9600;

int[] vals = {};
//float[] vals = {};

void setup() {
  noLoop();
  final String[] ports = Serial.list();
  printArray(ports);
  new Serial(this, ports[PORT_INDEX], BAUDS).bufferUntil(ENTER);
}

void draw() {
  println(vals);
}

void serialEvent(final Serial s) {
  vals = int(splitTokens(s.readString()));
  //vals = float(splitTokens(s.readString()));
  redraw = true;
}
2 Likes