我如何关闭 RXTX 中的 CommPort

How do i close a CommPort in RXTX

我正在尝试使用此关闭 Comm 端口:

public synchronized void closeComportButtonActionPerformed(
                java.awt.event.ActionEvent evt)  {

            try {
                twoWaySerCom.disconnect();
            } catch (Exception e) {

                e.printStackTrace();
            }
        } 

我的断开连接是这样的:

 void disconnect() throws Exception {
    if (serialPort != null) {
        InputStream in = serialPort.getInputStream();
        OutputStream out = serialPort.getOutputStream();
        try {

            // close the i/o streams.
            in.close();
            out.close();
        } catch (IOException ex) {
            // don't care
        }
        // Close the port.
        serialPort.close();
    }

当我按下关闭按钮时,我的应用程序冻结了,最终我在控制台中收到一条错误消息:

A fatal error has been detected by the Java Runtime Environment:

#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00949e69, pid=7568,      tid=4724
#
# JRE version: Java(TM) SE Runtime Environment (8.0_25-b18) (build 1.8.0_25-b18)
# Java VM: Java HotSpot(TM) Client VM (25.25-b02 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [rxtxSerial.dll+0x9e69]

谁能解释一下我做错了什么? 谢谢

此问题已通过以下内容部分解决。我现在可以成功关闭端口一次。 如果我重新打开端口,然后再次尝试关闭它,则会发生同样的错误。

try {

            serialPort.removeEventListener();
            serialPort.close();            
            in.close();
            out.close();
        } catch (IOException ex) {
            // don't care
        }
        // Close the port.
        serialPort.close();
    }
 if(portInUse!=null)
{
    try{
        portInUse.port = (SerialPort)portInUse.portId.open(this.getClass().getName(),TIME_OUT);
                 portInUse.port.setSerialPortParams(baudRate,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);               
                 comConn=this.COM_CONNECTED;
        }

        portInUse.input = new BufferedReader(
           newInputStreamReader(portInUse.port.getInputStream())
        );

        portInUse.output =  portInUse.port.getOutputStream();
        //adding listeners to input and output streams
        portInUse.port.addEventListener(this);
        portInUse.port.notifyOnDataAvailable(true);
        portInUse.port.notifyOnOutputEmpty(true);
        currentPort=portInUse;
    }

portInUse 的数据类型:

private class CustomPort
{
    String portName;
    CommPortIdentifier portId;
    SerialPort port;
    BufferedReader input;
    OutputStream output;
    Scanner inputName;
} 
CustomPort currentPort=new CustomPort();

以及我使用的冲洗功能:

public void flush()
{
    try {
        this.currentPort.output.flush();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我使用的关闭函数:

public void close(){
    if(currentPort.port!=null){
        currentPort.port.close(); //close serial port

    currentPort.input = null;        //close input and output streams
    currentPort.output = null;
    }
}

如何关闭端口:

 this.flush();
 this.close();

已多次反复测试,效果完美。

原来RXTX 2.2版本存在内存泄漏错误,当您尝试关闭串口时,该错误会导致该库在MacOS上崩溃。我的解决方案是使用 nrjavaserial,除其他外,它专门解决了这个错误,这样您就可以在 MacOS 中关闭串行端口而不会导致库崩溃。

我必须从我的 /Library/Java/Extensions 文件夹中删除我安装的 rxtx 库,当然,在 nrjavaserial 库工作之前删除我的 POM 文件中对它的引用。然后就用这段代码关闭了串口,终于可以正常使用了。

public void disconnectSerial() {
    new Thread(() -> {
        if (serialPort != null) {
            try {
                serialPort.getInputStream().close();
                serialPort.getOutputStream().close();
                serialPort.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}