关闭和重新打开串行连接时的Java Comms PortinUseeException
我制作了一个程序,通过rs232连接到一个设备。我在运行WindowsVista的笔记本电脑上使用Java1.7和java通信运行它。我正在使用一个丰富的USB到串行适配器连接到这个设备。在我关闭连接并尝试从同一个应用程序重新打开它之前,一切都非常正常。我得到以下例外情况: javax.comm.PortInUseException:未知窗口当前拥有的端口 应用 当我完全关闭应用程序并重新启动它时,我可以再次连接到端口。我在另一台(旧的)笔记本电脑上没有这个问题,它仍然有一个板载rs232端口。我在这个网站上看到了一些类似的问题(例如:和),但是他们的解决方案对我不起作用 我的问题是:关闭和重新打开串行连接时的Java Comms PortinUseeException,java,serial-port,Java,Serial Port,我制作了一个程序,通过rs232连接到一个设备。我在运行WindowsVista的笔记本电脑上使用Java1.7和java通信运行它。我正在使用一个丰富的USB到串行适配器连接到这个设备。在我关闭连接并尝试从同一个应用程序重新打开它之前,一切都非常正常。我得到以下例外情况: javax.comm.PortInUseException:未知窗口当前拥有的端口 应用 当我完全关闭应用程序并重新启动它时,我可以再次连接到端口。我在另一台(旧的)笔记本电脑上没有这个问题,它仍然有一个板载rs232端口。
- 为什么我的端口仍然归我的程序所有
- 如何关闭端口以避免此异常李>
- 使用USB到串行适配器如何导致此问题? 提前感谢您的帮助或建议李>
private void connectToPort(String preferredPort) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(preferredPort)) {
try {
serialPort = (SerialPort) portId.open("PumpController", 2000);
} catch (PortInUseException e) {
System.err.println(e);
}
try {
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
printStream = new PrintStream(outputStream);
} catch (IOException e) {
System.err.println(e);
}
try {
serialPort.addEventListener((SerialPortEventListener) this);
} catch (Exception e) {
System.err.println(e);
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(38400,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
System.err.println(e);
}
}
}
}
connectedToPort = true;
}
public void closeConnection() throws IOException{
try{
serialPort.removeEventListener();
outputStream.close();
inputStream.close();
serialPort.close();
connectedToPort = false;
}catch(NullPointerException e){
System.err.println(e);
}
}
此方法关闭我的连接:
private void connectToPort(String preferredPort) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(preferredPort)) {
try {
serialPort = (SerialPort) portId.open("PumpController", 2000);
} catch (PortInUseException e) {
System.err.println(e);
}
try {
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
printStream = new PrintStream(outputStream);
} catch (IOException e) {
System.err.println(e);
}
try {
serialPort.addEventListener((SerialPortEventListener) this);
} catch (Exception e) {
System.err.println(e);
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(38400,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
System.err.println(e);
}
}
}
}
connectedToPort = true;
}
public void closeConnection() throws IOException{
try{
serialPort.removeEventListener();
outputStream.close();
inputStream.close();
serialPort.close();
connectedToPort = false;
}catch(NullPointerException e){
System.err.println(e);
}
}
要解决端口所有权冲突,您可以注册一个PortOwnershipListener,这样您将被告知其他应用程序是否要使用该端口。然后,您可以关闭端口,另一个应用程序将获得该端口,或者忽略该请求,另一个程序将获得PortinUseeException。问题不是另一个应用程序试图使用该端口。这是我自己的应用程序没有正确发布它。