获取与COM端口相关的设备/驱动程序信息? [英] Getting device/driver information related to a COM port?

查看:442
本文介绍了获取与COM端口相关的设备/驱动程序信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows设备管理器中有一个串口到USB的设备,其中有一个类似命名的设备驱动程序。设备并不总是在系统启动时获取相同的COM端口,因此我的程序需要在启动时识别它。

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 program needs to identify it on start up.

我尝试过使用 RXTX 枚举系统上的COM端口,但这不起作用,因为 CommPortIdentifier.getName()只返回COM名称(例如COM1,COM2等)我需要获取驱动程序制造商名称或设备管理器中显示的驱动程序名称,并将其与COM名称相关联。

I've tried using RXTX to enumerate the COM ports on the system, but this didn't work because CommPortIdentifier.getName() simply returns the COM name (eg. COM1, COM2, etc.) I need to acquire either the driver manufacturer name, or the driver name as it appears in the device manager, and associate it with the COM name.

这可以用Java轻松完成吗? (我对任何支持它的第三方Java库感兴趣。)否则,我如何通过win32 API开始实现这一点?

Can this easily be done in Java? (I'd be interested in any 3rd party Java libraries that support this.) Otherwise, how I could begin to accomplish this via the win32 API?

推荐答案

我使用 WinRegistry 类实现了我的目标read-write-to-windows-registry-using-java>这个问题从与我的USB设备关联的注册表项中获取 FriendlyName 。然后我从友好名称解析出COM号码。

I achieved what I wanted by using the WinRegistry class provided by David in this SO question to obtain the FriendlyName from registry key associated with my USB device. I then parse out the COM number from the friendly name.

需要考虑的事项:


  1. USB设备位于注册表中的 HKEY_LOCAL_MACHINE \SYSTEM \ CurrentControlSet \ Enum \ _USB \ (在WinXP,Win7上测试)。 )

  1. USB devices are located at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\ in the registry (tested on WinXP, Win7.)

我要求设备VID + PID识别正确的设备密钥(例如 VID_xxxx& PID_xxxx 。)由于VID和PID是特定于设备的,因此该密钥在多个系统中应该是可靠的。

I required the device VID + PID to identify the correct device key (eg. VID_xxxx&PID_xxxx.) Since VID and PID are device specific, this key should be reliable across multiple systems.

VID_xxxx& PID_xxxx key包含另一个带有设备值的子键。我在使用 WinRegistry 枚举子键时遇到了一些麻烦,因此我在开发过程中将子键名称硬编码为快速入侵。一个更安全的解决方案是搜索子键以找到正确的名称。

The VID_xxxx&PID_xxxx key contains another sub-key with device values. I had some trouble enumerating the sub-keys with WinRegistry, so I hard-coded the sub-key name as a quick hack during development. A much safer solution would search sub-keys to find the correct name.

无论设备当前是否连接,设备密钥都存在于注册表中。此代码假设如果设备重新连接到其他COM端口,Windows将更新 FriendlyName 。我没有验证这一点,但在使用测试期间看起来很不错。

The device keys exist in the registry regardless of whether the device is currently connected. This code makes the assumption that Windows will update FriendlyName if the device is reconnected to a different COM port. I haven't verified this, but things looked good during use-testing.



示例



Example

String keyPath = "SYSTEM\\CurrentControlSet\\Enum\\USB\\Vid_067b&Pid_2303\\";
String device1 = "5&75451e6&0&1";
System.out.println("First COM device: " + getComNumber(keyPath + device1));



代码



Code

import java.util.regex.Pattern;
import java.util.regex.Matcher;

// Given a registry key, attempts to get the 'FriendlyName' value
// Returns null on failure.
//
public static String getFriendlyName(String registryKey) {
    if (registryKey == null || registryKey.isEmpty()) {
        throw new IllegalArgumentException("'registryKey' null or empty");
    }
    try {
        int hkey = WinRegistry.HKEY_LOCAL_MACHINE;
        return WinRegistry.readString(hkey, registryKey, "FriendlyName");
    } catch (Exception ex) { // catch-all: 
        // readString() throws IllegalArg, IllegalAccess, InvocationTarget
        System.err.println(ex.getMessage());
        return null;
    }
}

// Given a registry key, attempts to parse out the integer after
// substring "COM" in the 'FriendlyName' value; returns -1 on failure.
//
public static int getComNumber(String registryKey) {
    String friendlyName = getFriendlyName(registryKey);

    if (friendlyName != null && friendlyName.indexOf("COM") >= 0) {
        String substr = friendlyName.substring(friendlyName.indexOf("COM"));
        Matcher matchInt = Pattern.compile("\\d+").matcher(substr);
        if (matchInt.find()) {
            return Integer.parseInt(matchInt.group());
        }
    }
    return -1;
}   

这篇关于获取与COM端口相关的设备/驱动程序信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