From f8574f0d66aff0b7e2e608af3a7587ea85eb23cf Mon Sep 17 00:00:00 2001 From: Edward Wright Date: Sun, 9 Jun 2019 20:41:25 -0400 Subject: [PATCH] cleaned up port enumeration and filtering (fixes #5, fixes #6) --- SimplySerial/SimplySerial.cs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/SimplySerial/SimplySerial.cs b/SimplySerial/SimplySerial.cs index 2d320bf..77edcb8 100644 --- a/SimplySerial/SimplySerial.cs +++ b/SimplySerial/SimplySerial.cs @@ -525,7 +525,7 @@ namespace SimplySerial using (var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=\"{4d36e978-e325-11ce-bfc1-08002be10318}\"")) { var ports = searcher.Get().Cast().ToList(); - return ports.Select(p => + List detectedPorts = ports.Select(p => { ComPort c = new ComPort(); c.name = p.GetPropertyValue("Name").ToString(); @@ -536,27 +536,45 @@ namespace SimplySerial Match mPID = Regex.Match(c.vid, pidPattern, RegexOptions.IgnoreCase); if (mVID.Success) + { c.vid = mVID.Groups[1].Value; - if (mPID.Success) - c.pid = mPID.Groups[1].Value; + c.vid = c.vid.Substring(0, Math.Min(4, c.vid.Length)); + } + 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); if (mName.Success) { c.name = mName.Value; c.num = int.Parse(c.name.Substring(3)); + if (c.num == 0) + c.name = "????"; } else { - c.name = "COM??"; + c.name = "????"; c.num = 0; } + c.board = MatchBoard(c.vid, c.pid); + return c; }).ToList(); + + // remove all unusable ports from the list + detectedPorts.RemoveAll(p => p.name == "????"); + + return (detectedPorts); } }