获取 USB 适配器的 com 端口号

get com port number of an USB adapter(获取 USB 适配器的 com 端口号)

本文介绍了获取 USB 适配器的 com 端口号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个使用 arduino mega 和基于 FTDI 的 USB 转 RS485 适配器的程序.

I am trying to write a program that uses an arduino mega and a FTDI-based USB to RS485 adapter.

我想让程序对用户友好,因此我不希望用户手动检查 com 端口号,但我想自动检测它.这是代码片段

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. 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)
    {
    }

我无法理解的一点是为什么我可以找到 Arduino 的 USB 串行端口(匹配 description.Contains("Arduino") )但找不到 USB 的 com 端口RS485 接口.

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.

您知道为什么会发生这种情况吗?查询 SELECT * FROM Win32_SerialPort 错了吗?

Do you have an idea why this could happen? is the query SELECT * FROM Win32_SerialPort wrong?

推荐答案

嗯,研究了几个论坛后,我发现如果我运行查询 SELECT * FROM 会列出与 USB/RS485 适配器关联的 com 端口Win32_PnPEntity .

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 .

但我真的不明白为什么 arduino 的串行端口由查询显示,而另一个端口与另一个查询显示......我的意思是,它们都是 uSB<-> 串行适配器!

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!

这篇关于获取 USB 适配器的 com 端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:获取 USB 适配器的 com 端口号

基础教程推荐