1.下载java Communications api开发包。
2. javacomm20-win32包中的文件配置到
以下jre路径为 C:\Program Files\Java\jdk1.7.0_45\jre,请不要放错了
\jre\bin\win32com.dll
\jre\lib\javax.comm.properties
\jre\lib\comm.jar
3 代码
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
public class ViewPorts {
public void listPorts(){
Enumeration enumeration=CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId;
while(enumeration.hasMoreElements()){
portId=(CommPortIdentifier)enumeration.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL){
System.out.println("port name :"+portId.getName());
}
}
}
public static void main(String[] args) {
ViewPorts vPorts=new ViewPorts();
vPorts.listPorts();
}
}
打印出结果:port name :COM1