Mathieu Brouwers
Mathieu Brouwers

Reputation: 564

Read serial line per line in Processing

I'm working on a simple demo application in Processing, this program should eventually show a chart or graph displaying my sensordata.

The problem I'm having is that I can't find any other method to read from the Serial port other then using port.readString(). This seems like the right way to do so, but this way it doesn't read the serial per line.

This is the current output I am getting, don't worry about the 'Writing to file' and 'Items written:' text. I know how to fix this and it's not a problem.

enter image description here

For the input of the program each line should consist of 12 values. Without having this it will get much harder to get a graph out of this data.

Is there any way to get the Serial data per line instead of 'random' linebreaks in the middle of numbers?

Upvotes: 1

Views: 1523

Answers (1)

Kevin Workman
Kevin Workman

Reputation: 42176

From the Serial library documentation, it sounds like you're looking for the readStringUntil() function.

The readStringUntil() function does exactly like you described and reads a String until it gets to a character you specify, which can be a line break. In fact that's what the example in that link uses.

If for some reason that doesn't work, you could use the bufferUntil() function and the serialEvent() callback to get events whenever a line is completed. Or you could simply detect the line break yourself and split the values as appropriate.

I'd recommend trying something and posting an MCVE if you get stuck. Good luck.

Upvotes: 1

Related Questions