2017-09-13 185 views
1

我试图在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()); 
    } 
} 

回答

0

我用serial.println打破到底线,而serial.write没有。在另一侧的java的readLine()方法读取整个行,直到它recorgnizes换行符(\ n)的

问题:serial.write()情况下,不存在换行“\ n”个,所以readLine()根本无法找到任何线路,并且导致java.io.IOException: Underlying input stream returned zero bytes 无法为readLine()工作发送一个换行符。

溶液:同时使用serial.write()手动提供线break语句( “\ n” 个)