1

我制作了一个通过 rs232 连接到设备的程序。在我关闭连接并尝试从同一个应用程序再次重新打开它之前,这一切都很好。我得到以下异常:

javax.comm.PortInUseException: Port currently owned by controller.Application
        at javax.comm.CommPortIdentifier.open(CommPortIdentifier.java:310)
        at controller.Application.connect(Application.java:30)
        at controller.Application.send(Application.java:63)

当我完全关闭我的应用程序并重新启动它时,我能够再次连接到端口。但是我需要在不关闭整个应用程序的情况下再次使用该端口。我试图关闭我的端口,如下所示,但它没有按我的意愿工作:

public void disconnect() {
        if (serialPort != null) {
            try {
                in.close();
                out.close();
                serialPort.removeEventListener();
                serialPort.close();
                commPort.close();
            } catch (Exception ex) {
                System.out.println("When closing: " + ex);
            }
        }

    }

我应该或可以如何关闭我的端口以避免此异常?

4

1 回答 1

0

您可以使用

                    if (serialPort != null)
                    {
                        serialPort.close();
                    }

我希望它会有所帮助

于 2015-07-15T15:11:21.197 回答