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.
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.