Hello,

I am another newbie, trying to finish an assignment, and I would need some help in splitting a string from serial.

I use the demo code for eZ430-RF2500 Wireless Sensor Monitor, where I changed only the tx of the access point's temperature(by commenting it), with the purpose of displaying only the temperature of the remote sensor.

The string is in the following form: Node:0001, Temp:28.1C, Battery:2.6V, Strenght:031%, RE:no

The processing code for the above displayed string:

import processing.serial.*;

Serial myPort;

String sensorReading="";

void setup() {

size(800,600);

myPort = new Serial(this, "COM7", 9600);

myPort.bufferUntil('\n');

}

void draw() {

//The serialEvent controls the display

}

void serialEvent (Serial myPort){

sensorReading = myPort.readStringUntil('\n');

if(sensorReading != null){

sensorReading=trim(sensorReading);

}

writeText("Sensor Reading: " + sensorReading);

}

void writeText(String textToWrite){

background(255);

fill(0);

text(textToWrite, width/20, height/2);

}

I would need to split the string in this way:

Node:0001

Temp:28.1C

Battery:2.6V

Strenght:031%

RE:no

I tried some examples using an array with (splitTokens(sensorReading, ",")), charAt(), but the println() does not seem to work,I got the error disabling serialevent() for com null.

Would anyone suggest me how can I solve this?

Thank you.

Have a good day.

If those fields are separated by a comma and a space, you can simply use String's split method to create an array of fields

String[] fields = sensorReading.split(", ");

Edited 1 Year Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.