我编写了一个通过 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);
}
}
}
我应该或者可以如何关闭我的端口以避免这种异常?
最佳答案
您可以使用
if (serialPort != null)
{
serialPort.close();
}
希望对你有帮助
关于javax.comm.portinuseexception 当前拥有的端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27855217/