G
Guest
Вот этот тред:
вот нарисую класс Scanner:
При вызове Inicialisation () , повисает portList = CommPortIdentifier.getPortIdentifiers();// , причем через рас , а если второй раз вызвать Inicialisation , то все проходит ... Что за байда?
Код:
Scanner scanner = new Scanner();
scanner.setDaemon(true);
scanner.start();
Код:
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
public class Scanner extends Thread{
public SerialPort inicialase(String defaultPort,int enableReceiveTimeout){
SerialPort port = null;
portList = CommPortIdentifier.getPortIdentifiers();// Вот здесь повисает
while (portList.hasMoreElements()){
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
if (portId.getName().equals(defaultPort)){
try{
port = (SerialPort) portId.open("ComPort",2000);
System.out.println("Port "+defaultPort+" open!");
if(enableReceiveTimeout>0){
port.enableReceiveTimeout(enableReceiveTimeout);
System.out.println("Port "+defaultPort+".setEnableReceiveTimeout("+enableReceiveTimeout+")");
}
}catch (PortInUseException e){
System.out.println("Port "+defaultPort+" is already in use, can not use this port!! ");
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
System.out.println("return com port");
return port;
}
public void run(){
this.Inicialise("COM1",20*1000);
}
}