Java String Data

I am having a ridiculous time trying to store the data read from the serial port and storing it into a String. The Arduino is set to delay(1000) and then Serial.print("Hello World") in a loop. That's working fine inside of the Serial Monitor in the Arduino IDE. Now here's my code where I read the string.

public synchronized void serialEvent(SerialPortEvent oEvent)
            if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE)
                    int available = input.available();
                    byte readData[] = new byte[available];
          , 0, available);
                    //System.out.print(new String(readData));
                    lastRecieved = new String(readData);
                    System.out.print(lastRecieved + "\n");
                } catch (Exception e) {System.err.println(e.toString());}


When i print the data using "System.out.print(new String(readData));" than it outputs "Hello World" fine - but when I try to store that data into a variable, and print that, i get random chunks of Hello World...

I get like:

He llo World Hello Wo rld

Oh and "lastRecieved" is a public static String accessed by the main program (this is a class, but is still all messed up when i add a main to it and run it that way).

its patternless and im at a complete loss haha. I have a general background in C++ and Java, but not with serial reading like this before.

Thanks in advance! -Jordan


int available = input.available(); returns the number of bytes avaliable on the serial port. This number changes sometime there are 2 bytes sometimes there are 9.

The SerialPort.Event is triggered when there are any bytes to read.

Both print statements actually print the same content System.out.print(new String(readData)); and System.out.print(lastRecieved + "\n"); You just don't see where the data from one event starts and ends in the first call.

Changing System.out.print(new String(readData)); to System.out.print("Received in this Event : "+new String(readData)); should make this clear. For debugging puprposes in Java it is better to use System.out.println calls. You get a automatic newline at the end as a visible mark for a loop iteration.


The difference is simple.

System.out.print(new String(readData)); -> print each chunk in succession System.out.print(lastRecieved + "\n"); -> print each chunk with a newline character on the end I reckon that the thing you're after is removing the newline character (then it doesn't matter which version you use) and use Serial.println() in the arduino as opposed to Serial.print(). Using println on the arduino means you have two characters to break each command on, namely the carriage return and newline feed characters. Just because you print it in one go on the arduino, doesn't mean that it gets sent in 1 chunk, and even if it is sent in 1 chunk, there is no guarantee that the serial port will receive it in 1 chunk. You'll have to provide the logic for discerning one command from the other yourself.

okay cool haha, thats where i was wrong

i was thinking the data was all going to be sent in one shot, rather than occasionally broken up...

i guess ill just make my own little protocol where the start of text is & and the end is * and that will be my String designators

Thanks for clearing that uppp -Jordan

If you use Serial.println exclusively on the arduino side, you can simply use the carriage return or newline feed characters as command terminators. You shouldn't need a command initialiser character (at a linebreak or newline feed, simply accept any following characters as belonging to the next command).