我正在尝试从串行端口读取输入流但是获得异常" java.io.IOException:底层输入流返回零字节"
以下是代码:
package communication;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import org.apache.commons.io.IOUtils;
public class Serial_0306 implements SerialPortEventListener {
public SerialPort serialPort;
private static final String PORT_NAMES[] = {
"COM3", // Windows
};
enter code herepublic static BufferedReader input;
public static OutputStream output;
public static InputStream inputStream;
public static final int TIME_OUT = 2000;
public static final int DATA_RATE = 9600;
public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}
try {
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
inputStream = serialPort.getInputStream();
String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8); // Exception over here
System.out.println("Result ::" + result);
output = serialPort.getOutputStream();
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
@Override
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
System.out.println("Result in Serial Event::" + result);
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
public static synchronized void writeData(String data) {
System.out.println("Sent: " + data);
try {
output.write(data.getBytes());
System.out.println("Received DATA BYTES");
} catch (Exception e) {
System.out.println("could not write to port");
}
}
public static void main(String[] args) throws Exception {
Serial_0306 main = new Serial_0306();
main.initialize();
Thread t = new Thread() {
public void run() {
//the following line will keep this app alive for 1000 seconds,
//waiting for events to occur and responding to them (printing incoming messages to console).
try {
Thread.sleep(1500);
writeData("CP");
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
};
t.start();
System.out.println("Started");
}
}
**输出:
Native lib Version = RXTX-2.1-7 Java lib Version = RXTX-2.1-7 入门 java.io.IOException:底层输入流返回零字节 已发送:CP 无法写入端口**
我的实际需求是从串口获取输入流并将其转换为String。 但出于测试目的,我自己发送输出流并尝试读取相同的内容。但每当我尝试读取输入流时,我都会得到异常。 我的代码是否正确?我还需要做些什么才能获得正确的输入流并将其转换为String。