Esempio n. 1
0
        /// <summary>
        /// 通过vid,pid获得串口设备号
        /// </summary>
        /// <param name="vid">vid</param>
        /// <param name="pid">pid</param>
        /// <returns>串口号</returns>
        public int GetPortNameFromVidPid(string[] HardID, out string[] Comport)//string vid, string pid)
        {
            Guid myGUID = Guid.Empty;
            string enumerator = "USB";
            int ComCount = 0;
            Comport = new string[5];
            int j = 0;
            try
            {
                IntPtr hDevInfo = USBDevice.SetupDiGetClassDevs(ref myGUID, enumerator, IntPtr.Zero, USBDevice.DIGCF_ALLCLASSES | USBDevice.DIGCF_PRESENT);
                if (hDevInfo.ToInt32() == USBDevice.INVALID_HANDLE_VALUE)
                {
                    throw new Exception("没有该类设备");
                }
                USBDevice.SP_DEVINFO_DATA deviceInfoData;//想避免在api中使用ref,就把structure映射成类
                deviceInfoData = new USBDevice.SP_DEVINFO_DATA();
                deviceInfoData.cbSize = 28;//如果要使用SP_DEVINFO_DATA,一定要给该项赋值28=16+4+4+4
                deviceInfoData.devInst = 0;
                deviceInfoData.classGuid = System.Guid.Empty;
                deviceInfoData.reserved = 0;

                UInt32 i;
                StringBuilder property = new StringBuilder(USBDevice.MAX_DEV_LEN);
                for (i = 0; USBDevice.SetupDiEnumDeviceInfo(hDevInfo, i, deviceInfoData); i++)
                {
                    USBDevice.SetupDiGetDeviceRegistryProperty(hDevInfo, deviceInfoData,
                                                                          (uint)USBDevice.SPDRP_.SPDRP_CLASS,
                                                                            0,
                                                                            property,
                                                                            (uint)property.Capacity,
                                                                            IntPtr.Zero);
                    if (property.ToString().ToLower() != "ports")
                    {
                        continue;//首先看看是不是串口设备(有些USB设备不是串口设备)
                    }
                    USBDevice.SetupDiGetDeviceRegistryProperty(hDevInfo,
                                                                       deviceInfoData,
                                                                        (uint)USBDevice.SPDRP_.SPDRP_HARDWAREID,
                                                                        0,
                                                                        property,
                                                                        (uint)property.Capacity,
                                                                        IntPtr.Zero);
                    //if (!(property.ToString().ToLower().Contains(vid.ToLower())
                    //    && property.ToString().ToLower().Contains(pid.ToLower())))
                    int Count = 0;
                    for (j = 0; j < HardID.Length; j++)
                    {
                        if (!(property.ToString().ToLower().Contains(HardID[j].ToLower())))
                        {
                            Count++;
                            continue;//未找到对应于vid&pid的设备  
                        }

                    }
                    if (Count == HardID.Length)
                    {
                        continue;
                    }
                    USBDevice.SetupDiGetDeviceRegistryProperty(hDevInfo,
                                                                        deviceInfoData,
                                                                        (uint)USBDevice.SPDRP_.SPDRP_FRIENDLYNAME,
                                                                        0,
                                                                        property,
                                                                        (uint)property.Capacity,
                                                                        IntPtr.Zero);

                    string friendlyName = property.ToString();
                    string pattern = @"(COM[1-9][0-9]?)"; //@"(COM[1-9][0-9]?)$";//friendlyName一般形式为以(COMn)结尾,n为1-99
                    if (Regex.IsMatch(friendlyName, pattern, RegexOptions.IgnoreCase))
                    {
                        string portName = Regex.Match(friendlyName, pattern, RegexOptions.IgnoreCase).Value;
                        Comport[ComCount] = portName.Trim(new char[] { '(', ')' });
                        ComCount++;
                    }

                }
                USBDevice.SetupDiDestroyDeviceInfoList(hDevInfo);//记得用完释放相关内存
                return ComCount;
                //   return 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return 0;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 通过vid,pid获得串口设备号
        /// </summary>
        /// <param name="vid">vid</param>
        /// <param name="pid">pid</param>
        /// <returns>串口号</returns>
        public int GetAdbdevices(string HardID, out string[] Comport)//string vid, string pid)
        {
            Guid myGUID = Guid.Empty;
            string enumerator = "USB";
            int ComCount = 0;
            Comport = new string[5];
            int j = 0;
            try
            {
                IntPtr hDevInfo = USBDevice.SetupDiGetClassDevs(ref myGUID, enumerator, IntPtr.Zero, USBDevice.DIGCF_ALLCLASSES | USBDevice.DIGCF_PRESENT);
                if (hDevInfo.ToInt32() == USBDevice.INVALID_HANDLE_VALUE)
                {
                    throw new Exception("没有该类设备");
                }
                USBDevice.SP_DEVINFO_DATA deviceInfoData;//想避免在api中使用ref,就把structure映射成类
                deviceInfoData = new USBDevice.SP_DEVINFO_DATA();
                deviceInfoData.cbSize = 28;//如果要使用SP_DEVINFO_DATA,一定要给该项赋值28=16+4+4+4
                deviceInfoData.devInst = 0;
                deviceInfoData.classGuid = System.Guid.Empty;
                deviceInfoData.reserved = 0;

                UInt32 i;
                StringBuilder property = new StringBuilder(USBDevice.MAX_DEV_LEN);
                for (i = 0; USBDevice.SetupDiEnumDeviceInfo(hDevInfo, i, deviceInfoData); i++)
                {
                    USBDevice.SetupDiGetDeviceRegistryProperty(hDevInfo, deviceInfoData,
                                                                          (uint)USBDevice.SPDRP_.SPDRP_CLASS,
                                                                            0,
                                                                            property,
                                                                            (uint)property.Capacity,
                                                                            IntPtr.Zero);

                    USBDevice.SetupDiGetDeviceRegistryProperty(hDevInfo,
                                                                       deviceInfoData,
                                                                        (uint)USBDevice.SPDRP_.SPDRP_HARDWAREID,
                                                                        0,
                                                                        property,
                                                                        (uint)property.Capacity,
                                                                        IntPtr.Zero);
                    //if (!(property.ToString().ToLower().Contains(vid.ToLower())
                    //    && property.ToString().ToLower().Contains(pid.ToLower())))
                    if (property.ToString().ToLower() == "ports")
                    {
                        continue;//首先看看是不是串口设备(有些USB设备不是串口设备)
                    }
                    if (!(property.ToString().ToLower().Contains(HardID.ToLower())))
                    {
                        continue;//未找到对应于vid&pid的设备  
                    }



                    USBDevice.SetupDiGetDeviceRegistryProperty(hDevInfo,
                                                                        deviceInfoData,
                                                                        (uint)USBDevice.SPDRP_.SPDRP_FRIENDLYNAME,
                                                                        0,
                                                                        property,
                                                                        (uint)property.Capacity,
                                                                        IntPtr.Zero);

                    string friendlyName = property.ToString();
                    MessageBox.Show(friendlyName);

                }
                USBDevice.SetupDiDestroyDeviceInfoList(hDevInfo);//记得用完释放相关内存
                return ++ComCount;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return 0;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 通过串口号获得对应设备的硬件ID(里面包括该串口设备的vid和pid)
        /// </summary>
        /// <param name="portName">串口号</param>
        /// <returns></returns>
        public string GetHardWareIdFromPortName(string portName)
        {
            //先判断当前机器是否有该串口号
            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            //  int k;
            //if (!DataOperator.IsContainElement(ports, portName, out k))
            //{
            //    return null;
            //}
            Guid myGUID = Guid.Empty;
            string enumerator = "USB";
            try
            {
                IntPtr hDevInfo = USBDevice.SetupDiGetClassDevs(ref myGUID, enumerator, IntPtr.Zero, USBDevice.DIGCF_ALLCLASSES | USBDevice.DIGCF_PRESENT);
                if (hDevInfo.ToInt32() == USBDevice.INVALID_HANDLE_VALUE)
                {
                    throw new Exception("没有该类设备");
                }
                USBDevice.SP_DEVINFO_DATA deviceInfoData;//想避免在api中使用ref,就把structure映射成类
                deviceInfoData = new USBDevice.SP_DEVINFO_DATA();
                deviceInfoData.cbSize = 28;//如果要使用SP_DEVINFO_DATA,一定要给该项赋值28=16+4+4+4
                deviceInfoData.devInst = 0;
                deviceInfoData.classGuid = System.Guid.Empty;
                deviceInfoData.reserved = 0;
                UInt32 i;
                StringBuilder property = new StringBuilder(USBDevice.MAX_DEV_LEN);
                for (i = 0; USBDevice.SetupDiEnumDeviceInfo(hDevInfo, i, deviceInfoData); i++)
                {
                    USBDevice.SetupDiGetDeviceRegistryProperty(hDevInfo, deviceInfoData,
                                                                       (uint)USBDevice.SPDRP_.SPDRP_CLASS,
                                                                        0,
                                                                        property,
                                                                        (uint)property.Capacity,
                                                                        IntPtr.Zero);
                    if (property.ToString().ToLower() != "ports")
                    {
                        continue;//首先看看是不是串口设备(有些USB设备不是串口设备)
                    }
                    USBDevice.SetupDiGetDeviceRegistryProperty(hDevInfo,
                                                                       deviceInfoData,
                                                                       (uint)USBDevice.SPDRP_.SPDRP_FRIENDLYNAME,
                                                                        0,
                                                                        property,
                                                                        (uint)property.Capacity,
                                                                        IntPtr.Zero);
                    if (!property.ToString().ToLower().Contains(portName.ToLower()))
                    {
                        continue;//找到对应于portName的设备
                    }
                    USBDevice.SetupDiGetDeviceRegistryProperty(hDevInfo,
                                                                        deviceInfoData,
                                                                        (uint)USBDevice.SPDRP_.SPDRP_HARDWAREID,
                                                                        0,
                                                                        property,
                                                                        (uint)property.Capacity,
                                                                        IntPtr.Zero);
                    break;

                }
                USBDevice.SetupDiDestroyDeviceInfoList(hDevInfo);
                return property.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }