Search code examples
javarxtx

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?


Solution

  • 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.