我想使用Java扫描可用的com端口。我使用了下面的代码,使用了Comm库,但它不能工作
java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
int i = 0;
String[] r = new String[10];
while (portEnum.hasMoreElements()){
CommPortIdentifier portIdentifier = portEnum.nextElement();
r[i] = portIdentifier.getName();
i++;
}
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(r));
portEnum.hasMoreElements()
每次都返回false
。
我的电脑上没有老式的RS232
接口,我用的是用putty测试的USBtoRS232
转换器。
我只想扫描可用的端口,所以我不介意使用其他库。
答案:我在注释波纹管中使用了该代码,并使用了它的工作
import jssc.SerialPortList;
public class Main {
public static void main(String[] args) {
String[] portNames = SerialPortList.getPortNames();
for(int i = 0; i < portNames.length; i++){
System.out.println(portNames[i]);
}
}
}
发布于 2017-08-29 09:26:08
您使用的是哪个java版本?jdk 8可能会出现问题。
你试过https://code.google.com/archive/p/java-simple-serial-connector/吗?
示例用法:
import jssc.SerialPortList;
public class Main {
public static void main(String[] args) {
String[] portNames = SerialPortList.getPortNames();
for(int i = 0; i < portNames.length; i++){
System.out.println(portNames[i]);
}
}
}
要获得更多的示例,请访问examples.wiki
https://stackoverflow.com/questions/45932969
复制相似问题