Files
SpectraElement/HardwareControl.Core/SerialPortNames.cs
T

28 lines
767 B
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace HardwareControl.Core
{
/// <summary>
/// 串口名规范化。原生库要求 Windows 设备路径 "\\.\COMx",
/// 同时兼容用户在系统参数里只填写 "COM5" 的情况。
/// </summary>
internal static class SerialPortNames
{
internal static string Normalize(string port)
{
var value = port.Trim();
if (value.StartsWith(@"\\.\", StringComparison.Ordinal))
{
return value;
}
if (value.StartsWith("COM", StringComparison.OrdinalIgnoreCase) &&
int.TryParse(value.AsSpan(3), out _))
{
return @"\\.\" + value.ToUpperInvariant();
}
return value;
}
}
}