mirror of
https://github.com/fasteddy516/SimplySerial.git
synced 2024-11-24 10:04:35 +00:00
modifed GetSerialPorts() for more reliable retrieval of all available ports
This commit is contained in:
parent
b9841375c9
commit
a76d13f75c
@ -361,20 +361,23 @@ namespace SimplySerial
|
|||||||
/// Modified from the example written by Kamil Górski (freakone) available at
|
/// Modified from the example written by Kamil Górski (freakone) available at
|
||||||
/// http://blog.gorski.pm/serial-port-details-in-c-sharp
|
/// http://blog.gorski.pm/serial-port-details-in-c-sharp
|
||||||
/// https://github.com/freakone/serial-reader
|
/// https://github.com/freakone/serial-reader
|
||||||
|
/// Some modifications were based on this stackoverflow thread:
|
||||||
|
/// https://stackoverflow.com/questions/11458835/finding-information-about-all-serial-devices-connected-through-usb-in-c-sharp
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>List of available serial ports</returns>
|
/// <returns>List of available serial ports</returns>
|
||||||
private static List<ComPort> GetSerialPorts()
|
private static List<ComPort> GetSerialPorts()
|
||||||
{
|
{
|
||||||
const string vidPattern = @"VID_([0-9A-F]{4})";
|
const string vidPattern = @"VID_([0-9A-F]{4})";
|
||||||
const string pidPattern = @"PID_([0-9A-F]{4})";
|
const string pidPattern = @"PID_([0-9A-F]{4})";
|
||||||
|
const string namePattern = @"(?<=\()COM[0-9]{1,3}(?=\)$)";
|
||||||
using (var searcher = new ManagementObjectSearcher("SELECT * FROM WIN32_SerialPort"))
|
|
||||||
|
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 =>
|
return ports.Select(p =>
|
||||||
{
|
{
|
||||||
ComPort c = new ComPort();
|
ComPort c = new ComPort();
|
||||||
c.name = p.GetPropertyValue("DeviceID").ToString();
|
c.name = p.GetPropertyValue("Name").ToString();
|
||||||
c.vid = p.GetPropertyValue("PNPDeviceID").ToString();
|
c.vid = p.GetPropertyValue("PNPDeviceID").ToString();
|
||||||
c.description = p.GetPropertyValue("Caption").ToString();
|
c.description = p.GetPropertyValue("Caption").ToString();
|
||||||
|
|
||||||
@ -386,6 +389,12 @@ namespace SimplySerial
|
|||||||
if (mPID.Success)
|
if (mPID.Success)
|
||||||
c.pid = mPID.Groups[1].Value;
|
c.pid = mPID.Groups[1].Value;
|
||||||
|
|
||||||
|
Match mName = Regex.Match(c.name, namePattern);
|
||||||
|
if (mName.Success)
|
||||||
|
c.name = mName.Value;
|
||||||
|
else
|
||||||
|
c.name = "COM??";
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
|
|
||||||
}).ToList();
|
}).ToList();
|
||||||
|
Loading…
Reference in New Issue
Block a user