implemented automatic reload of COM ports in SerialSettingsView and automatic reload of usb devices in UsbHidSettingsView.

therefore implemented ICloneable and IEquatable in UsbHidDeviceInfo
master
Jonas Arnold 3 years ago
parent f3a2c98f18
commit 9ae494e2e7
  1. 28
      MultiTerm.Protocols/UsbHid/UsbHidDeviceInfo.cs
  2. 23
      MultiTerm.Protocols/UsbHid/UsbHidProtocolSettingsViewModel.cs
  3. 32
      MultiTerm.Wpf/View/SettingsView/SerialSettingsView.xaml
  4. 10
      MultiTerm.Wpf/View/SettingsView/UsbHidSettingsView.xaml

@ -1,6 +1,6 @@
namespace MultiTerm.Protocols.UsbHid;
public class UsbHidDeviceInfo
public class UsbHidDeviceInfo : ICloneable, IEquatable<UsbHidDeviceInfo>
{
public string VendorId { get; internal set; } = string.Empty;
@ -9,4 +9,30 @@ public class UsbHidDeviceInfo
public string Manufacturer { get; internal set; } = string.Empty;
public string SerialNumber { get; internal set; } = string.Empty;
#region ICloneable Implementation
public object Clone()
{
return base.MemberwiseClone();
}
#endregion
#region IEquatable<UsbHidDeviceInfo> Implementation
public bool Equals(UsbHidDeviceInfo? other)
{
// null cases
if (other == null && this == null)
{ return true; }
if (other == null && this != null)
{ return false; }
// otherwise if vendor id, product id and serial number match => they are equal
if (this.VendorId == other!.VendorId &&
this.ProductId == other!.ProductId &&
this.SerialNumber == other!.SerialNumber)
{ return true; }
else
{ return false; }
}
#endregion
}

