diff --git a/CapMachine.Wpf/LinDrive/ToomossLin.cs b/CapMachine.Wpf/LinDrive/ToomossLin.cs index b3a33e5..06d5d18 100644 --- a/CapMachine.Wpf/LinDrive/ToomossLin.cs +++ b/CapMachine.Wpf/LinDrive/ToomossLin.cs @@ -1,5 +1,6 @@ using CapMachine.Model.CANLIN; using CapMachine.Wpf.CanDrive; +using CapMachine.Wpf.Dtos; using CapMachine.Wpf.Services; using ImTools; using Microsoft.VisualBasic; @@ -675,11 +676,25 @@ namespace CapMachine.Wpf.LinDrive } + /// + /// 定时扫描更新数据 扫描Task + /// + private static Task CycleUpdateCmdTask { get; set; } + + /// + /// 定时更新时间 + /// + private int UpdateCycle { get; set; } = 500; + /// /// 调度表数量 /// public int SchCount { get; set; } + /// + /// LIN 调度表的配置信息 + /// + public List ListLINScheduleConfig { get; set; } /// /// 获取LIN的LINScheduleConfig @@ -701,7 +716,7 @@ namespace CapMachine.Wpf.LinDrive SchTabIndex = i, MsgName = GetSchFrameName(SchName, j), MsgNameIndex = j, - Cycle = 10, + Cycle = 100, IsActive = false, }); } @@ -752,7 +767,7 @@ namespace CapMachine.Wpf.LinDrive return pSchName.ToString(); } - Random Random=new Random(); + Random Random = new Random(); /// /// 开始调度表执行 @@ -788,15 +803,77 @@ namespace CapMachine.Wpf.LinDrive } } - LDFParser.LDF_ExeSchToBus(LDFHandle, new StringBuilder(itemMsg.Key),1); - LDFParser.LDF_SetSchToTable(LDFHandle, new StringBuilder(itemMsg.Key),1); + LDFParser.LDF_ExeSchToBus(LDFHandle, new StringBuilder(itemMsg.Key), 1); + LDFParser.LDF_SetSchToTable(LDFHandle, new StringBuilder(itemMsg.Key), 1); } - + } + /// + /// 循环更新调度表的指令数据 + /// 定时更新数据到调度表中 + /// + public void StartCycleUpdateCmd() + { + CycleUpdateCmdTask = Task.Run(async () => + { + while (IsCycleSend) + { + await Task.Delay(UpdateCycle); + try + { + + if (CmdData.Count() == 0) return; + + //防止有多个不同的消息名称/帧,每个帧单独处理发送 + var GroupMsg = CmdData.GroupBy(x => x.MsgName); + foreach (var itemMsg in GroupMsg) + { + foreach (var itemSignal in itemMsg) + { + if (itemSignal.ConfigName.Contains("速")) + { + itemSignal.SignalCmdValue = Random.NextDouble() * 1000; + } + //主机写操作,发送数据给从机 + LDFParser.LDF_SetSignalValue(LDFHandle, new StringBuilder(itemMsg.Key), new StringBuilder(itemSignal.SignalName), itemSignal.SignalCmdValue); + } + + ////【0】参数注意 + //LDFParser.LDF_ExeFrameToBus(LDFHandle, new StringBuilder(itemMsg.Key), 0); + + //读取当前的指令帧数据,LDF_ExeFrameToBus执行后就可以读取本身的数据 + foreach (var item in ListLinLdfModel) + { + if (CmdData.Any(a => a.MsgName == item.MsgName)) + { + LDFParser.LDF_GetSignalValueStr(LDFHandle, new StringBuilder(item.MsgName), new StringBuilder(item.SignalName), ReadValueStr); + item.SignalRtValueSb = ReadValueStr; + } + } + + LDFParser.LDF_ExeSchToBus(LDFHandle, new StringBuilder(itemMsg.Key), 1); + LDFParser.LDF_SetSchToTable(LDFHandle, new StringBuilder(itemMsg.Key), 1); + } + + + } + catch (Exception ex) + { + IsSendOk = false; + LoggerService.Info($"时间:{DateTime.Now.ToString()}-【MSG】-{ex.Message}"); + } + } + + IsSendOk = false; + }); + } + + + public void LinTest() { diff --git a/CapMachine.Wpf/Services/LinDriveService.cs b/CapMachine.Wpf/Services/LinDriveService.cs index 4f97a3e..325c894 100644 --- a/CapMachine.Wpf/Services/LinDriveService.cs +++ b/CapMachine.Wpf/Services/LinDriveService.cs @@ -1,4 +1,5 @@ using CapMachine.Model.CANLIN; +using CapMachine.Wpf.Dtos; using CapMachine.Wpf.LinDrive; using ImTools; using Prism.Ioc; @@ -121,6 +122,11 @@ namespace CapMachine.Wpf.Services /// public List CmdData { get; set; } = new List(); + /// + /// 调度表LIN配置信息 + /// + public List ListLINScheduleConfig { get; set; } = new List(); + /// /// 增加发送的指令数据 /// @@ -356,10 +362,42 @@ namespace CapMachine.Wpf.Services { if (CmdData.Count > 0) { - ToomossLinDrive.IsCycleSend = true; + //ToomossLinDrive.IsCycleSend = true; ToomossLinDrive.CmdData = CmdData; - ToomossLinDrive.StartPrecisionCycleSendMsg(); + //ToomossLinDrive.StartPrecisionCycleSendMsg(); //ToomossLinDrive.StartCycleSendMsg(); + + if (ToomossLinDrive.SchEnable) + { + //使用调度表的话,需要在调度表中配置信息后才可以进行操作 + var GroupMsg = ToomossLinDrive.CmdData.GroupBy(a => a.MsgName).ToList(); + foreach (var itemMsg in GroupMsg) + { + if (!ListLINScheduleConfig.ToList().Any(a => a.MsgName == itemMsg.Key)) + { + System.Windows.MessageBox.Show($"你使能了调度表,但是调度表中没有设置【{itemMsg.Key}】信息,请设置后再操作", "提示", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Hand); + return; + } + } + + if (ListLINScheduleConfig == null && ListLINScheduleConfig!.Count() == 0) + { + System.Windows.MessageBox.Show("调度表配置为空数据,请检查!将无法发送数据", "提示", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Hand); + return; + } + + ToomossLinDrive.ListLINScheduleConfig = ListLINScheduleConfig!; + ToomossLinDrive.StartSchedule(); + ToomossLinDrive.StartCycleUpdateCmd(); + } + else + { + ToomossLinDrive.StartPrecisionCycleSendMsg(); + //ToomossCanDrive.StartCycleSendMsg(); + } + + ToomossLinDrive.IsCycleSend = true; + } else { diff --git a/CapMachine.Wpf/ViewModels/LinConfigViewModel.cs b/CapMachine.Wpf/ViewModels/LinConfigViewModel.cs index 12a422a..bab14d8 100644 --- a/CapMachine.Wpf/ViewModels/LinConfigViewModel.cs +++ b/CapMachine.Wpf/ViewModels/LinConfigViewModel.cs @@ -901,6 +901,48 @@ namespace CapMachine.Wpf.ViewModels set { _DataBaudRateCbxItems = value; RaisePropertyChanged(); } } + + private DelegateCommand _SchEnableCmd; + /// + /// 调度表的使能 操作的指令 + /// + public DelegateCommand SchEnableCmd + { + set + { + _SchEnableCmd = value; + } + get + { + if (_SchEnableCmd == null) + { + _SchEnableCmd = new DelegateCommand((Par) => SchEnableCmdCall(Par)); + } + return _SchEnableCmd; + } + } + /// + /// 调度表的使能信息 + /// + /// + /// + private void SchEnableCmdCall(object par) + { + var dd = SelectedLINConfigExdDto.SchEnable; + var Result = (bool)par; + if (Result) + { + LinDriveService.ToomossLinDrive.SchEnable = true; + + } + else + { + LinDriveService.ToomossLinDrive.SchEnable = false; + + } + } + + private DelegateCommand _LinOpCmd; /// /// LIN操作的指令 @@ -1047,6 +1089,12 @@ namespace CapMachine.Wpf.ViewModels break; case "CycleSend"://循环发送你 + + //有可能加载完毕后不手动改变状态,导致值没有传输,所以这里赋值 + LinDriveService.ToomossLinDrive.SchEnable = SelectedLINConfigExdDto.SchEnable; + //无论有没有调度表,都要把这个配置给LinDriveService + LinDriveService.ListLINScheduleConfig = ListLINScheduleConfigDto.ToList(); + //循环发送数据 LinDriveService.CycleSendMsg(); @@ -1260,8 +1308,8 @@ namespace CapMachine.Wpf.ViewModels //LinDriveService.ToomossLinDrive.LinTest(); - LinDriveService.ToomossLinDrive.StartSchedule(); - break; + //LinDriveService.ToomossLinDrive.StartSchedule(); + //break; // if (LinDriveService.ToomossLinDrive.OpenState) { diff --git a/CapMachine.Wpf/Views/LINConfigView.xaml b/CapMachine.Wpf/Views/LINConfigView.xaml index c2ef49b..9216a88 100644 --- a/CapMachine.Wpf/Views/LINConfigView.xaml +++ b/CapMachine.Wpf/Views/LINConfigView.xaml @@ -652,8 +652,8 @@ Width="40" Margin="5,2" Command="{Binding SchEnableCmd}" - CommandParameter="{Binding SelectedCANConfigExdDto.SchEnable}" - IsChecked="{Binding SelectedCANConfigExdDto.SchEnable}" + CommandParameter="{Binding SelectedLINConfigExdDto.SchEnable}" + IsChecked="{Binding SelectedLINConfigExdDto.SchEnable}" Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="启用调度表" />