CAN设置添加

This commit is contained in:
hsc
2026-07-02 15:36:55 +08:00
parent 63f43853fa
commit 60dc155ccf
20 changed files with 1250 additions and 413 deletions

View File

@@ -20,6 +20,7 @@ namespace SettingModule
// 设备连接配置弹窗
containerRegistry.RegisterDialog<TCPConfigView>("TCPConfig");
containerRegistry.RegisterDialog<SerialPortConfigView>("SerialPortConfig");
containerRegistry.RegisterDialog<CANConfigView>("CANConfig");
}
}
}

View File

@@ -0,0 +1,134 @@
using System.Collections.ObjectModel;
using System.Windows.Input;
using UIShare.PubEvent;
using UIShare.UIViewModel;
using UIShare.ViewModelBase;
namespace SettingModule.ViewModels.Dialogs
{
/// <summary>
/// CAN 连接配置对话框 VM。
/// 通过 DialogParameters 接收宿主 DeviceInfoVM保存时把副本写回宿主。
/// 参数对应 ZLGCANFD 构造函数 + 初始化并启动通道 方法。
/// </summary>
public class CANConfigViewModel : DialogViewModelBase
{
#region
private string _title = "CAN 连接配置";
public string Title
{
get => _title;
set => SetProperty(ref _title, value);
}
/// <summary>编辑用的副本,取消时不会污染宿主对象。</summary>
private CANConfigVM _config = new();
public CANConfigVM Config
{
get => _config;
set => SetProperty(ref _config, value);
}
/// <summary>常用仲裁域波特率。</summary>
public ObservableCollection<string> CommonABitBauds { get; } = new()
{
"250000", "500000", "1000000"
};
/// <summary>常用数据域波特率。</summary>
public ObservableCollection<string> CommonDBitBauds { get; } = new()
{
"1000000", "2000000", "4000000", "5000000", "8000000"
};
private string _errorMessage = string.Empty;
public string ErrorMessage
{
get => _errorMessage;
set => SetProperty(ref _errorMessage, value);
}
#endregion
#region
public ICommand SaveCommand { get; }
public ICommand CancelCommand { get; }
#endregion
// 用于保存时把副本回写到原对象
private DeviceInfoVM? _hostDevice;
public CANConfigViewModel(IContainerProvider containerProvider) : base(containerProvider)
{
SaveCommand = new DelegateCommand(OnSave);
CancelCommand = new DelegateCommand(OnCancel);
}
private bool Validate(out string error)
{
error = string.Empty;
if (Config.DeviceType == 0)
{
error = "设备类型号不能为 0";
return false;
}
if (string.IsNullOrWhiteSpace(Config.ABitBaud))
{
error = "仲裁域波特率不能为空";
return false;
}
if (string.IsNullOrWhiteSpace(Config.DBitBaud))
{
error = "数据域波特率不能为空";
return false;
}
return true;
}
private void OnSave()
{
if (!Validate(out var error))
{
ErrorMessage = error;
return;
}
ErrorMessage = string.Empty;
// 把副本写回宿主
if (_hostDevice != null)
{
_hostDevice.CANConfig ??= new CANConfigVM();
Config.CopyTo(_hostDevice.CANConfig);
_hostDevice.ConnectionType = "CAN";
}
RequestClose.Invoke(ButtonResult.OK);
}
private void OnCancel() => RequestClose.Invoke(ButtonResult.Cancel);
#region Prism Dialog
public override void OnDialogOpened(IDialogParameters parameters)
{
_eventAggregator.GetEvent<OverlayEvent>().Publish(true);
if (parameters.ContainsKey("Device"))
{
_hostDevice = parameters.GetValue<DeviceInfoVM>("Device");
Title = $"CAN 连接配置 - {_hostDevice?.DeviceName}";
Config = new CANConfigVM(_hostDevice?.CANConfig);
}
else if (parameters.ContainsKey("Config"))
{
Config = new CANConfigVM(parameters.GetValue<CANConfigVM>("Config"));
}
}
public override void OnDialogClosed()
{
_eventAggregator.GetEvent<OverlayEvent>().Publish(false);
}
#endregion
}
}

View File

