【发布时间】:2017-09-13 09:53:35
【问题描述】:
我正在尝试借助 rxtx 库使用 java 程序读取 arduino uno 数据。我为此使用 COM8 串行通信端口。我用的是win10。
我的问题:当我使用“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());
}
}
【问题讨论】:
标签: java arduino-uno ioexception serial-communication