我試圖在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());
}
}