一、情境:windows
作項目的時候要打開串口而後進行一些庫函數的調用來操做目標板。串口使用的是usb轉串口,板子插進拔出的,每次都使用不同的usb口,debug的時候懶得每次改com口,又不想在UI上加上一個選擇api
com口的combox,因而就使用了下面這個方法。網絡
二、環境:ide
win7 6四、vs2010函數
三、目標:spa
獲取下圖的設備到底使用的是com幾。操作系統
四、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 device 120 }
六、結果:debug
正確得到對應的com口號,達到了隨便插哪一個口程序均可以跑的目的。3d