简体   繁体   English

获取 USB 适配器的 com 端口号

[英]get com port number of an USB adapter

I am trying to write a program that uses an arduino mega and a FTDI-based USB to RS485 adapter.我正在尝试编写一个使用 arduino mega 和基于 FTDI 的 USB 转 RS485 适配器的程序。

I want to make the program user-friendly, thus I don't wont the user to manually check the com port number, but I want to auto-detect it.我想让程序对用户友好,因此我不会让用户手动检查 com 端口号,但我想自动检测它。 Here's a snippet of the code这是代码的片段

    ManagementScope scope = new ManagementScope();
    SelectQuery query = new SelectQuery("SELECT * FROM Win32_SerialPort");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

    try
    {
        foreach (ManagementObject item in searcher.Get())
        {
            String description = item["Description"].ToString();
            String deviceID = item["DeviceID"].ToString();

            Console.WriteLine("Porta " + description + " deviceID " + deviceID);

            if (description.Contains("USB Serial Port"))
                return deviceID;
        }
    }
    catch (ManagementException)
    {
    }

The point that I am not able to understand is why I can find the USB Serial port of the Arduino (matching description.Contains("Arduino") ) but not the com port of the USB RS485 port.我无法理解的一点是为什么我可以找到 Arduino 的 USB 串行端口(匹配description.Contains("Arduino") )而不是 USB RS485 端口的 com 端口。

Do you have an idea why this could happen?你知道为什么会发生这种情况吗? is the query SELECT * FROM Win32_SerialPort wrong?查询SELECT * FROM Win32_SerialPort错误吗?

Well, after studying several forums, I discovered that the com port associated to the USB/RS485 adapter is listed if I run the query SELECT * FROM Win32_PnPEntity .好吧,在研究了几个论坛之后,我发现如果我运行查询SELECT * FROM Win32_PnPEntity就会列出与 USB/RS485 适配器关联的 com 端口。

But I really don't understand why the serial port of the arduino is shown by a query and the other port with the other query.... I mean, those are both uSB<->serial adapters!但我真的不明白为什么 arduino 的串行端口显示为查询而另一个端口显示另一个查询......我的意思是,那些都是 uSB<-> 串行适配器!

I don't have a USB<->RS485 adapter, but I suspect they don't fall into the "serial port" category (which seems reserved for RS232 interfaces), which could be the reason why they don't appear among the Win32_SerialPort query results.我没有 USB<->RS485 适配器,但我怀疑它们不属于“串行端口”类别(这似乎是为 RS232 接口保留的),这可能是它们没有出现在Win32_SerialPort 查询结果。

To detect the connection of and Arduino board I look into this registry key:为了检测和 Arduino 板的连接,我查看了这个注册表项:

HKLM\HARDWARE\DEVICEMAP\SERIALCOMM

for entries like对于像这样的条目

\Device\VCP0

(VCP is the prefix to look for). (VCP 是要查找的前缀)。

Maybe you can look into this registry key too, or watch its parent key, DEVICEMAP, and see what happens when you connect the FTDI RS485 adapter.也许您也可以查看此注册表项,或查看其父项 DEVICEMAP,看看连接 FTDI RS485 适配器时会发生什么。 Detecting a change in one of these registry keys contents should be straightforward at that point.此时检测这些注册表项内容之一的更改应该很简单。

HTH HTH

This might work for you.这可能对你有用。 I used this to dynamically read port number of Arduino on a system.我用它来动态读取系统上 Arduino 的端口号。 Here这里

description.Contains("uino") description.Contains("uino")

is to look for both Arduino and Genuino keyword for both varients of board.是为两种板子寻找 Arduino 和 Genuino 关键字。

    public string detectArduinoPort()
    {
        ManagementScope mScope = new ManagementScope();
        SelectQuery query = new SelectQuery("SELECT * FROM Win32_SerialPort");
        ManagementObjectSearcher objectList = new ManagementObjectSearcher(mScope, query);

        try
        {
            foreach (ManagementObject obj in objectList.Get())
            {
                string description = obj["Description"].ToString();
                string deviceId = obj["DeviceID"].ToString();

                if (description.Contains("uino"))
                {
                    return deviceId;
                }
            }
        }
        catch (Exception)
        {

        }
        return "";
    }

as @Marcello Romani pointed out FTDI don't fall into the "serial port" category.正如@Marcello Romani 指出的那样,FTDI 不属于“串行端口”类别。 Even though I am late I came across this problem recently and I fixed it using another query.即使我迟到了,我最近也遇到了这个问题,并使用另一个查询修复了它。 Instead of searching in WIN32_SerialPort, you can query the Win32_PnPEntity class.One downside of using this class is that is slow to query.您可以查询 Win32_PnPEntity 类,而不是在 WIN32_SerialPort 中搜索。使用此类的一个缺点是查询速度很慢。

 using (var searcher = new ManagementObjectSearcher
            ("SELECT * FROM Win32_PnPEntity"))
        {
            string[] portnames = SerialPort.GetPortNames();
            var ports = searcher.Get().Cast<ManagementBaseObject>().ToList();

            foreach (ManagementBaseObject queryObj in ports)
            {
            }

        }

Basically, this is due to the HID (Human Interface Device)基本上,这是由于 HID(人机接口设备)

I think that this is due to the HID provided by the arduino board.我认为这是由于arduino板提供的HID。 Arduino hardware (and thus, the microcontroller) communicates directely with the PC using HID (and so, no need for additional device). Arduino 硬件(以及微控制器)使用 HID 直接与 PC 通信(因此,不需要额外的设备)。 And HID can behave as a mouse, keyboard, or simply a serial device (it "simply" declare itself to the PC as a Serial Comport). HID 可以充当鼠标、键盘或简单的串行设备(它“简单地”向 PC 声明自己为串行端口)。 So somehow, there is some "intelligent" way when communicating with the PC.所以不知何故,在与 PC 通信时有一些“智能”的方式。

On the other hand, USBtoSerial converters and in some Arduino boards, like the Arduino Nano which embed an FTDI (or CH340), these are "obliged" to behave as a normal comport cause there is no intelligence behind (the the one behind the HID).另一方面,USBtoSerial 转换器和一些 Arduino 板,如嵌入 FTDI(或 CH340)的 Arduino Nano,这些“有义务”作为正常通信运行,因为背后没有智能(HID 后面的那个) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM