Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Management" />
<Reference Include="System.Windows" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
Expand Down
33 changes: 16 additions & 17 deletions Desktop/FirmwareInstaller/FirmwareInstaller/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

Expand All @@ -18,13 +18,13 @@
</MenuItem>

</Menu>

<Grid Margin="10" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<Grid Grid.Row="0" IsEnabled="{Binding IsBusy, Converter={StaticResource Cnv_BoolNegate}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
Expand All @@ -34,43 +34,42 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width=".5*" />
</Grid.ColumnDefinitions>

<!-- Versions -->
<!-- Versions -->
<Label Grid.Column="0" Grid.Row="0" Content="Version" Margin="0,0,0,0"/>
<ComboBox Grid.Column="1" Grid.Row="0" Margin="0,0,15,0" HorizontalAlignment="Right" MinWidth="150"
<ComboBox Grid.Column="1" Grid.Row="0" Margin="36,0,5,0" MinWidth="150"
ItemsSource="{Binding Versions}" SelectedValue="{Binding SelectedVersion}"
IsEnabled="{Binding ElementName=CtrlUseCustomFw, Path=IsChecked, Converter={StaticResource Cnv_BoolNegate}}"/>

<!-- COM -->
<Label Grid.Column="0" Grid.Row="2" Content="Port" Margin="0,5,0,0"/>
<ComboBox Grid.Column="1" Grid.Row="2" Margin="0,5,15,0" HorizontalAlignment="Right" MinWidth="150"
ItemsSource="{Binding Ports}" SelectedValue="{Binding SelectedPort}"/>
<ComboBox Grid.Column="1" Grid.Row="2" Margin="36,5,5,0" MinWidth="150"
ItemsSource="{Binding Ports}" DisplayMemberPath="Name" SelectedValue="{Binding SelectedPort}"/>

<!-- Custom FW -->
<Label Grid.Column="0" Grid.Row="3" Content="Use custom firmware" Margin="0,5,0,0"/>
<DockPanel Grid.Column="1" Grid.Row="3" Margin="0,5,15,0">
<CheckBox x:Name="CtrlUseCustomFw" DockPanel.Dock="Right" IsChecked="{Binding UseCustomFw}"/>
<DockPanel Grid.Column="1" Grid.Row="3" Margin="0,5,0,0">
<CheckBox x:Name="CtrlUseCustomFw" DockPanel.Dock="Right" IsChecked="{Binding UseCustomFw}" Margin="0,0,0,0"/>
<Label Content="{Binding CustomFwFilePath}" />
</DockPanel>

<!-- Bootloader -->
<Label Grid.Column="0" Grid.Row="4" Content="Use old bootloader" Margin="0,5,0,0"/>
<CheckBox Grid.Column="1" Grid.Row="4" Margin="0,5,15,0" HorizontalAlignment="Right"
IsChecked="{Binding UseOldBootloader}"/>
<CheckBox Grid.Column="1" Grid.Row="4" Margin="0,0,0,0" HorizontalAlignment="Right" IsChecked="{Binding UseOldBootloader}"/>

<!-- Install -->
<Button Grid.Column="1" Grid.Row="5" Content="Install" Margin="0,5,15,0" MinWidth="150" HorizontalAlignment="Right"
Command="{Binding InstallCommand}"/>
<Button Grid.Row="5" Content="Install" Margin="5,5,5,0" MinWidth="150"
Command="{Binding InstallCommand}" Grid.ColumnSpan="2"/>
</Grid>

<!-- Log -->
<ScrollViewer Grid.Row="1" x:Name="Ctrl_LogScroll" Margin="0,25,0,0" >
<TextBox x:Name="Ctrl_Log" IsReadOnly="True" Text="{Binding Log, Mode=OneWay}"/>
<ScrollViewer Grid.Row="1" x:Name="Ctrl_LogScroll" Margin="5,25,5,0" >
<TextBox x:Name="Ctrl_Log" IsReadOnly="True" Text="{Binding Log, Mode=OneWay}" Margin="0,0,0,0"/>
</ScrollViewer>
</Grid>
</DockPanel>
Expand Down
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));
Copy link
Collaborator

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

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this ever resolved?

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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public MainViewModel()
private DelegateCommand _installCommand;
private DelegateCommand<string> _loadFirmwareCommand;

private string _selectedPort;
private COMDevice _selectedPort;
private string _selectedVersion;
private string _customFwFilePath;
private IList<string> _logList;
Expand Down Expand Up @@ -88,7 +88,7 @@ private set
/// <summary>
/// Selected COM port.
/// </summary>
public string SelectedPort
public COMDevice SelectedPort
{
get => _selectedPort;
set => SetProperty(ref _selectedPort, value);
Expand Down Expand Up @@ -146,7 +146,7 @@ public bool UseCustomFw
/// <summary>
/// List of available COM ports.
/// </summary>
public ObservableCollection<string> Ports { get; private set; }
public ObservableCollection<COMDevice> Ports { get; private set; }

/// <summary>
/// List of available versions to download.
Expand Down Expand Up @@ -208,7 +208,7 @@ private void InitPorts()
{
SendLog("Discovering COM devices...");
var ports = _discoveryService.Discover();
Ports = new ObservableCollection<string>(ports);
Ports = new ObservableCollection<COMDevice>(ports);

SelectedPort = Ports.FirstOrDefault();
}
Expand Down Expand Up @@ -259,7 +259,7 @@ private bool CanInstall()
{
return false;
}
else if(string.IsNullOrEmpty(SelectedPort))
else if(SelectedPort is null)
{
return false;
}
Expand Down Expand Up @@ -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;
}
Expand Down