如何使用java使用供应商ID和产品ID列出已连接的USB端口号

时间:2013-07-22 04:21:55

标签: java usb

使用供应商ID和产品ID显示连接的USB端口号的java中最好的API是什么?

1 个答案:

答案 0 :(得分:0)

您可以使用简单的javax.comm API

列出所有串口
import javax.comm.*;
import java.util.Enumeration;

public class ListPorts {
public static void main(String args[]) {
Enumeration ports = CommPortIdentifier.getPortIdentifiers();
while (ports.hasMoreElements()) {
CommPortIdentifier port = (CommPortIdentifier)ports.nextElement();
String type;
switch (port.getPortType()) {
case CommPortIdentifier.PORT_SERIAL:
type = "Serial"; 
break;
default: /// Shouldn't happen
type = "Unknown"; 
break;
}
System.out.println(port.getName() + ": " + type);
}
}
}

输出类似于

COM1: Serial
COM2: Serial
COM7: Serial

修改: 另请参阅Creating and using a real-time port monitoring application powered by IBM Lotus上的好博客 另外,通过使用jusb for windowsfor linux,您可以执行所需的读写操作。您将找到a example here