简体   繁体   中英

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?

You can list all serial ports by using simple 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 .
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

I just found an good sulotion to get all ports names in java using JserialComm.jar library

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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