C# 中 SerialPort.GetPortNames 获取串口号错误的问题及解决方法

开发环境:VS2008+.NET3.5

以前一直没发现这个问题,感觉SerialPort.GetPortNames方法很好用,只需要这么一行就能直接获取到系统的串口列表。

但当我们系统中存在虚拟串口时(部分蓝牙设备或者手机接上电脑后会虚拟出一些串口),此方法获取到的串口号可能就不是我们想要的结果了,如图:

对比图片红色标记区域会发现,.net提供的方法获取的串口号后面带有一个特殊字符,如果仅是特殊字符那还好说,过滤一下就好了,但你GOOGLE会发现,有不少朋友碰到过这样的问题,他们串口号后面多出的字符可能是字母,数字,或者跟我一样是特殊字符,后两种情况都还好说,但如果多出的是数字呢(例如“COM3”变成了“COM36”)?

起初我也只是过滤一下非数字,但在生产环境下什么情况都有可能发生,所以又改成了调用系统API读取注册表并处理读取结果的方案,以下为我的代码:

        [DllImport("advapi32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        private static extern int RegCloseKey(int hKey);

        [DllImport("advapi32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true, EntryPoint = "RegOpenKeyA")]
        private static extern int RegOpenKey(uint hKey, string lpSubKey, ref int phkResult);

        [DllImport("advapi32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true, EntryPoint = "RegEnumValueA")]
        private static extern int RegEnumValue(int hKey, int dwIndex, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpValueName, ref int lpcbValueName, int lpReserved, int lpType, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpData, ref int lpcbData);
        /// <summary>
        /// 获取当前计算机的串口名称数组
        /// </summary>
        /// <returns></returns>
        public static string[] GetSerialPortNames()
        {
            #region 方式一:调用系统API(DLL文件:advapi32.dll)读取注册表,并处理读取结果的“字符串结束符”
            string[] ports = null;
            List<string> portsList = new List<string>();
            uint HKEY_LOCAL_MACHINE = 0x80000002;
            int hKey = -1;
            int ret = RegOpenKey(HKEY_LOCAL_MACHINE, @"Hardware\DEVICEMAP\SERIALCOMM", ref hKey);
            try
            {
                if (ret == 0)
                {
                    int index = 0;
                    int BufferSize = 255;
                    int ERROR_NO_MORE_ITEMS = 259;
                    string valueName = "".PadRight(BufferSize, ' ');
                    int valueNameLength = BufferSize;
                    int valueLength = BufferSize;
                    string value = "".PadRight(BufferSize, ' ');
                    while (RegEnumValue(hKey, index, ref valueName, ref valueNameLength, 0, 0, ref value, ref valueLength) != ERROR_NO_MORE_ITEMS)
                    {
                        if (valueLength > 0)
                        {
                            if (value[valueLength - 1] == 0)
                                valueLength -= 1;
                            portsList.Add(value.Substring(0, valueLength));
                        }
                        index += 1;
                        valueName = "".PadRight(BufferSize, ' ');
                        valueNameLength = BufferSize;
                        valueLength = BufferSize;
                    }
                }
            }
            catch (Exception)
            {
            }
            finally 
            {
                if (ret == 0)
                    RegCloseKey(hKey);
            }
            if (portsList.Count == 0)
                ports = new string[0];
            else
                ports = portsList.ToArray();
            return ports;
            #endregion

            #region 方式二:C#方式读取注册表后过滤非数字(此方式无法过滤数字,可能造成部分虚拟串口获取错误,例如 COM3 识别为 COM32)
            //HashSet<char> numbers = new HashSet<char>(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
            //string[] ports = null;

            //RegistryKey localMachine = null;
            //RegistryKey subKey = null;
            //new RegistryPermission(RegistryPermissionAccess.Read, @"HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM").Assert();
            //try
            //{
            //    localMachine = Registry.LocalMachine;
            //    subKey = localMachine.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM", false);
            //    if (subKey != null)
            //    {
            //        string[] valueNames = subKey.GetValueNames();
            //        ports = new string[valueNames.Length];
            //        for (int j = 0; j < valueNames.Length; j++)
            //        {
            //            object obj = subKey.GetValue(valueNames[j]);
            //            if (obj == null)
            //                continue;
            //            string str = (string)obj;
            //            if (string.IsNullOrEmpty(str) || str.Length < 4)
            //                continue;

            //            string port = "COM";
            //            char[] cs = str.Substring(3).Replace("\n", "").ToCharArray();
            //            foreach (char c in cs)
            //            {
            //                if (numbers.Contains(c))
            //                    port += c.ToString();
            //                else
            //                    break;
            //            }
            //            ports[j] = port;
            //        }
            //    }
            //}
            //finally
            //{
            //    if (localMachine != null)
            //        localMachine.Close();
            //    if (subKey != null)
            //        subKey.Close();
            //    System.Security.CodeAccessPermission.RevertAssert();
            //}

            //if (ports == null)
            //    ports = new string[0];
            //return ports;
            #endregion
        }
   

另外,针对以上问题,微软其实在08年的时候给过回复,说在下一版本.net(也就是.net Framework 4)中解决此问题,确实,在.net Framework 4 中已经不会存在此问题,所以如果有条件的话大家还是用.net Framework 4 开发比较好些,没办法必须得用3.5的话那就试试上面所说的方法吧。经测试4.0确实没有上问题了!

原文地址:https://www.cnblogs.com/mic86/p/2551771.html