2017-07-18 439 views
1

我目前正在開發一個C#程序,與Arduino微控制器進行交互。該程序有一個組合框,您可以在其中選擇COM端口。 μc通過USB和虛擬COM端口(CH340)連接。WMI:獲取所有串行(COM)端口列表,包括虛擬端口

我使用下面的代碼填充組合框的可用COM端口。

private void Form1_Load(object sender, EventArgs e) 
    { 
     string[] PortNames = SerialPort. GetPortNames(); 
     comboBoxPort.Items.AddRange(PortNames); 
    } 

這樣做的缺點是,你必須看看進入設備管理器,看看哪一個是正確的爲μC。例如我的電腦有4個活動COM端口,一個物理端口,2個虛擬端口和另一個虛擬端口。我正在尋找與相關的COM口,以顯示該設備的完整名稱(比如,你可以在設備管理器中找到它)

COM-ports in the Device Manager

後位的研究,我發現了一種方法通過使用WMI有另一種可能性。經過對「WMI Code Creator」的大量測試後,我不知道還有什麼可以嘗試完成我參加過的活動。我嘗試過的所有命名空間和類只生成像COM1,COM2這樣的COM端口,或者它們生成對程序用戶無用的硬件ID。 下面的代碼或多或少正是我正在尋找的,但它只適用於COM端口中的「真實」構建。

using System; 
using System.Management; 
using System.Windows.Forms; 

namespace WMISample 
    { 
     public class MyWMIQuery 
     { 
      public static void Main() 
      { 
       try 
       { 
        ManagementObjectSearcher searcher = 
         new ManagementObjectSearcher("root\\CIMV2", 
         "SELECT * FROM Win32_SerialPort"); 

        foreach (ManagementObject queryObj in searcher.Get()) 
        { 
         Console.WriteLine("-----------------------------------"); 
         Console.WriteLine("Win32_SerialPort instance"); 
         Console.WriteLine("-----------------------------------"); 
         Console.WriteLine("Name: {0}", queryObj["Name"]); 
        } 
       } 
       catch (ManagementException e) 
       { 
        MessageBox.Show("An error occurred while querying for WMI data: " + e.Message); 
       } 
      } 
     } 
    } 

是否有任何其他可能的方式獲得所有COM端口的列表,如有一個設備管理器的內幕?是否有可能使用設備的硬件ID來以某種方式識別它們,然後在第二步中爲它們獲取正確的名稱?

如果我能得到一些幫助,我會非常高興。必須有辦法做到這一點,但我找不到它。

和SRY我的英語水平,如果事情聽起來很奇怪:)

+0

[參考這個解決方案(https://stackoverflow.com/a/2876126) –

+0

THX的答案,但是這一次也僅適用於「真實的」物理COM端口的其他三個虛擬端口獲得忽略。 –

+0

我會去在Windows註冊表中搜索。這些信息必須在那裏。 – Oscar

回答

0

由於mentiont之前這裏是完整的工作代碼,以填補所有avaible COM端口組合框和seletion後設置相關的端口。

(原來的答案如何獲得端口名稱 - >link

感謝@o_O的鏈接,我希望有人能發現此代碼非常有用。

private void Form1_Load(object sender, EventArgs e) 
{ 
    // Get all serial (COM)-ports you can see in the devicemanager 
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\cimv2", 
     "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=\"{4d36e978-e325-11ce-bfc1-08002be10318}\""); 

    // Sort the items in the combobox 
    CmdBoxPort.Sorted = true; 

    // Add all available (COM)-ports to the combobox 
    foreach (ManagementObject queryObj in searcher.Get()) 
     CmdBoxPort.Items.Add(queryObj["Caption"]); 
} 

private void CmdBoxPort_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    // Set the right port for the selected item. 
    // The portname is based on the "COMx" part of the string (SelectedItem) 
    string item = CmdBoxPort.SelectedItem.ToString(); 

    // Search for the expression "(COM" in the "selectedItem" string 
    if (item.Contains("(COM")) 
    { 
     // Get the index number where "(COM" starts in the string 
     int indexOfCom = item.IndexOf("(COM"); 

     // Set PortName to COMx based on the expression in the "selectedItem" string 
     // It automatically gets the correct length of the COMx expression to make sure 
     // that also a COM10, COM11 and so on is working properly. 
     serialPort1.PortName = item.Substring(indexOfCom + 1, item.Length - indexOfCom - 2); 
    } 
    else 
     return; 
}