Java 和 Arduino 不能使用 RXTX lib 占用相同的 COM 端口?

Java and Arduino can't occupy the same COM port using RXTX lib?

我正在尝试使用 RXTX 库将字符串从 Arduino Uno 发送到 Java。我的问题是,一旦 Arduino 准备好发送数据,它就会占用 java 应用程序应该从中读取的 COM 端口?其他在线示例也有类似的代码,可以工作 'presumably' - 通过连接到同一个端口?谢谢。

主要

    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 class

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());
  }
}

我从 java 那里得到错误,一旦 arduino 首先编译。 - 看过关于 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.

我后来没有关闭COM连接,所以占用了端口。我必须手动关闭进程中的 Java.exe 才能释放端口。 - 并编写适当的关闭方法;)