简体   繁体   中英

RXTX getPortIdentifiers() stuck

I have problem to read lit of serial ports.when programe reach at CommPortIdentifier.getPortIdentifier() it stuck for almost 5 minnutes. as I observe delay could be due to scaning all ports iin system. So how to avoid this 5 minutes delay?

How do you scan for available ports?

For instance, the code below will return string list of available serial ports:

public List<String> getAvailablePorts() {

    List<String> list = new ArrayList<String>();

    Enumeration portList = CommPortIdentifier.getPortIdentifiers();

    while (portList.hasMoreElements()) {
        CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            list.add(portId.getName());
        }
    }

    return list;
}

Edit : since actual solution is digged in comments, I add it here:

It appears that commPortIdentifier.getPortIdentifier(portName) indeed does port rescan under certain circumstances; fast workaround is to set fixed port list manually via gnu.io.rxtx.SerialPorts system property.

This one may not apply to all circumstances but it works for me and it takes less than a second to get the ports I wanted. I used the devcon utility of Windows basing from the command line.

devcon find "usb\vid_067B&PID_2303*"

This will list down the ports where the Prolific PL2303 USB-Serial cable is attached. Then I parsed the result. I usually got few results because I only plug one or two cables in my computer.

This code may not be portable and limited but it fits well for my purpose and its speed is way too fast than using CommPortIdentifier.getPortIdentifiers().

Here is a part of my code:

public List<String> getAvailablePorts() {        
    List<String> list = new ArrayList<String>();        
    String res="";
    BufferedReader br;
    String cmd;
    Process proc;

    try {
        /*search for ports where Prolific PL2303 cable is attached*/
        cmd = "devcon find \"usb\\vid_067B&PID_2303*\"";
        proc = Runtime.getRuntime().exec(cmd);

        /* parse the command line results*/
        br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        do {
            try {
                res = br.readLine();
                if (res == null)
                    break;

                if (res.contains((CharSequence)("Prolific USB")))
                    list.add(res.substring(res.indexOf("(COM")+1, res.indexOf(")")));
            } catch(Exception e) {
                e.printStackTrace();
            }
        } while (!res.equals("null"));
        br.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return list;
}

Just make sure the devcon.exe file is correct (32bit or 64bit) and it is in the right folder where the application can access it.

This works on WinXP and Win7/8 64bit but for 32bit Win7/8, the application may need to be launched with administrator rights (run as admin). I added a .manifest file in my final executable so there's no need to "right click and run as admin" on 32bit Windows.

On Ubuntu 14.04 Linux, I used the terminal command

ls /dev/ttyUSB*

to list down the USB-Serial ports and then parsed the result.

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