【问题标题】:javax.comm.portinuseexception port currently owned byjavax.comm.portinuseexception 当前拥有的端口
【发布时间】:2015-03-07 11:00:15
【问题描述】:

我制作了一个通过 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);
            }
        }

    }

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

【问题讨论】:

    标签: java serial-port comm disconnection


    【解决方案1】:

    你可以使用

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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      相关资源
      最近更新 更多