<small id='OKBj5'></small><noframes id='OKBj5'>

    • <bdo id='OKBj5'></bdo><ul id='OKBj5'></ul>
    <tfoot id='OKBj5'></tfoot>
    <legend id='OKBj5'><style id='OKBj5'><dir id='OKBj5'><q id='OKBj5'></q></dir></style></legend>

        <i id='OKBj5'><tr id='OKBj5'><dt id='OKBj5'><q id='OKBj5'><span id='OKBj5'><b id='OKBj5'><form id='OKBj5'><ins id='OKBj5'></ins><ul id='OKBj5'></ul><sub id='OKBj5'></sub></form><legend id='OKBj5'></legend><bdo id='OKBj5'><pre id='OKBj5'><center id='OKBj5'></center></pre></bdo></b><th id='OKBj5'></th></span></q></dt></tr></i><div id='OKBj5'><tfoot id='OKBj5'></tfoot><dl id='OKBj5'><fieldset id='OKBj5'></fieldset></dl></div>

        如何获取串行端口设备 ID?

        How do I get serial Port device id?(如何获取串行端口设备 ID?)
        • <tfoot id='XvbN9'></tfoot>
            <tbody id='XvbN9'></tbody>

              <small id='XvbN9'></small><noframes id='XvbN9'>

                <bdo id='XvbN9'></bdo><ul id='XvbN9'></ul>
                <legend id='XvbN9'><style id='XvbN9'><dir id='XvbN9'><q id='XvbN9'></q></dir></style></legend>
                  <i id='XvbN9'><tr id='XvbN9'><dt id='XvbN9'><q id='XvbN9'><span id='XvbN9'><b id='XvbN9'><form id='XvbN9'><ins id='XvbN9'></ins><ul id='XvbN9'></ul><sub id='XvbN9'></sub></form><legend id='XvbN9'></legend><bdo id='XvbN9'><pre id='XvbN9'><center id='XvbN9'></center></pre></bdo></b><th id='XvbN9'></th></span></q></dt></tr></i><div id='XvbN9'><tfoot id='XvbN9'></tfoot><dl id='XvbN9'><fieldset id='XvbN9'></fieldset></dl></div>
                1. 本文介绍了如何获取串行端口设备 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在这个程序中,我首先尝试连接到可用端口.找到并连接后,我想读取连接的 USB 设备 ID 和供应商 ID,我该怎么做?

                  In this program I'm first trying to connect to availalbe port. When found and connected, I want to read the connected USB device ID and vendor ID, How do I do that?

                  亲切的问候

                  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);
                  

                  推荐答案

                  几天前我终于自己解决了这个问题.有两个部分,一个检查注册表,另一个检查设备的 vid/pid.

                  I finally got this sorted out myself a couple days ago. There are two parts, one to check the registry and another to check the vid/pid of the device.

                  我使用的注册表方法只是为了确保我不会捕获像 com0com 这样的空调制解调器模拟器.

                  The registry method I use just to make sure I don't capture a null modem emulator like com0com.

                      /// <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 ="HARDWAREDEVICEMAPSERIALCOMM";
                          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

                  The vid/pid portion was gleaned from 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;
                      }
                  

                  这篇关于如何获取串行端口设备 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                    <legend id='XJGwD'><style id='XJGwD'><dir id='XJGwD'><q id='XJGwD'></q></dir></style></legend>

                    1. <small id='XJGwD'></small><noframes id='XJGwD'>

                    2. <tfoot id='XJGwD'></tfoot>

                        <tbody id='XJGwD'></tbody>
                        <i id='XJGwD'><tr id='XJGwD'><dt id='XJGwD'><q id='XJGwD'><span id='XJGwD'><b id='XJGwD'><form id='XJGwD'><ins id='XJGwD'></ins><ul id='XJGwD'></ul><sub id='XJGwD'></sub></form><legend id='XJGwD'></legend><bdo id='XJGwD'><pre id='XJGwD'><center id='XJGwD'></center></pre></bdo></b><th id='XJGwD'></th></span></q></dt></tr></i><div id='XJGwD'><tfoot id='XJGwD'></tfoot><dl id='XJGwD'><fieldset id='XJGwD'></fieldset></dl></div>

                          • <bdo id='XJGwD'></bdo><ul id='XJGwD'></ul>