Files
CapMachine/CapMachine.Wpf/Services/LinDriveService.cs
2025-04-26 10:02:53 +08:00

300 lines
9.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using CapMachine.Model.CANLIN;
using CapMachine.Wpf.LinDrive;
using ImTools;
using Prism.Ioc;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CapMachine.Wpf.Services
{
/// <summary>
/// Lin驱动服务
/// </summary>
public class LinDriveService : BindableBase
{
public HighSpeedDataService HighSpeedDataService { get; }
/// <summary>
/// 实例化函数
/// </summary>
public LinDriveService(HighSpeedDataService highSpeedDataService, IContainerProvider containerProvider)
{
ToomossLinDrive = new ToomossLin(containerProvider);
//高速数据服务
HighSpeedDataService = highSpeedDataService;
//ToomossLinDrive.StartLinDrive();
}
/// <summary>
/// 当前选中的CanLinConfigPro 程序
/// </summary>
public CanLinConfigPro SelectedCanLinConfigPro { get; set; }
/// <summary>
/// 图莫斯 CAN Drive
/// ToomossLinDrive
/// </summary>
public ToomossLin ToomossLinDrive { get; set; }
/// <summary>
/// Ldf消息集合
/// 包括读取的实时值和数据
/// </summary>
public ObservableCollection<LinLdfModel> ListLinLdfModel { get; set; } = new ObservableCollection<LinLdfModel>();
/// <summary>
/// 初始化CAN的配置信息
/// </summary>
public void InitLinConfig(CanLinConfigPro selectedLinLinConfigPro)
{
//赋值配置数据
SelectedCanLinConfigPro = selectedLinLinConfigPro;
//为DBC实时数据关联配置的名称
foreach (var item in SelectedCanLinConfigPro.CanLinConfigContents)
{
var FindData = ListLinLdfModel.FindFirst(a => a.SignalName == item.SignalName);
if (FindData != null)
{
FindData.Name = item.Name;
}
}
}
/// <summary>
/// 开始DBC 配置文件 加载
/// </summary>
/// <returns></returns>
public ObservableCollection<LinLdfModel> StartLdf(string Path)
{
ListLinLdfModel = ToomossLinDrive.StartLdf(Path);
return ListLinLdfModel;
}
#region CAN
private int _AutoSpeedSv;
/// <summary>
/// 转速SV
/// </summary>
public int AutoSpeedSv
{
get { return _AutoSpeedSv; }
set { _AutoSpeedSv = value; RaisePropertyChanged(); }
}
/// <summary>
/// 转速 指令数据 实例
/// </summary>
private LinCmdData SpeedLinCmdData { get; set; }
/// <summary>
/// 功率限制 指令数据 实例
/// </summary>
private LinCmdData PwLimitLinCmdData { get; set; }
/// <summary>
/// 使能 指令数据 实例
/// </summary>
private LinCmdData EnableLinCmdData { get; set; }
/// <summary>
/// 要发送的CAN指令数据
/// 在程序配置好后就确定要发送哪些数据
/// </summary>
public List<LinCmdData> CmdData { get; set; } = new List<LinCmdData>();
/// <summary>
/// 增加发送的指令数据
/// </summary>
/// <param name="canCmdData"></param>
public void AddCmdData(LinCmdData SendLinCmdData)
{
//提取常用的实例数据
switch (SendLinCmdData.ConfigName)
{
case "转速":
SpeedLinCmdData = SendLinCmdData;
break;
case "功率限制":
PwLimitLinCmdData = SendLinCmdData;
break;
case "使能":
EnableLinCmdData = SendLinCmdData;
break;
case "Anti_Sleep":
//SpeedLinCmdData = SendLinCmdData;
break;
default:
break;
}
//添加到发送数据集合
CmdData.Add(SendLinCmdData);
}
/// <summary>
/// 更新速度信息
/// 默认是启动
/// </summary>
/// <param name="canCmdData"></param>
public void UpdateSpeedCmdData(double SpeedData)
{
if (SpeedLinCmdData != null)
{
AutoSpeedSv = (int)SpeedData;
SpeedLinCmdData.SignalCmdValue = SpeedData;
}
//if (EnableLinCmdData != null)
//{
// EnableLinCmdData.SignalCmdValue = 1;
//}
}
/// <summary>
/// 更新压缩机使能数据
/// </summary>
/// <param name="IsEnable"></param>
public void UpdateCapEnableCmdData(bool IsEnable)
{
if (EnableLinCmdData != null)
{
EnableLinCmdData.SignalCmdValue = IsEnable ? 1 : 0;
}
}
/// <summary>
/// 发送消息给CAN 驱动
/// </summary>
public void SendMsgToLinDrive(double SpeedData)
{
if (ToomossLinDrive.OpenState)
{
if (CmdData.Count > 0)
{
//更新速度信息
UpdateSpeedCmdData(SpeedData);
ToomossLinDrive.SendLinMsg(CmdData);
}
else
{
System.Windows.MessageBox.Show("未发现配置的数据内容", "提示", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Hand);
}
}
else
{
System.Windows.MessageBox.Show("未打开CAN通信无法发送数据", "提示", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Hand);
}
}
/// <summary>
/// 循环发送数据到CAN
/// </summary>
public void CycleSendMsg()
{
if (ToomossLinDrive.OpenState)
{
if (ToomossLinDrive.IsCycleSend == false)
{
if (CmdData.Count > 0)
{
ToomossLinDrive.IsCycleSend = true;
ToomossLinDrive.CmdData = CmdData;
ToomossLinDrive.StartCycleSendMsg();
}
else
{
System.Windows.MessageBox.Show("未发现配置的数据内容", "提示", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Hand);
}
}
else
{
ToomossLinDrive.IsCycleSend = false;
}
}
}
/// <summary>
///循环接收数据
/// </summary>
public void CycleReciveMsg()
{
if (ToomossLinDrive.OpenState)
{
if (ToomossLinDrive.IsCycleRevice == false)
{
if (ListLinLdfModel.Count > 0)
{
ToomossLinDrive.IsCycleRevice = true;
ToomossLinDrive.StartCycleReviceMsg();
}
else
{
System.Windows.MessageBox.Show("未发现配置的数据内容", "提示", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Hand);
}
}
else
{
ToomossLinDrive.IsCycleRevice = false;
}
}
}
/// <summary>
/// 获取数据值
/// 从DBC中获取数据给数据中心集合
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
public double GetLdfValueByName(string Name)
{
if (!ToomossLinDrive.IsCycleRevice) return 0;
if (ListLinLdfModel.Any(a => a.Name == Name))
{
//double.TryParse(ListLinLdfModel.FindFirst(a => a.Name == Name).SignalRtValue, out double Result1);
return double.TryParse(ListLinLdfModel.FindFirst(a => a.Name == Name).SignalRtValue.Split(" ")[0], out double Result) == true ? Result : 0;
}
return 0;
}
/// <summary>
/// 速度的数据的获取
/// 获取速度数据值
/// 从DBC中获取Speed数据给数据中心集合
/// </summary>
/// <param name="Name"></param>
/// <returns></returns>
public double GetLdfSpeedValueBySpeedName(string Name)
{
if (!ToomossLinDrive.IsCycleRevice) return 0;
if (ListLinLdfModel.Any(a => a.Name == Name))
{
//double.TryParse(ListLinLdfModel.FindFirst(a => a.Name == Name).SignalRtValue, out double Result1);
return double.TryParse(ListLinLdfModel.FindFirst(a => a.Name == Name).SignalRtValue.Split(" ")[0], out double Result) == true ? Result : 0;
}
return 0;
}
#endregion
}
}