serial-port - 串行端口 gnu.io. PortInUseException: 未知应用程序?


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: Unknown Application


 at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:354)



时间:

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





mkdir /var/lock


chmod go+rwx /var/lock




问题出在,程序运行时打开了端口,而在程序终止后却没有关闭, 重启可以解决。

我遇到这个问题,因为端口实际上还在使用中,javaw.exe的早先实例,占用了端口。

...