import java.util.*;
import javax.comm.*;
public class PortTest
{
static CommPortIdentifier portId; //检测系统中可用的通讯端口类
static Enumeration portList; //Enumeration 为枚举型类,在util包中。
public static void main(String[] args) {
portList=CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()){
portId=(CommPortIdentifier)portList.nextElement();//强制转换为通讯端口类型
/*如果端口类型是串口,则输出其端口信息*/
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println(portId.getName());
}
}//while end
} //main() end
} //PortTest end
这段程序应该列举出所有能用的端口号,我编译也通过了,就是运行是没有结果,我整了两天了没有找出原因,希望大家帮忙!!情况如下:
F:\e>javac PortTest.java
F:\e>java PortTest
F:\e>
import javax.comm.*;
public class PortTest
{
static CommPortIdentifier portId; //检测系统中可用的通讯端口类
static Enumeration portList; //Enumeration 为枚举型类,在util包中。
public static void main(String[] args) {
portList=CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()){
portId=(CommPortIdentifier)portList.nextElement();//强制转换为通讯端口类型
/*如果端口类型是串口,则输出其端口信息*/
if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println(portId.getName());
}
}//while end
} //main() end
} //PortTest end
这段程序应该列举出所有能用的端口号,我编译也通过了,就是运行是没有结果,我整了两天了没有找出原因,希望大家帮忙!!情况如下:
F:\e>javac PortTest.java
F:\e>java PortTest
F:\e>
不知道你的comm.jar包安装是否正确?
=================================
按理说,你如果把win32com.dll放到JDK的\jre\lib\ext目录下,并把javax.comm.properties放到JDK的\jre\lib目录,把comm.jar放到你的工程的lib目录,是不会出现上述错误的,但是偶尔一次我要把发短信的后台程序做成windows服务,问题出来,就是找不到win32com,只用一招就把它搞定。呵呵,就是把win32com.dll放到JDK的\jre\bin目录下。