@ -33,9 +33,30 @@ public partial class UsbHidProtocolSettingsViewModel : ProtocolSettingsViewModel
[RelayCommand]
private void ReloadDevices()
{
UsbHidDeviceInfo? previouslySelectedDevice = null;
// if any device is selected => temporarily store it
if (this.SelectedDevice != null)
{
// shallow copy of the device
previouslySelectedDevice = (UsbHidDeviceInfo)this.SelectedDevice.Clone();
}
// reload collection
this.Devices = new ObservableCollection<UsbHidDeviceInfo>(UsbHidProtocol.GetDevices());
// select first deivce, if none is selected yet and there are some available
// select previously selected device again if it still exists
if (previouslySelectedDevice != null)
{
var equalDevices = this.Devices.Where(d => d.Equals(previouslySelectedDevice));
// exactly one match found => take as selected device and quit method
if(equalDevices.Count() == 1)
{
this.SelectedDevice = equalDevices.First();
}
}
// otherwise select first device, if none is selected yet and there are some available
if (this.SelectedDevice == null && this.Devices.Any())
{
this.SelectedDevice = this.Devices.First();

@ -7,6 +7,7 @@
xmlns:local="clr-namespace:MultiTerm.Wpf.View.SettingsView"
xmlns:conv="clr-namespace:MultiTerm.Wpf.ValueConverters"
xmlns:serial_protocol="clr-namespace:MultiTerm.Protocols.Serial;assembly=MultiTerm.Protocols"
xmlns:XamlBehavioursWpf="http://schemas.microsoft.com/xaml/behaviors"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="800">
<DockPanel>
@ -43,16 +44,39 @@
</StackPanel.Resources>
<Label Margin="10 0" VerticalAlignment="Center">Port: </Label>
<ComboBox Width="100" ItemsSource="{Binding Path=ComPorts}" SelectedItem="{Binding Path=PortName, Mode=TwoWay}"
VerticalContentAlignment="Center"/>
<ComboBox Width="100" x:Name="comboBoxPorts"
ItemsSource="{Binding Path=ComPorts}" SelectedItem="{Binding Path=PortName, Mode=TwoWay}"
VerticalContentAlignment="Center">
<!-- Auto reload com ports when opening combobox -->
<XamlBehavioursWpf:Interaction.Triggers>
<XamlBehavioursWpf:EventTrigger EventName="DropDownOpened" SourceObject="{Binding ElementName=comboBoxPorts}">
<XamlBehavioursWpf:InvokeCommandAction Command="{Binding ReloadComPortsCommand}" />
</XamlBehavioursWpf:EventTrigger>
</XamlBehavioursWpf:Interaction.Triggers>
</ComboBox>
<Button Margin="5 0" Content="R" VerticalContentAlignment="Center"
Command="{Binding ReloadComPortsCommand}" Width="25"/>
<Label Margin="20 0 10 0" VerticalAlignment="Center">Baud Rate:</Label>
<TextBox Width="100" VerticalContentAlignment="Center" Text="{Binding Path=BaudRate, Converter={StaticResource IntToStringConverter}, Mode=TwoWay}" />
<ComboBox IsEditable="True" Width="100" VerticalContentAlignment="Center" Text="{Binding Path=BaudRate, Converter={StaticResource IntToStringConverter}, Mode=TwoWay}" >
<ComboBoxItem Content="2400"/>
<ComboBoxItem Content="9600"/>
<ComboBoxItem Content="19200"/>
<ComboBoxItem Content="56000"/>
<ComboBoxItem Content="57600"/>
<ComboBoxItem Content="115200"/>
<ComboBoxItem Content="128000"/>
<ComboBoxItem Content="256000"/>
</ComboBox>
<Label Margin="10 0 10 0" VerticalAlignment="Center">Data Bits:</Label>
<TextBox Width="40" VerticalContentAlignment="Center" Text="{Binding Path=DataBits, Converter={StaticResource IntToStringConverter}, Mode=TwoWay}" />
<ComboBox IsEditable="False" Width="Auto" VerticalContentAlignment="Center" Text="{Binding Path=DataBits, Converter={StaticResource IntToStringConverter}, Mode=TwoWay}" >
<ComboBoxItem Content="5"/>
<ComboBoxItem Content="6"/>
<ComboBoxItem Content="7"/>
<ComboBoxItem Content="8"/>
<ComboBoxItem Content="16"/>
</ComboBox>
<Label Margin="10 0 10 0" VerticalAlignment="Center">Parity:</Label>
<ComboBox Width="Auto" VerticalContentAlignment="Center"

@ -4,6 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MultiTerm.Wpf.View.SettingsView"
xmlns:XamlBehavioursWpf="http://schemas.microsoft.com/xaml/behaviors"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="800">
<DockPanel>
@ -80,9 +81,16 @@
</StackPanel.Resources>
<Label Margin="10 0" VerticalAlignment="Center">Devices:</Label>
<ComboBox Width="190" ItemsSource="{Binding Path=Devices}" SelectedItem="{Binding Path=SelectedDevice, Mode=TwoWay}"
<ComboBox Width="190" x:Name="comboBoxHidDevices"
ItemsSource="{Binding Path=Devices}" SelectedItem="{Binding Path=SelectedDevice, Mode=TwoWay}"
VerticalContentAlignment="Center"
ItemTemplate="{StaticResource HidDeviceComboBoxTemplate}">
<!-- Auto reload hid devices when opening combobox -->
<XamlBehavioursWpf:Interaction.Triggers>
<XamlBehavioursWpf:EventTrigger EventName="DropDownOpened" SourceObject="{Binding ElementName=comboBoxHidDevices}">
<XamlBehavioursWpf:InvokeCommandAction Command="{Binding ReloadDevicesCommand}" />
</XamlBehavioursWpf:EventTrigger>
</XamlBehavioursWpf:Interaction.Triggers>
</ComboBox>
<Button Margin="5 0" Content="R" VerticalContentAlignment="Center"
Command="{Binding ReloadDevicesCommand}" Width="25"/>

Loading…
Cancel
Save