C# .net中获取台式电脑中串口设备的名称

1、情境:

做项目的时候要打开串口然后进行一些库函数的调用来操作目标板。串口使用的是usb转串口,板子插进拔出的,每次都使用不一样的usb口,debug的时候懒得每次改com口,又不想在UI上加上一个选择

com口的combox,于是就使用了下面这个方法。

2、环境:

win7 64、vs2010

 

3、目标:

获取下图的设备到底使用的是com几。

C# .net中获取台式电脑中串口设备的名称

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 device

120     }

 

 

 

6、结果:

正确获得对应的com口号,达到了随便插哪个口程序都可以跑的目的。

C# .net中获取台式电脑中串口设备的名称

你可能感兴趣的:(.net)