2012-01-28 31 views
5
void connect (String portName) throws Exception 
{ 
    CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); 
    if (portIdentifier.isCurrentlyOwned()) 
    { 
     System.out.println("Error: Port is currently in use"); 
    } 
    else 
    { 
     System.out.println(portIdentifier.getCurrentOwner()); 
     CommPort commPort = portIdentifier.open(this.getClass().getName(),2000); 

     if (commPort instanceof SerialPort) 
     { 
      SerialPort serialPort = (SerialPort) commPort; 
      serialPort.setSerialPortParams(115200,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); 

      InputStream in = serialPort.getInputStream(); 
      OutputStream out = serialPort.getOutputStream(); 

      (new Thread(new SerialReader(in))).start(); 
      (new Thread(new SerialWriter(out))).start(); 

     } 
     else 
     { 
      System.out.println("Error: Only serial ports are handled by this example."); 
     } 
    }  
} 

是给gnu.io.PortInUseException:未知应用程序?

gnu.io.PortInUseException: Unknown Application 
    at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:354) 

我使用RXTX与Java在Windows 7家庭64位。

回答

3

重新启动系统/禁用端口。
实际的问题是程序运行时端口被打开,程序终止后没有关闭。
它的工作原理。

2

我遇到了这个问题,因为端口实际上是正在使用中。 Windows任务管理器中出现了javaw.exe的先前实例,它占用了该端口。

之前java进程挂起的原因是硬件问题:当我偶然使用的USB-2串行转换器插入到USB-2端口时,所有工作都正常。当插入USB-3端口时,RXTX CommPortIdentifier代码将挂起,然后Java的后续实例收到PortInUseException。

12

检查/ var/lock文件夹是否存在于您的机器上。

 

mkdir /var/lock 
chmod go+rwx /var/lock 
 
0

我用Process Explorer找到一个手柄\Device\PCISerial0并关闭手柄。如果您的COM端口不在PCI卡上,则名称可能有所不同。

相关问题