@@ -59,10 +59,9 @@ namespace SettingModule.ViewModels
public ObservableCollection<string> ConnectionTypes { get; } = new()
{
"None", "Tcp", "Serial"
"None", "Tcp", "Serial", "CAN"
};
#endregion
#region
public ICommand RefreshCommand { get; }
public ICommand SaveCommand { get; }
@@ -165,6 +164,7 @@ namespace SettingModule.ViewModels
{
"Tcp" => "TCPConfig",
"Serial" => "SerialPortConfig",
"CAN" => "CANConfig",
_ => string.Empty
};
@@ -200,6 +200,7 @@ namespace SettingModule.ViewModels
_scope = _globalInfo.ScopeDic[TestStatus];
_scopedContext = _globalInfo.ContextDic[TestStatus];
SystemConfig = _scope.Resolve<SystemConfig>();
ConfigService.EnsureDefaultCanDevice(SystemConfig);
if (DeviceList != null && DeviceList.Count > 0)
{
SelectedDevice = DeviceList[0];

View File

@@ -0,0 +1,131 @@
<UserControl x:Class="SettingModule.Views.Dialogs.CANConfigView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SettingModule.Views.Dialogs"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:helpers="clr-namespace:UIShare.Helpers;assembly=UIShare"
xmlns:converters="clr-namespace:UIShare.Converters;assembly=UIShare"
mc:Ignorable="d"
xmlns:prism="http://prismlibrary.com/"
Background="White"
prism:ViewModelLocator.AutoWireViewModel="True"
Width="440"
Height="360">
<prism:Dialog.WindowStyle>
<Style BasedOn="{StaticResource DialogUserManageStyle}"
TargetType="Window" />
</prism:Dialog.WindowStyle>
<UserControl.Resources>
<converters:StringToVisibilityConverter x:Key="StringToVisibility"/>
</UserControl.Resources>
<GroupBox Padding="12,8,12,8"
helpers:WindowDragHelper.EnableWindowDrag="True">
<GroupBox.Header>
<Grid Margin="0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="*" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}"
Foreground="White"
VerticalAlignment="Center"
Margin="5,0,10,0" />
</Grid>
</GroupBox.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- 表单 -->
<Grid Grid.Row="0" Margin="0,4,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- 设备类型号 -->
<TextBlock Grid.Row="0" Grid.Column="0"
Text="设备类型号:"
VerticalAlignment="Center" Margin="0,6"/>
<TextBox Grid.Row="0" Grid.Column="1" Margin="0,6"
materialDesign:HintAssist.Hint="43 = USBCANFD-400U"
Text="{Binding Config.DeviceType, UpdateSourceTrigger=PropertyChanged}"/>
<!-- 设备索引 -->
<TextBlock Grid.Row="1" Grid.Column="0"
Text="设备索引:"
VerticalAlignment="Center" Margin="0,6"/>
<TextBox Grid.Row="1" Grid.Column="1" Margin="0,6"
materialDesign:HintAssist.Hint=""
Text="{Binding Config.DeviceIndex, UpdateSourceTrigger=PropertyChanged}"/>
<!-- 仲裁域波特率 -->
<TextBlock Grid.Row="2" Grid.Column="0"
Text="仲裁域波特率:"
VerticalAlignment="Center" Margin="0,6"/>
<ComboBox Grid.Row="2" Grid.Column="1" Margin="0,6"
IsEditable="True"
materialDesign:HintAssist.Hint=""
ItemsSource="{Binding CommonABitBauds}"
Text="{Binding Config.ABitBaud, UpdateSourceTrigger=PropertyChanged}"/>
<!-- 数据域波特率 -->
<TextBlock Grid.Row="3" Grid.Column="0"
Text="数据域波特率:"
VerticalAlignment="Center" Margin="0,6"/>
<ComboBox Grid.Row="3" Grid.Column="1" Margin="0,6"
IsEditable="True"
materialDesign:HintAssist.Hint=""
ItemsSource="{Binding CommonDBitBauds}"
Text="{Binding Config.DBitBaud, UpdateSourceTrigger=PropertyChanged}"/>
<!-- 终端电阻 -->
<TextBlock Grid.Row="4" Grid.Column="0"
Text="终端电阻:"
VerticalAlignment="Center" Margin="0,6"/>
<ToggleButton Grid.Row="4" Grid.Column="1" Margin="0,6"
IsChecked="{Binding Config.EnableTerminalResistance}"
Style="{StaticResource MaterialDesignSwitchToggleButton}"/>
</Grid>
<!-- 错误提示 -->
<TextBlock Grid.Row="1"
Margin="0,8,0,0"
Foreground="#D32F2F"
TextWrapping="Wrap"
Text="{Binding ErrorMessage}"
Visibility="{Binding ErrorMessage, Converter={StaticResource StringToVisibility}}"/>
<!-- 按钮 -->
<StackPanel Grid.Row="2"
Orientation="Horizontal"
HorizontalAlignment="Right"
Margin="0,12,0,0">
<Button Content="取消"
Width="80" Padding="0,4"
Command="{Binding CancelCommand}"/>
<Button Content="保存"
Width="80" Padding="0,4"
Margin="10,0,0,0"
IsDefault="True"
Command="{Binding SaveCommand}"/>
</StackPanel>
</Grid>
</GroupBox>
</UserControl>

View File

@@ -0,0 +1,15 @@
using System.Windows.Controls;
namespace SettingModule.Views.Dialogs
{
/// <summary>
/// CANConfigView.xaml 的交互逻辑
/// </summary>
public partial class CANConfigView : UserControl
{
public CANConfigView()
{
InitializeComponent();
}
}
}

View File

@@ -368,6 +368,33 @@
<Run Text="{Binding SelectedDevice.SerialPortConfig.Parity}"/>
</TextBlock>
</StackPanel>
<!-- CAN 参数预览 -->
<StackPanel Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"
Margin="0,4,0,0" Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="StackPanel">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedDevice.ConnectionType}" Value="CAN">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<TextBlock Foreground="#666" FontSize="12">
<Run Text="设备类型:"/>
<Run Text="{Binding SelectedDevice.CANConfig.DeviceType}"/>
<Run Text=" 设备索引:"/>
<Run Text="{Binding SelectedDevice.CANConfig.DeviceIndex}"/>
<Run Text=" 仲裁/数据波特率:"/>
<Run Text="{Binding SelectedDevice.CANConfig.ABitBaud}"/>
<Run Text="/"/>
<Run Text="{Binding SelectedDevice.CANConfig.DBitBaud}"/>
<Run Text=" 终端电阻:"/>
<Run Text="{Binding SelectedDevice.CANConfig.EnableTerminalResistance}"/>
</TextBlock>
</StackPanel>
</Grid>
</StackPanel>
</Border>