SerialHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using MV485.model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO.Ports;
  5. using System.Linq;
  6. using System.Management;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. namespace MV485.helper
  11. {
  12. //串口工具类
  13. public class SerialHelper
  14. {
  15. public static string[] BaudRates = new string[] {
  16. "115200", "57600","56000", "38400", "19200","14400", "9600", "4800", "2400", "1200", "600", "300", "110"
  17. };
  18. public static List<SPortModel> GetSerialPortsWithDetails()
  19. {
  20. List<SPortModel> ports = new List<SPortModel>();
  21. using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(COM%'"))
  22. {
  23. foreach (ManagementObject obj in searcher.Get())
  24. {
  25. string name = obj["Name"]?.ToString() ?? "未知设备"; // 获取完整设备名称
  26. string port = ExtractCOMPort(name); // 提取 COM 端口号
  27. if (!string.IsNullOrEmpty(port))
  28. {
  29. string cleanName = CleanDeviceName(name); // 清理设备名称
  30. ports.Add(new SPortModel(port, cleanName));
  31. }
  32. }
  33. }
  34. return ports;
  35. }
  36. private static string ExtractCOMPort(string name)
  37. {
  38. // 处理标准格式: "USB Serial Port (COM6)"
  39. Match match = Regex.Match(name, @"\(COM\d+");
  40. if (match.Success)
  41. {
  42. return match.Value.Trim('(', ')'); // 提取 COM 端口号
  43. }
  44. // 处理虚拟串口: "Electronic Team Virtual Serial Port (COM1->COM2)"
  45. match = Regex.Match(name, @"\(COM\d+->COM\d+\)");
  46. if (match.Success)
  47. {
  48. Match firstPortMatch = Regex.Match(match.Value, @"COM\d+");
  49. if (firstPortMatch.Success)
  50. {
  51. return firstPortMatch.Value; // 只返回第一个端口 COM1
  52. }
  53. }
  54. return string.Empty;
  55. }
  56. private static string CleanDeviceName(string name)
  57. {
  58. // 移除所有带 COMx 的部分,包括 (COMx) 和 (COMx->COMy)
  59. return Regex.Replace(name, @"\s*\(COM\d+.*?\)", "").Trim();
  60. }
  61. //public static List<SPortModel> GetSerialPortsWithDetails()
  62. //{
  63. // List<SPortModel> ports = new List<SPortModel>();
  64. // using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(COM%'"))
  65. // {
  66. // foreach (ManagementObject obj in searcher.Get())
  67. // {
  68. // string name = obj["Name"]?.ToString() ?? "未知设备"; // 设备名称
  69. // string cleanName = Regex.Replace(name, @"\(\w+\)", "").Trim();
  70. // string port = ExtractCOMPort(name); // 提取 COM 端口号
  71. // if (!string.IsNullOrEmpty(port))
  72. // {
  73. // ports.Add(new SPortModel(port, cleanName));
  74. // }
  75. // }
  76. // }
  77. // return ports;
  78. //}
  79. //static string ExtractCOMPort(string deviceName)
  80. //{
  81. // int start = deviceName.IndexOf("(COM");
  82. // if (start >= 0)
  83. // {
  84. // int end = deviceName.IndexOf(")", start);
  85. // if (end > start)
  86. // {
  87. // return deviceName.Substring(start + 1, end - start - 1); // 获取 "COMX"
  88. // }
  89. // }
  90. // return null;
  91. //}
  92. //----直接获取串口名称
  93. public static string[] GetSerialPortNames()
  94. {
  95. string[] ports = SerialPort.GetPortNames();
  96. if (ports.Length == 0)
  97. {
  98. Console.WriteLine("未找到可用的串口设备。");
  99. }
  100. else
  101. {
  102. Console.WriteLine("可用的串口设备:");
  103. foreach (string port in ports)
  104. {
  105. Console.WriteLine(port);
  106. }
  107. }
  108. return ports;
  109. }
  110. }
  111. }