C# .net中擷取台式電腦中串口裝置的名稱

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   使用   ar   

1、情境:

做項目的時候要開啟串口然後進行一些庫函數的調用來操作目標板。串口使用的是usb轉串口,板子插進拔出的,每次都使用不一樣的usb口,debug的時候懶得每次改com口,又不想在UI上加上一個選擇

com口的combox,於是就使用了下面這個方法。

2、環境:

win7 64、vs2010

 

3、目標:

擷取的裝置到底使用的是com幾。

4、source codery>

 

  1         /// <summary>  2         /// Get the target com num.  3         /// </summary>  4         /// <returns></returns>  5         public static int GetComNum()  6         {  7             int comNum = -1;  8             string[] strArr = GetHarewareInfo(HardwareEnum.Win32_PnPEntity, "Name");  9             foreach (string s in strArr) 10             { 11                 Debug.WriteLine(s); 12  13                 if (s.Length >= 23 && s.Contains("CH340")) 14                 { 15                     int start = s.IndexOf("(") + 3; 16                     int end = s.IndexOf(")"); 17                     comNum = Convert.ToInt32(s.Substring(start + 1, end - start - 1)); 18                 } 19             } 20  21             return comNum; 22  23         } 24  25         /// <summary> 26         /// Get the system devices information with windows api. 27         /// </summary> 28         /// <param name="hardType">Device type.</param> 29         /// <param name="propKey">the property of the device.</param> 30         /// <returns></returns> 31         private static string[] GetHarewareInfo(HardwareEnum hardType, string propKey) 32         { 33  34             List<string> strs = new List<string>(); 35             try 36             { 37                 using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + hardType)) 38                 { 39                     var hardInfos = searcher.Get(); 40                     foreach (var hardInfo in hardInfos) 41                     { 42                         if (hardInfo.Properties[propKey].Value != null) 43                         { 44                             String str = hardInfo.Properties[propKey].Value.ToString(); 45                             strs.Add(str); 46                         } 47  48                     } 49                 } 50                 return strs.ToArray(); 51             } 52             catch 53             { 54                 return null; 55             } 56             finally 57             {  58                 strs = null; 59             } 60         }//end of func GetHarewareInfo(). 61  62     /// <summary> 63     /// 枚舉win32 api 64     /// </summary> 65     public enum HardwareEnum 66     { 67         // 硬體 68         Win32_Processor, // CPU 處理器 69         Win32_PhysicalMemory, // 實體記憶體條 70         Win32_Keyboard, // 鍵盤 71         Win32_PointingDevice, // 點輸入裝置,包括滑鼠。 72         Win32_FloppyDrive, // 軟碟機 73         Win32_DiskDrive, // 硬碟 74         Win32_CDROMDrive, // 光碟片磁碟機 75         Win32_BaseBoard, // 主板 76         Win32_BIOS, // BIOS 晶片 77         Win32_ParallelPort, // 並口 78         Win32_SerialPort, // 串口 79         Win32_SerialPortConfiguration, // 串口配置 80         Win32_SoundDevice, // 多媒體設定,一般指音效卡。 81         Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP) 82         Win32_USBController, // USB 控制器 83         Win32_NetworkAdapter, // 網路介面卡 84         Win32_NetworkAdapterConfiguration, // 網路介面卡設定 85         Win32_Printer, // 印表機 86         Win32_PrinterConfiguration, // 印表機設定 87         Win32_PrintJob, // 印表機任務 88         Win32_TCPIPPrinterPort, // 印表機連接埠 89         Win32_POTSModem, // MODEM 90         Win32_POTSModemToSerialPort, // MODEM 連接埠 91         Win32_DesktopMonitor, // 顯示器 92         Win32_DisplayConfiguration, // 顯卡 93         Win32_DisplayControllerConfiguration, // 顯卡設定 94         Win32_VideoController, // 顯卡細節。 95         Win32_VideoSettings, // 顯卡支援的顯示模式。 96  97         // 作業系統 98         Win32_TimeZone, // 時區 99         Win32_SystemDriver, // 驅動程式100         Win32_DiskPartition, // 磁碟分割101         Win32_LogicalDisk, // 邏輯磁碟102         Win32_LogicalDiskToPartition, // 邏輯磁碟所在分區及始末位置。103         Win32_LogicalMemoryConfiguration, // 邏輯記憶體配置104         Win32_PageFile, // 系統頁檔案資訊105         Win32_PageFileSetting, // 頁檔案設定106         Win32_BootConfiguration, // 系統啟動配置107         Win32_ComputerSystem, // 電腦資訊簡要108         Win32_OperatingSystem, // 作業系統資訊109         Win32_StartupCommand, // 系統自動啟動程式110         Win32_Service, // 系統安裝的服務111         Win32_Group, // 系統管理組112         Win32_GroupUser, // 系統組帳號113         Win32_UserAccount, // 使用者帳號114         Win32_Process, // 系統進程115         Win32_Thread, // 系統線程116         Win32_Share, // 共用117         Win32_NetworkClient, // 已安裝的網路用戶端118         Win32_NetworkProtocol, // 已安裝的網路通訊協定119         Win32_PnPEntity,//all device120     }

 

 

 

6、結果:

正確獲得對應的com口號,達到了隨便插哪個口程式都可以跑的目的。

C# .net中擷取台式電腦中串口裝置的名稱

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.