|
@@ -18,7 +18,6 @@ namespace MV485.helper
|
|
|
"115200", "57600","56000", "38400", "19200","14400", "9600", "4800", "2400", "1200", "600", "300", "110"
|
|
|
};
|
|
|
|
|
|
-
|
|
|
public static List<SPortModel> GetSerialPortsWithDetails()
|
|
|
{
|
|
|
List<SPortModel> ports = new List<SPortModel>();
|
|
@@ -27,12 +26,12 @@ namespace MV485.helper
|
|
|
{
|
|
|
foreach (ManagementObject obj in searcher.Get())
|
|
|
{
|
|
|
- string name = obj["Name"]?.ToString() ?? "未知设备"; // 设备名称
|
|
|
- string cleanName = Regex.Replace(name, @"\(\w+\)", "").Trim();
|
|
|
- string port = ExtractCOMPort(name); // 提取 COM 端口号
|
|
|
+ string name = obj["Name"]?.ToString() ?? "未知设备"; // 获取完整设备名称
|
|
|
|
|
|
+ string port = ExtractCOMPort(name); // 提取 COM 端口号
|
|
|
if (!string.IsNullOrEmpty(port))
|
|
|
{
|
|
|
+ string cleanName = CleanDeviceName(name); // 清理设备名称
|
|
|
ports.Add(new SPortModel(port, cleanName));
|
|
|
}
|
|
|
}
|
|
@@ -40,20 +39,70 @@ namespace MV485.helper
|
|
|
return ports;
|
|
|
}
|
|
|
|
|
|
- static string ExtractCOMPort(string deviceName)
|
|
|
+ private static string ExtractCOMPort(string name)
|
|
|
{
|
|
|
- int start = deviceName.IndexOf("(COM");
|
|
|
- if (start >= 0)
|
|
|
+ // 处理标准格式: "USB Serial Port (COM6)"
|
|
|
+ Match match = Regex.Match(name, @"\(COM\d+");
|
|
|
+ if (match.Success)
|
|
|
{
|
|
|
- int end = deviceName.IndexOf(")", start);
|
|
|
- if (end > start)
|
|
|
+ return match.Value.Trim('(', ')'); // 提取 COM 端口号
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理虚拟串口: "Electronic Team Virtual Serial Port (COM1->COM2)"
|
|
|
+ match = Regex.Match(name, @"\(COM\d+->COM\d+\)");
|
|
|
+ if (match.Success)
|
|
|
+ {
|
|
|
+ Match firstPortMatch = Regex.Match(match.Value, @"COM\d+");
|
|
|
+ if (firstPortMatch.Success)
|
|
|
{
|
|
|
- return deviceName.Substring(start + 1, end - start - 1); // 获取 "COMX"
|
|
|
+ return firstPortMatch.Value; // 只返回第一个端口 COM1
|
|
|
}
|
|
|
}
|
|
|
- return null;
|
|
|
+
|
|
|
+ return string.Empty;
|
|
|
}
|
|
|
|
|
|
+ private static string CleanDeviceName(string name)
|
|
|
+ {
|
|
|
+ // 移除所有带 COMx 的部分,包括 (COMx) 和 (COMx->COMy)
|
|
|
+ return Regex.Replace(name, @"\s*\(COM\d+.*?\)", "").Trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ //public static List<SPortModel> GetSerialPortsWithDetails()
|
|
|
+ //{
|
|
|
+ // List<SPortModel> ports = new List<SPortModel>();
|
|
|
+
|
|
|
+ // using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(COM%'"))
|
|
|
+ // {
|
|
|
+ // foreach (ManagementObject obj in searcher.Get())
|
|
|
+ // {
|
|
|
+ // string name = obj["Name"]?.ToString() ?? "未知设备"; // 设备名称
|
|
|
+ // string cleanName = Regex.Replace(name, @"\(\w+\)", "").Trim();
|
|
|
+ // string port = ExtractCOMPort(name); // 提取 COM 端口号
|
|
|
+
|
|
|
+ // if (!string.IsNullOrEmpty(port))
|
|
|
+ // {
|
|
|
+ // ports.Add(new SPortModel(port, cleanName));
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // return ports;
|
|
|
+ //}
|
|
|
+
|
|
|
+ //static string ExtractCOMPort(string deviceName)
|
|
|
+ //{
|
|
|
+ // int start = deviceName.IndexOf("(COM");
|
|
|
+ // if (start >= 0)
|
|
|
+ // {
|
|
|
+ // int end = deviceName.IndexOf(")", start);
|
|
|
+ // if (end > start)
|
|
|
+ // {
|
|
|
+ // return deviceName.Substring(start + 1, end - start - 1); // 获取 "COMX"
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // return null;
|
|
|
+ //}
|
|
|
+
|
|
|
//----直接获取串口名称
|
|
|
public static string[] GetSerialPortNames()
|
|
|
{
|