2013-02-20 63 views
2

在這個程序中,我首先嚐試連接到availalbe端口。 找到並連接後,我想要讀取連接的USB設備ID和供應商ID,我該怎麼做?如何獲得串行端口設備ID?

親切的問候

Program() 
    { 

     // Get a list of serial port names. 
     string[] ports = SerialPort.GetPortNames(); 

     // Search for the right port. 
     foreach (string port in ports) 
     { 
      _serialPort = new SerialPort(port, 250000, Parity.None, 8, StopBits.One); 
      _serialPort.Handshake = Handshake.None; 
      _serialPort.ReadTimeout = 300; 
      _serialPort.WriteTimeout = 300; 

      try 
      { 
       _serialPort.Open(); 
       break; 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("Serial port " + port + ": " + e.Message); 
      } 
     } 
     /* ENTER CODE TO GET ID HERE */ 

     Console.WriteLine("Using: " + _serialPort.PortName); 
     Console.WriteLine("Device ID: " + _serialPort.DeviceID); 
+3

串口收集它不是USB端口,只是COM端口。 – 2013-02-20 23:00:10

+0

[需要知道設備是有線串行還是藍牙]可能的重複(http://stackoverflow.com/questions/3659939/need-to-know-whether-a-device-is-wired-serial-or-藍牙) – 2013-02-20 23:21:18

+0

要添加一些額外的信息:當我寫這篇文章我雖然你hade打開端口/連接recive供應商ID或設備ID。我正在爲Windows編寫此代碼,通過這樣做我應該從Windows註冊表中收集這類信息。我試圖讓一個可以找到合適的設備然後運行的代碼。 – Christian 2013-02-22 16:21:38

回答

4

我終於整理出了這自己前幾天。有兩個部分,一個檢查註冊表,另一個檢查設備的vid/pid。

我使用的註冊表方法只是爲了確保我不捕獲像com0com這樣的null調制解調器仿真程序。

/// <summary> 
    /// Removes any comm ports that are not explicitly defined as allowed in ALLOWED_TYPES 
    /// </summary> 
    /// <param name="allPorts">reference to List that will be checked</param> 
    /// <returns></returns> 
    private static void nullModemCheck(ref List<string> allPorts) 
    { 
     // Open registry to get the COM Ports available with the system 
     RegistryKey regKey = Registry.LocalMachine; 

     // Defined as: private const string REG_COM_STRING ="HARDWARE\DEVICEMAP\SERIALCOMM"; 
     regKey = regKey.OpenSubKey(REG_COM_STRING); 

     Dictionary<string, string> tempDict = new Dictionary<string, string>(); 
     foreach (string p in allPorts) 
      tempDict.Add(p, p); 

     // This holds any matches we may find 
     string match = ""; 
     foreach (string subKey in regKey.GetValueNames()) 
     { 
      // Name must contain either VCP or Seial to be valid. Process any entries NOT matching 
      // Compare to subKey (name of RegKey entry) 
      if (!(subKey.Contains("Serial") || subKey.Contains("VCP"))) 
      { 
       // Okay, this might be an illegal port. 
       // Peek in the dictionary, do we have this key? Compare to regKey.GetValue(subKey) 
       if(tempDict.TryGetValue(regKey.GetValue(subKey).ToString(), out match))   
       { 
        // Kill it! 
        allPorts.Remove(match); 

        // Reset our output string 
        match = ""; 
       } 

      } 

     } 

     regKey.Close(); 
    } 

的VID/PID部分從techinpro

/// <summary> 
    /// Compile an array of COM port names associated with given VID and PID 
    /// </summary> 
    /// <param name="VID">string representing the vendor id of the USB/Serial convertor</param> 
    /// <param name="PID">string representing the product id of the USB/Serial convertor</param> 
    /// <returns></returns> 
    private static List<string> getPortByVPid(String VID, String PID) 
    { 
     String pattern = String.Format("^VID_{0}.PID_{1}", VID, PID); 
     Regex _rx = new Regex(pattern, RegexOptions.IgnoreCase); 
     List<string> comports = new List<string>(); 
     RegistryKey rk1 = Registry.LocalMachine; 
     RegistryKey rk2 = rk1.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum"); 
     foreach (String s3 in rk2.GetSubKeyNames()) 
     { 
      RegistryKey rk3 = rk2.OpenSubKey(s3); 
      foreach (String s in rk3.GetSubKeyNames()) 
      { 
       if (_rx.Match(s).Success) 
       { 
        RegistryKey rk4 = rk3.OpenSubKey(s); 
        foreach (String s2 in rk4.GetSubKeyNames()) 
        { 
         RegistryKey rk5 = rk4.OpenSubKey(s2); 
         RegistryKey rk6 = rk5.OpenSubKey("Device Parameters"); 
         comports.Add((string)rk6.GetValue("PortName")); 
        } 
       } 
      } 
     } 
     return comports; 
    } 
+0

太棒了。謝謝你。 – ppumkin 2014-01-28 09:25:50

+0

這會生成與PID/VID相關的端口列表,而不是實際端口的PID/VID - 也就是說,即使在拔下設備時也會生成結果。作爲USB CDC/ACM設備的製造商,我有許多與我的設備相關的端口,但我需要獲取連接設備的PID/VID,而不是歷史上關聯的設備。我使用'QextPortInfo'完成了Qt,但理想情況下需要.Net方法。 – Clifford 2016-05-25 12:11:36

+0

你是對的。獲得該信息的唯一方法(我知道)是使用libusb/winusb。這需要更多的努力來達到同樣的效果。我相信這是足夠的,因爲操作系統在打開之前必須知道該端口。因此,如果能夠實際連接,檢索到的pid/vid是最新的。 – 2016-05-25 13:47:49

相關問題