diff --git a/Desktop/FirmwareInstaller/FirmwareInstaller/FirmwareInstaller.csproj b/Desktop/FirmwareInstaller/FirmwareInstaller/FirmwareInstaller.csproj
index af88ec77..17b89598 100644
--- a/Desktop/FirmwareInstaller/FirmwareInstaller/FirmwareInstaller.csproj
+++ b/Desktop/FirmwareInstaller/FirmwareInstaller/FirmwareInstaller.csproj
@@ -61,6 +61,7 @@
+
diff --git a/Desktop/FirmwareInstaller/FirmwareInstaller/MainWindow.xaml b/Desktop/FirmwareInstaller/FirmwareInstaller/MainWindow.xaml
index 249ae420..f8e0ee1a 100644
--- a/Desktop/FirmwareInstaller/FirmwareInstaller/MainWindow.xaml
+++ b/Desktop/FirmwareInstaller/FirmwareInstaller/MainWindow.xaml
@@ -7,7 +7,7 @@
xmlns:att="clr-namespace:FirmwareInstaller.Framework.Attached"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d"
- Height="400" Width="400"
+ Height="400" Width="500"
Background="{StaticResource WindowBackgroundBrush}"
Icon="/Resources/fwinstaller.ico">
@@ -18,13 +18,13 @@
-
+
-
+
@@ -34,43 +34,42 @@
-
+
-
+
-
-
+
-
-
+
+
-
+
-
+
-
+
-
-
+
+
diff --git a/Desktop/FirmwareInstaller/FirmwareInstaller/Services/DiscoveryService.cs b/Desktop/FirmwareInstaller/FirmwareInstaller/Services/DiscoveryService.cs
index ed14f818..00b06f8a 100644
--- a/Desktop/FirmwareInstaller/FirmwareInstaller/Services/DiscoveryService.cs
+++ b/Desktop/FirmwareInstaller/FirmwareInstaller/Services/DiscoveryService.cs
@@ -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
{
+ ///
+ /// Represents a serial device.
+ ///
+ internal class COMDevice: IComparable
+ {
+ 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
+ }
+
///
/// Provides functionality to find available COM ports.
///
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
///
- /// Retrieves a list of availbale COM ports.
+ /// Retrieves a list of available devices on COM ports.
///
- /// List of available COM ports.
- public IEnumerable Discover()
+ /// List of available devices on COM ports.
+ public IEnumerable Discover()
+ {
+ ManagementObjectCollection usbDevices = queryUSBDevices();
+ List serialDevices = filterCOMDevice(usbDevices);
+ serialDevices.Sort();
+ return serialDevices;
+ }
+ #endregion
+
+ #region Private Methods
+ private ManagementObjectCollection queryUSBDevices()
+ {
+ var moSearch = new ManagementObjectSearcher(_usbDeviceQueryString);
+ return moSearch.Get();
+ }
+
+ private List filterCOMDevice(ManagementObjectCollection usbDevices)
{
- var result = SerialPort.GetPortNames();
- return result.ToList();
+ var serialDevices = new List();
+
+ 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
}
-}
+}
\ No newline at end of file
diff --git a/Desktop/FirmwareInstaller/FirmwareInstaller/ViewModels/MainViewModel.cs b/Desktop/FirmwareInstaller/FirmwareInstaller/ViewModels/MainViewModel.cs
index 9c65c1ff..c66136f7 100644
--- a/Desktop/FirmwareInstaller/FirmwareInstaller/ViewModels/MainViewModel.cs
+++ b/Desktop/FirmwareInstaller/FirmwareInstaller/ViewModels/MainViewModel.cs
@@ -59,7 +59,7 @@ public MainViewModel()
private DelegateCommand _installCommand;
private DelegateCommand _loadFirmwareCommand;
- private string _selectedPort;
+ private COMDevice _selectedPort;
private string _selectedVersion;
private string _customFwFilePath;
private IList _logList;
@@ -88,7 +88,7 @@ private set
///
/// Selected COM port.
///
- public string SelectedPort
+ public COMDevice SelectedPort
{
get => _selectedPort;
set => SetProperty(ref _selectedPort, value);
@@ -146,7 +146,7 @@ public bool UseCustomFw
///
/// List of available COM ports.
///
- public ObservableCollection Ports { get; private set; }
+ public ObservableCollection Ports { get; private set; }
///
/// List of available versions to download.
@@ -208,7 +208,7 @@ private void InitPorts()
{
SendLog("Discovering COM devices...");
var ports = _discoveryService.Discover();
- Ports = new ObservableCollection(ports);
+ Ports = new ObservableCollection(ports);
SelectedPort = Ports.FirstOrDefault();
}
@@ -259,7 +259,7 @@ private bool CanInstall()
{
return false;
}
- else if(string.IsNullOrEmpty(SelectedPort))
+ else if(SelectedPort is null)
{
return false;
}
@@ -291,8 +291,8 @@ private async void Install()
}
}
- SendLog($"Installing file {fwFilePath} to {_selectedPort}");
- await _installService.InstallAsync(fwFilePath, _selectedPort, _useOldBootloader);
+ SendLog($"Installing file {fwFilePath} to {_selectedPort.Port}");
+ await _installService.InstallAsync(fwFilePath, _selectedPort.Port, _useOldBootloader);
IsBusy = false;
}