• 返回当前系统串口名称


    主要针对当前的usb转串口进行了穷举。

    方便判断串口对应哪个设备。

    返回串口名称

    类对象,(包含了参考网址,以及对其进行了修改,防止出现蓝牙端口)

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using System.Management;
    7. namespace test
    8. {
    9. //参考地址 https://www.cnblogs.com/weiterli/p/8260790.html
    10. internal class GatSerialPortName
    11. {
    12. public enum HardwareEnum
    13. {
    14. // 硬件
    15. Win32_Processor, // CPU 处理器
    16. Win32_PhysicalMemory, // 物理内存条
    17. Win32_Keyboard, // 键盘
    18. Win32_PointingDevice, // 点输入设备,包括鼠标。
    19. Win32_FloppyDrive, // 软盘驱动器
    20. Win32_DiskDrive, // 硬盘驱动器
    21. Win32_CDROMDrive, // 光盘驱动器
    22. Win32_BaseBoard, // 主板
    23. Win32_BIOS, // BIOS 芯片
    24. Win32_ParallelPort, // 并口
    25. Win32_SerialPort, // 串口
    26. Win32_SerialPortConfiguration, // 串口配置
    27. Win32_SoundDevice, // 多媒体设置,一般指声卡。
    28. Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP)
    29. Win32_USBController, // USB 控制器
    30. Win32_NetworkAdapter, // 网络适配器
    31. Win32_NetworkAdapterConfiguration, // 网络适配器设置
    32. Win32_Printer, // 打印机
    33. Win32_PrinterConfiguration, // 打印机设置
    34. Win32_PrintJob, // 打印机任务
    35. Win32_TCPIPPrinterPort, // 打印机端口
    36. Win32_POTSModem, // MODEM
    37. Win32_POTSModemToSerialPort, // MODEM 端口
    38. Win32_DesktopMonitor, // 显示器
    39. Win32_DisplayConfiguration, // 显卡
    40. Win32_DisplayControllerConfiguration, // 显卡设置
    41. Win32_VideoController, // 显卡细节。
    42. Win32_VideoSettings, // 显卡支持的显示模式。
    43. // 操作系统
    44. Win32_TimeZone, // 时区
    45. Win32_SystemDriver, // 驱动程序
    46. Win32_DiskPartition, // 磁盘分区
    47. Win32_LogicalDisk, // 逻辑磁盘
    48. Win32_LogicalDiskToPartition, // 逻辑磁盘所在分区及始末位置。
    49. Win32_LogicalMemoryConfiguration, // 逻辑内存配置
    50. Win32_PageFile, // 系统页文件信息
    51. Win32_PageFileSetting, // 页文件设置
    52. Win32_BootConfiguration, // 系统启动配置
    53. Win32_ComputerSystem, // 计算机信息简要
    54. Win32_OperatingSystem, // 操作系统信息
    55. Win32_StartupCommand, // 系统自动启动程序
    56. Win32_Service, // 系统安装的服务
    57. Win32_Group, // 系统管理组
    58. Win32_GroupUser, // 系统组帐号
    59. Win32_UserAccount, // 用户帐号
    60. Win32_Process, // 系统进程
    61. Win32_Thread, // 系统线程
    62. Win32_Share, // 共享
    63. Win32_NetworkClient, // 已安装的网络客户端
    64. Win32_NetworkProtocol, // 已安装的网络协议
    65. Win32_PnPEntity,//all device
    66. }
    67. ///
    68. /// WMI取硬件信息
    69. ///
    70. ///
    71. ///
    72. ///
    73. public static string[] MulGetHardwareInfo(HardwareEnum hardType, string propKey)
    74. {
    75. List<string> strs = new List<string>();
    76. try
    77. {
    78. //如果报错:
    79. //managementobjectsearcher 缺少using
    80. //为什么已经引用了using System.Management 使用ManagementObjectSearcher时为什么提示未引用空间
    81. //解决办法:
    82. //在项目》》添加引用....里面引用System.Management 。 再using System.Management 就可以了
    83. //此处需要安装扩展包
    84. using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + hardType))
    85. {
    86. var hardInfos = searcher.Get();
    87. foreach (var hardInfo in hardInfos)
    88. {
    89. if (hardInfo.Properties[propKey].Value != null)
    90. {
    91. // COMM 这样的蓝牙接口无法滤除
    92. //if (hardInfo.Properties[propKey].Value.ToString().Contains("COM"))
    93. //{
    94. // strs.Add(hardInfo.Properties[propKey].Value.ToString());
    95. //}
    96. if (hardInfo.Properties[propKey].Value.ToString().Contains("COM1"))
    97. {
    98. strs.Add(hardInfo.Properties[propKey].Value.ToString());
    99. }
    100. else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM2"))
    101. {
    102. strs.Add(hardInfo.Properties[propKey].Value.ToString());
    103. }
    104. else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM3"))
    105. {
    106. strs.Add(hardInfo.Properties[propKey].Value.ToString());
    107. }
    108. else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM4"))
    109. {
    110. strs.Add(hardInfo.Properties[propKey].Value.ToString());
    111. }
    112. else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM5"))
    113. {
    114. strs.Add(hardInfo.Properties[propKey].Value.ToString());
    115. }
    116. else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM6"))
    117. {
    118. strs.Add(hardInfo.Properties[propKey].Value.ToString());
    119. }
    120. else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM7"))
    121. {
    122. strs.Add(hardInfo.Properties[propKey].Value.ToString());
    123. }
    124. else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM8"))
    125. {
    126. strs.Add(hardInfo.Properties[propKey].Value.ToString());
    127. }
    128. else if (hardInfo.Properties[propKey].Value.ToString().Contains("COM9"))
    129. {
    130. strs.Add(hardInfo.Properties[propKey].Value.ToString());
    131. }
    132. }
    133. }
    134. searcher.Dispose();
    135. }
    136. return strs.ToArray();
    137. }
    138. catch
    139. {
    140. return null;
    141. }
    142. finally
    143. { strs = null; }
    144. }
    145. //通过WMI获取COM端口
    146. ///
    147. /// 串口信息
    148. ///
    149. ///
    150. public static string[] GetSerialPort()
    151. {
    152. return MulGetHardwareInfo(HardwareEnum.Win32_PnPEntity, "Name");
    153. }
    154. }
    155. }

    调用方法

    1. string[] ArryPort = GatSerialPortName.GetSerialPort();
    2. foreach (string txt in ArryPort)
    3. add(txt);

    运行效果

    测试环境vs2022,win10

    特此记录

    anlog

    2022年12月3日

  • 相关阅读:
    ctfshow 月饼杯
    [C#]vs2022安装后C#创建winform没有.net framework4.8
    【Lua基础 第4章】Lua的流程控制、#的作用、table的创建方式、table表常用方法、函数、多返回值、可变长参数
    机械制造基础——加工方法
    提升后端API性能的几种解决方案
    Idea debug 调试运行慢
    godot引擎学习3
    基于暗原色先验的单幅图像去雾——算法复现
    Django-ORM-3:orm跨表查询,子查询(基于对象的跨表查询)和联表查询(基于双下划线的跨表查询)
    UnityShader 全局传值无效
  • 原文地址:https://blog.csdn.net/anlog/article/details/128168067