Java串口阅读程序returns乱码
Java serial reading program returns gibberish
我正在尝试从计算机上的串口读取数据。它一遍又一遍地连接到 arduino 打印 "hi"。由于某种原因,该程序只有 returns 乱码。代码:
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");
}
}
}
它returns [B@60e53b93
它打印出来的是一个字节数组,这正是你所读到的。您将需要使用 new String(bytes)
之类的东西对其进行转换以获得可读性。
我正在尝试从计算机上的串口读取数据。它一遍又一遍地连接到 arduino 打印 "hi"。由于某种原因,该程序只有 returns 乱码。代码:
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");
}
}
}
它returns [B@60e53b93
它打印出来的是一个字节数组,这正是你所读到的。您将需要使用 new String(bytes)
之类的东西对其进行转换以获得可读性。