自动加载dbc

This commit is contained in:
hsc
2026-07-09 16:19:20 +08:00
parent b21c1901e1
commit 412b56935c
5 changed files with 83 additions and 12 deletions

View File

@@ -164,9 +164,9 @@ namespace CANModule.ViewModels
set => SetProperty(ref _IsSaved, value); set => SetProperty(ref _IsSaved, value);
} }
public ObservableCollection<AutoDBCLoadItem> dbcAutoLoadList public ObservableCollection<AutoDBCLoadItem> DBCAutoLoadList
{ {
get => _systemConfig?.dbcAutoLoadList ?? new ObservableCollection<AutoDBCLoadItem>(); get => _systemConfig?.DBCAutoLoadList ?? new ObservableCollection<AutoDBCLoadItem>();
} }
private AutoDBCLoadItem _SelectedDcAutoLoad; private AutoDBCLoadItem _SelectedDcAutoLoad;
@@ -360,7 +360,7 @@ namespace CANModule.ViewModels
RaisePropertyChanged(nameof(BLFPath)); RaisePropertyChanged(nameof(BLFPath));
RaisePropertyChanged(nameof(DBCPath)); RaisePropertyChanged(nameof(DBCPath));
RaisePropertyChanged(nameof(dbcAutoLoadList)); RaisePropertyChanged(nameof(DBCAutoLoadList));
} }
public override void OnNavigatedFrom(NavigationContext navigationContext) public override void OnNavigatedFrom(NavigationContext navigationContext)
@@ -413,7 +413,7 @@ namespace CANModule.ViewModels
{ {
if (SelectedDcAutoLoad != null) if (SelectedDcAutoLoad != null)
{ {
dbcAutoLoadList.Remove(SelectedDcAutoLoad); DBCAutoLoadList.Remove(SelectedDcAutoLoad);
} }
} }
@@ -491,10 +491,10 @@ namespace CANModule.ViewModels
if (re && IsSaved) if (re && IsSaved)
{ {
var item = dbcAutoLoadList.FirstOrDefault(s => s.DBCChannel == DBCChannel); var item = DBCAutoLoadList.FirstOrDefault(s => s.DBCChannel == DBCChannel);
if (item == null) if (item == null)
{ {
dbcAutoLoadList.Add(new AutoDBCLoadItem DBCAutoLoadList.Add(new AutoDBCLoadItem
{ {
DBCFilePath = DBCPath, DBCFilePath = DBCPath,
DBCChannel = DBCChannel DBCChannel = DBCChannel
@@ -502,8 +502,8 @@ namespace CANModule.ViewModels
} }
else else
{ {
dbcAutoLoadList.Remove(item); DBCAutoLoadList.Remove(item);
dbcAutoLoadList.Add(new AutoDBCLoadItem DBCAutoLoadList.Add(new AutoDBCLoadItem
{ {
DBCFilePath = DBCPath, DBCFilePath = DBCPath,
DBCChannel = DBCChannel DBCChannel = DBCChannel

View File

@@ -124,7 +124,7 @@
<Label Content="加载记录保存" <Label Content="加载记录保存"
VerticalContentAlignment="Center" /> VerticalContentAlignment="Center" />
</StackPanel> </StackPanel>
<DataGrid ItemsSource="{Binding dbcAutoLoadList}" <DataGrid ItemsSource="{Binding DBCAutoLoadList}"
Width="650" Width="650"
IsReadOnly="True" IsReadOnly="True"
AutoGenerateColumns="False" AutoGenerateColumns="False"

View File

@@ -1,12 +1,15 @@
using DeviceCommand.Base; using DeviceCommand.Base;
using Logger; using Logger;
using Model.Models; using Model.Models;
using Prism.Events;
using Prism.Ioc; using Prism.Ioc;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.IO.Ports; using System.IO.Ports;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using UIShare.PubEvent;
using UIShare.UIViewModel; using UIShare.UIViewModel;
using ZLGUSBCANFD; using ZLGUSBCANFD;
@@ -22,6 +25,7 @@ namespace UIShare.GlobalVariable
public SystemConfig _systemConfig { get; set; } public SystemConfig _systemConfig { get; set; }
private readonly GlobalInfo _globalInfo; private readonly GlobalInfo _globalInfo;
private readonly string _scopeName; private readonly string _scopeName;
private readonly IEventAggregator _eventAggregator;
/// <summary>按 DeviceName 索引的设备字典,便于业务层按名取实例。</summary> /// <summary>按 DeviceName 索引的设备字典,便于业务层按名取实例。</summary>
public IDictionary<string, IBaseInterface> DeviceMap { get; private set; } public IDictionary<string, IBaseInterface> DeviceMap { get; private set; }
@@ -31,10 +35,11 @@ namespace UIShare.GlobalVariable
private static readonly IReadOnlyDictionary<string, Type> _deviceTypeMap = BuildDeviceTypeMap(); private static readonly IReadOnlyDictionary<string, Type> _deviceTypeMap = BuildDeviceTypeMap();
public ZLGCANFD CANFD { get; set; } public ZLGCANFD CANFD { get; set; }
public DeviceManager(SystemConfig systemConfig, GlobalInfo globalInfo) public DeviceManager(SystemConfig systemConfig, GlobalInfo globalInfo, IEventAggregator eventAggregator)
{ {
_systemConfig = systemConfig; _systemConfig = systemConfig;
_globalInfo = globalInfo; _globalInfo = globalInfo;
_eventAggregator = eventAggregator;
// 用 SystemConfig.Title 作为作用域唯一标识,无需反查 ConfigDic // 用 SystemConfig.Title 作为作用域唯一标识,无需反查 ConfigDic
_scopeName = _systemConfig.Title; _scopeName = _systemConfig.Title;
InitDevices(); InitDevices();
@@ -407,7 +412,7 @@ namespace UIShare.GlobalVariable
} }
/// <summary> /// <summary>
/// 打开 CAN 卡:打开设备 + 初始化并启动所有通道 /// 打开 CAN 卡:打开设备 + 初始化并启动所有通道 + 自动加载 DBC
/// </summary> /// </summary>
private async Task<bool> ConnectCanAsync(DeviceInfoVM info) private async Task<bool> ConnectCanAsync(DeviceInfoVM info)
{ {
@@ -431,6 +436,55 @@ namespace UIShare.GlobalVariable
bool ok = await Task.Run(() => bool ok = await Task.Run(() =>
{ {
if (!CANFD.()) return false; if (!CANFD.()) return false;
// 初始化并启动所有通道
int maxCh = CANFD.DBCParser.MaxChannels;
for (uint i = 0; i < maxCh; i++)
{
try
{
CANFD.(i);
}
catch (Exception ex)
{
LoggerHelper.Warn($"CAN 通道 {i} 初始化失败:{ex.Message}");
break;
}
}
// 自动加载 DBC 文件
if (_systemConfig?.DBCAutoLoadList != null)
{
foreach (var item in _systemConfig.DBCAutoLoadList)
{
if (item.DBCChannel < 0 || item.DBCChannel >= maxCh) continue;
if (string.IsNullOrWhiteSpace(item.DBCFilePath)) continue;
if (!File.Exists(item.DBCFilePath))
{
// 文件不存在,发布事件广播
_eventAggregator.GetEvent<DBCFileNotFoundEvent>().Publish(new DBCFileNotFoundArgs
{
Channel = item.DBCChannel,
FilePath = item.DBCFilePath,
Scope = _scopeName
});
LoggerHelper.Warn($"CAN 通道 {item.DBCChannel} 自动加载 DBC 失败:文件不存在 [{item.DBCFilePath}]");
continue;
}
bool loadOk = CANFD.DBC文件((uint)item.DBCChannel, item.DBCFilePath);
if (loadOk)
{
LoggerHelper.Info($"CAN 通道 {item.DBCChannel} 已自动加载 DBC{item.DBCFilePath}");
}
else
{
LoggerHelper.Warn($"CAN 通道 {item.DBCChannel} 自动加载 DBC 失败:{item.DBCFilePath}");
}
}
}
return true; return true;
}); });

View File

@@ -31,7 +31,7 @@ namespace UIShare.GlobalVariable
public ObservableCollection<DeviceInfoVM> DeviceList = new(); public ObservableCollection<DeviceInfoVM> DeviceList = new();
public ObservableCollection<SharedParameter> SharedParameterList = new(); public ObservableCollection<SharedParameter> SharedParameterList = new();
public ObservableCollection<CANSignalConfig> ConfigurationList = new(); public ObservableCollection<CANSignalConfig> ConfigurationList = new();
public ObservableCollection<AutoDBCLoadItem> dbcAutoLoadList = new(); public ObservableCollection<AutoDBCLoadItem> DBCAutoLoadList = new();
public ObservableCollection<ValueLimitVM> ValueLimitList = new(); public ObservableCollection<ValueLimitVM> ValueLimitList = new();
[JsonIgnore] [JsonIgnore]
public ObservableCollection<MonitorChannelVM> Channels = new(); public ObservableCollection<MonitorChannelVM> Channels = new();

View File

@@ -0,0 +1,17 @@
namespace UIShare.PubEvent
{
/// <summary>
/// DBC 自动加载时找不到文件事件。
/// 参数:(通道号, 文件路径)
/// </summary>
public class DBCFileNotFoundEvent : PubSubEvent<DBCFileNotFoundArgs>
{
}
public class DBCFileNotFoundArgs
{
public int Channel { get; set; }
public string FilePath { get; set; } = string.Empty;
public string Scope { get; set; } = string.Empty;
}
}