在 Windows 7 下从 .NET 应用程序中查找 USB 串行端口

Finding USB serial ports from a .NET application under Windows 7(在 Windows 7 下从 .NET 应用程序中查找 USB 串行端口)

本文介绍了在 Windows 7 下从 .NET 应用程序中查找 USB 串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序可以查找具有自定义 USB 描述符的特定 FTDI 串行端口.我当前的代码使用 Code Project 中的示例,它搜索MSSerial_PortName rootWMI 下的 WMI 表,并从 rootCIMV2WIN32_PnPEntity 中提取额外的 USB 信息.

I have an application that looks for a specific FTDI serial port with customised USB descriptors. My current code uses the example from Code Project, which searches the MSSerial_PortName WMI table under rootWMI, and pulls out extra USB information from rootCIMV2WIN32_PnPEntity.

这在 XP 下运行良好,但应用程序也必须在 Windows 7 上的标准用户下运行.在这种环境下,rootWMI 的访问会导致访问被拒绝"ManagementException.

This worked well under XP, but the application must also run under a standard user onWindows 7. In this environment access of rootWMI results in an "Access Denied" ManagementException.

在以标准用户身份运行时,任何人都可以建议一种将串行端口的 DOS 设备名称与 USB 信息交叉引用的方法吗?到目前为止,我查看了 rootCIMV2WIN32_SerialPort* 表,但它们只包含主板端口.我也考虑过使用 SetupAPI,但我还没有找到一个完整且有效的 PInvoke 模板.

Can anybody suggest a way to cross reference the DOS device name of a serial port to the USB information, while running as a standard user? So far I've looked at the rootCIMV2WIN32_SerialPort* tables, but they only contain motherboard ports. I've also considered using SetupAPI, but I haven't found a complete and working PInvoke template for this.

推荐答案

我发现了一个适合我们案例的答案,虽然不是一个通用的答案.我们的 USB 转换器都是 FTDI,FTDI 提供了一个 DLL 来处理这个.我使用 DLL 的代码如下:

I've discovered an answer suitable for our case, though not a generic one. Our USB converters are all FTDI, and FTDI provide a DLL that handles this. My code using the DLL is below:

UInt32 count = 0;
FTDI.FT_STATUS status = ftdi.GetNumberOfDevices(ref count);
if (status != FTDI.FT_STATUS.FT_OK)
{
    log.Warn("Unable to access FTDI");
    return ports;
}
FTDI.FT_DEVICE_INFO_NODE[] list = new FTDI.FT_DEVICE_INFO_NODE[count];
status = ftdi.GetDeviceList(list);
if (status != FTDI.FT_STATUS.FT_OK)
{
    log.Warn("Unable to access FTDI");
    return ports;
}
foreach (FTDI.FT_DEVICE_INFO_NODE node in list)
{
    if ((status = ftdi.OpenByLocation(node.LocId)) == FTDI.FT_STATUS.FT_OK)
    {
        try
        {
            string comport;
            ftdi.GetCOMPort(out comport);
            ports.Add(new Port(comport, node.Description, node.SerialNumber));
        }
        finally
        {
            ftdi.Close();
        }
    }
}

这篇关于在 Windows 7 下从 .NET 应用程序中查找 USB 串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Windows 7 下从 .NET 应用程序中查找 USB 串行端口

基础教程推荐