简体   繁体   English

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

[英]How to list the Connected USB Port Number using Vendor id and product id using java

What is the best API in java to display the connected USB port number using Vendor id and product id? java 中使用供应商 ID 和产品 ID 显示连接的 USB 端口号的最佳 API 是什么?

You can list all serial ports by using simple javax.comm API 您可以使用简单的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);
}
}
}

The output would be like 输出会像

COM1: Serial
COM2: Serial
COM7: Serial

EDIT : Also see a good blog on Creating and using a real-time port monitoring application powered by IBM Lotus . 编辑 :另请参阅有关创建和使用由IBM Lotus提供支持的实时端口监视应用程序的好博客。
Also by using jusb for windows , for linux you can do read and write operation which you want.you will find a a example here 同样,通过在Windows中使用jusb, 对于Linux,您可以执行所需的读写操作。您将在此处找到示例

I just found an good sulotion to get all ports names in java using JserialComm.jar library我刚刚找到了一个很好的解决方案,可以使用JserialComm.jar库获取 java 中的所有端口名称

private void getports() {
    ArrayList dataarr = new ArrayList();
    SerialPort[] ar = SerialPort.getCommPorts();
    int i = 0;
    while (i < ar.length) {
        String PortCom = ar[i].getDescriptivePortName();

        dataarr.add(PortCom);
        ++i;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM