cleaned up port enumeration and filtering (fixes #5, fixes #6)

This commit is contained in:
Edward Wright 2019-06-09 20:41:25 -04:00
parent fa65b51a41
commit f8574f0d66

View File

@ -525,7 +525,7 @@ namespace SimplySerial
using (var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=\"{4d36e978-e325-11ce-bfc1-08002be10318}\"")) using (var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=\"{4d36e978-e325-11ce-bfc1-08002be10318}\""))
{ {
var ports = searcher.Get().Cast<ManagementBaseObject>().ToList(); var ports = searcher.Get().Cast<ManagementBaseObject>().ToList();
return ports.Select(p => List<ComPort> detectedPorts = ports.Select(p =>
{ {
ComPort c = new ComPort(); ComPort c = new ComPort();
c.name = p.GetPropertyValue("Name").ToString(); c.name = p.GetPropertyValue("Name").ToString();
@ -536,27 +536,45 @@ namespace SimplySerial
Match mPID = Regex.Match(c.vid, pidPattern, RegexOptions.IgnoreCase); Match mPID = Regex.Match(c.vid, pidPattern, RegexOptions.IgnoreCase);
if (mVID.Success) if (mVID.Success)
{
c.vid = mVID.Groups[1].Value; c.vid = mVID.Groups[1].Value;
if (mPID.Success) c.vid = c.vid.Substring(0, Math.Min(4, c.vid.Length));
c.pid = mPID.Groups[1].Value; }
else
c.vid = "????";
c.board = MatchBoard(c.vid, c.pid); if (mPID.Success)
{
c.pid = mPID.Groups[1].Value;
c.pid = c.pid.Substring(0, Math.Min(4, c.vid.Length));
}
else
c.pid = "????";
Match mName = Regex.Match(c.name, namePattern); Match mName = Regex.Match(c.name, namePattern);
if (mName.Success) if (mName.Success)
{ {
c.name = mName.Value; c.name = mName.Value;
c.num = int.Parse(c.name.Substring(3)); c.num = int.Parse(c.name.Substring(3));
if (c.num == 0)
c.name = "????";
} }
else else
{ {
c.name = "COM??"; c.name = "????";
c.num = 0; c.num = 0;
} }
c.board = MatchBoard(c.vid, c.pid);
return c; return c;
}).ToList(); }).ToList();
// remove all unusable ports from the list
detectedPorts.RemoveAll(p => p.name == "????");
return (detectedPorts);
} }
} }