-
-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query available devices from Win32_PnPEntity in FirmwareInstaller #259
Open
samgaudr
wants to merge
1
commit into
t3knomanzer:master
Choose a base branch
from
samgaudr:firmware-device-list-name
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 63 additions & 11 deletions
74
Desktop/FirmwareInstaller/FirmwareInstaller/Services/DiscoveryService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,83 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO.Ports; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Management; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace FirmwareInstaller.Services | ||
{ | ||
/// <summary> | ||
/// Represents a serial device. | ||
/// </summary> | ||
internal class COMDevice: IComparable<COMDevice> | ||
{ | ||
public string Port { get; set; } | ||
public string Name { get; set; } | ||
|
||
#region Constructor | ||
public COMDevice() { } | ||
#endregion | ||
|
||
#region Overrides | ||
|
||
public int CompareTo(COMDevice other) | ||
{ | ||
int currentNumber = Int32.Parse(Port.Substring(Port.Length - 1)); | ||
int otherNumber = Int32.Parse(other.Port.Substring(other.Port.Length - 1)); | ||
|
||
return currentNumber.CompareTo(otherNumber); | ||
} | ||
#endregion | ||
} | ||
|
||
/// <summary> | ||
/// Provides functionality to find available COM ports. | ||
/// </summary> | ||
internal class DiscoveryService : BaseService | ||
{ | ||
#region Fields | ||
private const string _usbDeviceQueryString = @"SELECT name FROM Win32_PnPEntity"; | ||
private readonly Regex _comPortRegex = new Regex(@"COM\d", RegexOptions.Compiled | RegexOptions.IgnoreCase); | ||
#endregion | ||
|
||
#region Constructor | ||
public DiscoveryService() {} | ||
public DiscoveryService() { } | ||
#endregion | ||
|
||
#region Public Methods | ||
/// <summary> | ||
/// Retrieves a list of availbale COM ports. | ||
/// Retrieves a list of available devices on COM ports. | ||
/// </summary> | ||
/// <returns>List of available COM ports.</returns> | ||
public IEnumerable<string> Discover() | ||
/// <returns>List of available devices on COM ports.</returns> | ||
public IEnumerable<COMDevice> Discover() | ||
{ | ||
ManagementObjectCollection usbDevices = queryUSBDevices(); | ||
List<COMDevice> serialDevices = filterCOMDevice(usbDevices); | ||
serialDevices.Sort(); | ||
return serialDevices; | ||
} | ||
#endregion | ||
|
||
#region Private Methods | ||
private ManagementObjectCollection queryUSBDevices() | ||
{ | ||
var moSearch = new ManagementObjectSearcher(_usbDeviceQueryString); | ||
return moSearch.Get(); | ||
} | ||
|
||
private List<COMDevice> filterCOMDevice(ManagementObjectCollection usbDevices) | ||
{ | ||
var result = SerialPort.GetPortNames(); | ||
return result.ToList(); | ||
var serialDevices = new List<COMDevice>(); | ||
|
||
foreach (ManagementObject device in usbDevices) | ||
{ | ||
object deviceName = device.Properties["Name"].Value; | ||
if (deviceName != null && _comPortRegex.IsMatch(deviceName.ToString())) | ||
{ | ||
serialDevices.Add(new COMDevice { Port = _comPortRegex.Match(deviceName.ToString()).Value, Name = deviceName.ToString()}); | ||
} | ||
} | ||
return serialDevices; | ||
} | ||
#endregion | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Com port range is 1-256 which is higher than a single digit so this will not work long term.
https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z000000PAhzSAG
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this ever resolved?