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確實沒有上問題了!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM