C# SerialPort.GetPortNames()获取串口号错误

发布时间:2020-09-06编辑:佚名阅读(3024)

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
        }


    关键字: C# SerialPort.GetPortNames() 获取串口号 错误


鼓掌

0

正能量

0

0

呵呵

0


评论区