MrSchmuck
MrSchmuck

Reputation: 65

Java serial reading program returns gibberish

I am attempting to read data from the serial port on my computer. It is connected to an arduino printing "hi" over and over. For some reason the program only returns gibberish. code:

import jssc.SerialPort;
import jssc.SerialPortException;

public class SerialRead {

public static void main(String[] args) {
    byte[] x;
    SerialPort serialPort = new SerialPort("/dev/cu.usbmodem411");
    try {
        serialPort.openPort();//Open serial port
        serialPort.setParams(9600, 8, 1, 0);//Set params.
        byte[] buffer = serialPort.readBytes(10);//Read 10 bytes from serial port
        x = serialPort.readBytes(10);
        serialPort.closePort();//Close serial port
        System.out.println(x);
    }
    catch (SerialPortException ex) {
        System.out.println("aw cwap, someting went wong");
    }
}
}

it returns [B@60e53b93

Upvotes: 0

Views: 126

Answers (1)

leeor
leeor

Reputation: 17771

What it's printing out is a byte array, which is precisely what you've read. You will need to convert it using something like new String(bytes) to get something readable.

Upvotes: 1

Related Questions