【发布时间】:2016-01-19 16:00:45
【问题描述】:
我正在尝试使用 RXTX 库将字符串从 Arduino Uno 发送到 Java。我的问题是,一旦 Arduino 准备好发送数据,它会占用 Java 应用程序应该读取的 COM 端口吗?其他在线示例有类似的代码,“大概”可以工作 - 通过连接到同一个端口?谢谢。
主要
public class Main {
public static void main(String[] args) throws IOException, PortInUseException, NoSuchPortException
{
SerialPortHandler serial = new SerialPortHandler();
serial.connect("COM10");
OutputStream serialOut = serial.getSerialOutputStream();
String s = "hello\r\n";
serialOut.write(s.getBytes());
System.out.println(s.toString());
serialOut.flush();
serialOut.close();
}}
RXTX类
public class SerialPortHandler {
private SerialPort serialPort;
private OutputStream outStream;
private InputStream inStream;
public void connect(String portName) throws IOException, PortInUseException, NoSuchPortException {
try {
// Obtain a CommPortIdentifier object for the port you want to open
CommPortIdentifier portId =
CommPortIdentifier.getPortIdentifier(portName);
// Get the port's ownership
serialPort =
(SerialPort) portId.open("Demo application", 5000);
// Set the parameters of the connection.
setSerialPortParameters();
// Open the input and output streams for the connection. If they won't
// open, close the port before throwing an exception.
outStream = serialPort.getOutputStream();
inStream = serialPort.getInputStream();
} catch (NoSuchPortException e) {
System.out.println("no port");
throw new IOException(e.getMessage());
} catch (PortInUseException e) {
System.out.println("port in use");
throw new IOException(e.getMessage());
} catch (IOException e) {
serialPort.close();
throw e;
}
}
/**
* Get the serial port input stream
* @return The serial port input stream
*/
public InputStream getSerialInputStream() {
return inStream;
}
/**
* Get the serial port output stream
* @return The serial port output stream
*/
public OutputStream getSerialOutputStream() {
return outStream;
}
/**
* Sets the serial port parameters
*/
private void setSerialPortParameters() throws IOException {
int baudRate = 57600;
try {
serialPort.setSerialPortParams(
baudRate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(
SerialPort.FLOWCONTROL_NONE);
} catch (UnsupportedCommOperationException ex) {
throw new IOException("Unsupported serial port parameter");
}
}}
Arduino - 使用软件串行示例 - 在 COM10 上
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(4800);
mySerial.println("Hello, world?");
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
一旦arduino首先编译,我就会从java中得到错误。 - 看过关于 USB3 端口与 Arduino 混淆的帖子,但在 USB2 上仍然没有运气
Stable Library
=========================================
Native lib Version = RXTX-2.2pre2
Java lib Version = RXTX-2.1-7
WARNING: RXTX Version mismatch
Jar version = RXTX-2.1-7
native lib Version = RXTX-2.2pre2
port in use
Exception in thread "main" java.io.IOException: Unknown Application
at arduinocon.SerialPortHandler.connect(SerialPortHandler.java:38)
at arduinocon.Main.main(Main.java:19)
Error 0x5 at ..\src\termios.c(892): Access is denied.
【问题讨论】:
-
"Native lib 版本 = RXTX-2.2pre2 Java lib 版本 = RXTX-2.1-7" 您的库不兼容。显然,您正在使用一些 beta 作为本机库,但 Java 版本需要本机库的受支持版本。
标签: java arduino serial-port rxtx