Arduino to Java array

Hi all,

I'm very new to the Arduino and the RXTX package.

I'm trying to get what the Arduino prints to be written in to an array by spliting the string at character "-". So the Arduino send e.g. "example-example-example" it should store them in an array. This is what I've got so far:

public void serialEvent(SerialPortEvent ev) {
if (ev.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
int available = inputS.available();
byte chunk[] = new byte[available];
inputS.read(chunk, 0, available);
String temp = new String(chunk);
System.out.print(temp);
String array[] = temp.split("-");
System.out.print(array[1]);
} catch (Exception e) {
System.err.println(e.toString());
}
}
}

It does execute but it prints out the string in completly the wrong order.

Please help,

Many Thanks,

Edward Francis

It does execute but it prints out the string in completly the wrong order.

Same sample input and output (and more output) are in order, then.

What is actually read from the serial port (and stored in chunk)?

What is actually in temp? Why are you printing the 2nd element of the array, rather than the first (or all)?

The serial reads in the string 24.70-45.00-52.00-1 , this is what temp is.

Yeah I will need to print and use the entire array, but I am just using array[1] as an example because I know that it should print 45.00.

Thanks Ed

because I know that it should print 45.00.

Instead, it prints what?

What triggers serialEvent? In Processing, the event can be triggered whenever any serial data arrives, or by the arrival of a specific character, like carriage return, line feed, or ">".

What does the sending code look like? Is it sending the necessary trigger (if there is one defined)?

If the function is called whenever serial data is present, it may get called long before the complete packet has arrived, resulting in incomplete data.

Any reason not to use StringBuffer and ArrayList to accumulate these lists of strings... Perhaps you should explicitly set the encoding when creating the String from the byte[] (otherwise you are at the mercy of the current Java installation's default charset - Ascii, ISO-Latin-15(5), UTF-8 or whatever)

For example:
I want to print "Hello World" to the String temp ready for it to be written to a text file. However what it prints onto console is:

Hel
Lo
Worldhe
Llo W
Orldh
etc.....

I need just "Hello World" to be written to the string and not loads of them. I've tried to add a extra character 'q' to the end so it would print a new line when it read q - this did not work though.