我试图在rxtx库的帮助下使用java程序读取arduino uno数据。我为此使用COM8串行通信端口。我使用win10。java.io.IOException:底层输入流返回零字节
我的问题是:当我使用'serial.print'时,然后向下的java函数正常工作,并检索arduino发送的所有内容。但是当我尝试在arduino中使用'serial.write'时,发生了ioexception “java.io.IOException:底层输入流返回零字节” 我不知道为什么。 我的需求是使用'serial.write'方法,请告诉我代码中出了什么问题。两个代码是向下
Java函数代码:
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
String inputLine=input.readLine();
System.out.println(inputLine);
} catch (Exception e) {
System.err.println(e.toString());
}
}
// Ignore all the other eventTypes, but you should consider the other ones.
}
的Arduino UNO代码:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
void setup() {
mySerial.begin(9600); // Setting the baud rate of Software Serial Library
Serial.begin(9600); //Setting the baud rate of Serial Monitor
}
void loop() {
if(mySerial.available() > 0) {
Serial.print(mySerial.read());
}
}