Getting device/driver information related to a COM port?

后端 未结 3 1339
南旧
南旧 2020-12-09 20:14

I have a Serial-to-USB device with a similarly named device driver in the Windows device manager. The devices do not always grab the same COM port on system boot, so my prog

3条回答
  •  北海茫月
    2020-12-09 20:56

    @robjb Your code does not allow for more than one device to be connected. How will the user know the device name? I added to your code thus to return a list of com ports:

        ArrayList subKeys = WinRegistry.readStringSubKeys(WinRegistry.HKEY_LOCAL_MACHINE, keyPath);
        ArrayList comPorts = new ArrayList();
        for (String subKey : subKeys) {
            String friendlyName = getFriendlyName(keyPath + subKey);
            if (friendlyName != null && friendlyName.contains("MyDriverName") && friendlyName.contains("COM")) {
                int beginIndex = friendlyName.indexOf("COM") + 3 /*length of 'COM'*/;
                int endIndex = friendlyName.indexOf(")");
                comPorts.add(Integer.parseInt(friendlyName.substring(beginIndex, endIndex)));
            }
        }
    

    Update: I don't think these are solutions. Why? This information is statically stored in the registry - even when the device is not connected.

提交回复
热议问题