I need to send serial data to my Arduino UNO from within a java application. I am using JSSC for this.

I wanted to verify that my data actually arrives correctly to the arduino before going to the next step of my application. For this I tried to just return the string I am sending, however when I send "test", this is what is returned:


I am wondering if this is a problem on the Java side with sending/reading the string, or that it's a problem with sending/reading on the arduino, and how I could possibly fix this.

This is my java code to read the data:

try {
    port.addEventListener(event -> {
        try {
        } catch (SerialPortException e) {
} catch (Exception e) {

This is my java code to send the data:

try {
} catch (SerialPortException e) {

And this is the code on my arduino:

void loop() {
  while (Serial.available() > 0) {

I hope someone can help :)

  • you have to say "over" when using walkie-talkies. or serial. Otherwise, a response of "Yeah, that's a bad idea" could be interpreted as just "yeah" if it cuts out.
    – dandavis
    Oct 13, 2020 at 15:42

1 Answer 1


This is a common misconception about how serial communication works.

You're "sending" the "string" test. Except you're not.

What you're actually doing is you're placing the string test into the send buffer. Another routine then takes each byte from that send buffer in turn and sends it out through serial.

The Arduino then receives that byte and places it into its internal receive buffer.

You then get around to reading from the serial and you just get whatever is in the receive buffer at that moment in time - however many bytes that happens to be.

Then you "send" those bytes back - except again, you don't. You place those bytes into the internal transmit buffer in the Arduino and another routine takes each byte in turn from that transmit buffer and sends it out through the serial.

Again the Java program gets around to reading from the serial, and all it gets is what happens to be in its receive buffer at the time, however many bytes that may be at the time.

There is no such thing as "a string" or "a line" in serial. All there is is bytes - one after another.

It is up to you to receive those bytes and reconstitute them back into the form you want as each byte happens to arrive.

As it stands you have no way of knowing what that form should be, since you just send four bytes with no meaning attached to them.

It's common to add a "terminator" to a string when sending so that the receiver knows when all the bytes have arrived. For us humans the simplest to use is a "line feed" or \n character.

Your receiving routine would then receive each character in turn appending it to the target String up until it receives the terminating character, at which point it knows that it has finished receiving this chunk of data and can act on it.

I have a popular blog post all about reading serial on the Arduino here, and the same principle can be applied to any other language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.