From 6cab601d250a700f8111bcd3d2bea85cdce03431 Mon Sep 17 00:00:00 2001 From: tfarcraw <123211521@163.com> Date: Tue, 4 Aug 2026 13:57:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B0=94=E7=BC=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PLCSharpWpf/Models/DatasContext.cs | 6 + PLCSharpWpf/Models/GlobalModel.cs | 9 + PLCSharpWpf/VVMs/Cylinders/Cylinder.cs | 388 ++++++++++++++++++ PLCSharpWpf/VVMs/Cylinders/CylinderModel.cs | 188 +++++++++ PLCSharpWpf/VVMs/Cylinders/Cylinders.xaml | 147 +++++++ PLCSharpWpf/VVMs/Cylinders/Cylinders.xaml.cs | 29 ++ .../VVMs/Cylinders/CylindersViewModel.cs | 224 ++++++++++ 7 files changed, 991 insertions(+) create mode 100644 PLCSharpWpf/VVMs/Cylinders/Cylinder.cs create mode 100644 PLCSharpWpf/VVMs/Cylinders/CylinderModel.cs create mode 100644 PLCSharpWpf/VVMs/Cylinders/Cylinders.xaml create mode 100644 PLCSharpWpf/VVMs/Cylinders/Cylinders.xaml.cs create mode 100644 PLCSharpWpf/VVMs/Cylinders/CylindersViewModel.cs diff --git a/PLCSharpWpf/Models/DatasContext.cs b/PLCSharpWpf/Models/DatasContext.cs index e59a2b1..bd4010c 100644 --- a/PLCSharpWpf/Models/DatasContext.cs +++ b/PLCSharpWpf/Models/DatasContext.cs @@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore; using PLCSharp.Core.Common; using PLCSharp.VVMs.Authority; using PLCSharp.VVMs.Connects; +using PLCSharp.VVMs.Cylinders; using PLCSharp.VVMs.GlobalVariables; using PLCSharp.VVMs.Homepage; using PLCSharp.VVMs.MotionController; @@ -128,6 +129,11 @@ namespace PLCSharp.Models /// public DbSet RobotMatrices { get; set; } + /// + /// 气缸 + /// + public DbSet Cylinders { get; set; } + /// /// 错误Logs /// diff --git a/PLCSharpWpf/Models/GlobalModel.cs b/PLCSharpWpf/Models/GlobalModel.cs index e07cf6f..a5cd1ac 100644 --- a/PLCSharpWpf/Models/GlobalModel.cs +++ b/PLCSharpWpf/Models/GlobalModel.cs @@ -1,6 +1,7 @@ using PLCSharp.Core.Prism; using PLCSharp.Core.Tools; using PLCSharp.VVMs.Connects; +using PLCSharp.VVMs.Cylinders; using PLCSharp.VVMs.GlobalVariables; using PLCSharp.VVMs.ModeState; using PLCSharp.VVMs.MotionController; @@ -85,6 +86,9 @@ namespace PLCSharp.Models RobotModel = container.Resolve(); VisionsModel.Init(this); RobotModel.Init(this); + + CylindersModel = container.Resolve(); + CylindersModel.Init(this); bkgWorker = new BackgroundWorker { WorkerSupportsCancellation = true }; bkgWorker.DoWork += BackgroundWork; bkgWorker.RunWorkerAsync(); @@ -164,6 +168,11 @@ namespace PLCSharp.Models /// public RobotModel RobotModel { get; set; } + /// + /// 气缸模型 + /// + public CylinderModel CylindersModel { get; set; } + /// /// 全局变量 /// diff --git a/PLCSharpWpf/VVMs/Cylinders/Cylinder.cs b/PLCSharpWpf/VVMs/Cylinders/Cylinder.cs new file mode 100644 index 0000000..47be975 --- /dev/null +++ b/PLCSharpWpf/VVMs/Cylinders/Cylinder.cs @@ -0,0 +1,388 @@ +using PLCSharp.Core.Common; +using PLCSharp.VVMs.MotionController; +using Prism.Mvvm; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace PLCSharp.VVMs.Cylinders +{ + /// + /// 气缸感应类型 + /// + public enum SensorType + { + None, + OnlyA, + OnlyB, + Two, + } + + /// + /// 气缸 + /// + public class Cylinder : BindableBase + { + /// + /// 唯一标识 + /// + [Key] + public Guid ID { get; set; } = Guid.NewGuid(); + + /// + /// 关联的控制器ID + /// + public Guid ControllerID { get; set; } + + private string _Name; + + /// + /// 气缸名称 + /// + public string Name + { + get { return _Name; } + set + { + if (_Name != value) + { + Prompt = "已修改,请保存"; + } + SetProperty(ref _Name, value); + } + } + + private SensorType _SensorType; + + /// + /// 感应类型 + /// + public SensorType SensorType + { + get { return _SensorType; } + set + { + if (_SensorType != value) + { + Prompt = "已修改,请保存"; + } + SetProperty(ref _SensorType, value); + } + } + + private ushort _IOController; + /// + /// + /// + public ushort IOController + { + get { return _IOController; } + set { SetProperty(ref _IOController, value); } + } + + + private string _InA; + + /// + /// A位感应输入点 + /// + public string InA + { + get { return _InA; } + set + { + if (_InA != value) + { + Prompt = "已修改,请保存"; + } + SetProperty(ref _InA, value); + } + } + + private string _InB; + + /// + /// B位感应输入点 + /// + public string InB + { + get { return _InB; } + set + { + if (_InB != value) + { + Prompt = "已修改,请保存"; + } + SetProperty(ref _InB, value); + } + } + + private string _OutA; + + /// + /// A位输出点(DQ 名称或序号) + /// + public string OutA + { + get { return _OutA; } + set + { + if (_OutA != value) + { + Prompt = "已修改,请保存"; + } + SetProperty(ref _OutA, value); + } + } + + private string _OutB; + + /// + /// B位输出点(DQ 名称或序号) + /// + public string OutB + { + get { return _OutB; } + set + { + if (_OutB != value) + { + Prompt = "已修改,请保存"; + } + SetProperty(ref _OutB, value); + } + } + + private bool _OffWhenStopped; + + /// + /// 有停止命令时关闭输出 + /// + public bool OffWhenStopped + { + get { return _OffWhenStopped; } + set + { + if (_OffWhenStopped != value) + { + Prompt = "已修改,请保存"; + } + SetProperty(ref _OffWhenStopped, value); + } + } + + private bool _OffWhenInPosition; + + /// + /// 到位后关闭输出 + /// + public bool OffWhenInPosition + { + get { return _OffWhenInPosition; } + set + { + if (_OffWhenInPosition != value) + { + Prompt = "已修改,请保存"; + } + SetProperty(ref _OffWhenInPosition, value); + } + } + + private bool _Enabled = true; + /// + /// 启用 + /// + public bool Enabled + { + get { return _Enabled; } + set { SetProperty(ref _Enabled, value); } + } + + private bool _NoInterlock; + /// + /// 不需要互锁 + /// + public bool NoInterlock + { + get { return _NoInterlock; } + set { SetProperty(ref _NoInterlock, value); } + } + + private double _ATimeout; + /// + /// + /// + public double ATimeout + { + get { return _ATimeout; } + set { SetProperty(ref _ATimeout, value); } + } + + private double _BTimeout; + /// + /// + /// + public double BTimeout + { + get { return _BTimeout; } + set { SetProperty(ref _BTimeout, value); } + } + + + + + private double _ADelay; + /// + /// 感应到位延时 + /// + public double ADelay + { + get { return _ADelay; } + set { SetProperty(ref _ADelay, value); } + } + + + + private double _BDelay; + /// + /// 感应到位延时 + /// + public double BDelay + { + get { return _BDelay; } + set { SetProperty(ref _BDelay, value); } + } + + private string _Comment; + + /// + /// 备注 + /// + public string Comment + { + get { return _Comment; } + set + { + if (_Comment != value) + { + Prompt = "已修改,请保存"; + } + SetProperty(ref _Comment, value); + } + } + + private string _Prompt; + #region NotMapped Properties + private string _Cmd; + /// + /// 指令 "A"/"B"/"Stop" + /// + [NotMapped] + public string Cmd + { + get { return _Cmd; } + set { SetProperty(ref _Cmd, value); } + } + /// + /// 动作流程 + /// + [NotMapped] + public FlowModel Flow { get; set; } = new(); + /// + /// 提示(已修改,请保存) + /// + [NotMapped] + public string Prompt + { + get { return _Prompt; } + set { SetProperty(ref _Prompt, value); } + } + + private bool _StatusAIn; + + /// + /// A位感应状态 + /// + [NotMapped] + public bool StatusAIn + { + get { return _StatusAIn; } + set { SetProperty(ref _StatusAIn, value); } + } + + private bool _StatusBIn; + + /// + /// B位感应状态 + /// + [NotMapped] + public bool StatusBIn + { + get { return _StatusBIn; } + set { SetProperty(ref _StatusBIn, value); } + } + + + private bool _StatusAOut; + /// + /// A位输出状态 + /// + [NotMapped] + public bool StatusAOut + { + get { return _StatusAOut; } + + set { SetProperty(ref _StatusAOut, value); } + + } + + private bool _StatusBOut; + /// + /// A位输出状态 + /// + [NotMapped] + public bool StatusBOut + { + get { return _StatusBOut; } + set { SetProperty(ref _StatusAOut, value); } + } + + private string _FaultCode; + + /// + /// 故障码 + /// + [NotMapped] + public string FaultCode + { + get { return _FaultCode; } + set { SetProperty(ref _FaultCode, value); } + } + [NotMapped] + + public bool AComplit { get; set; } + + [NotMapped] + public bool BComplit { get; set; } + [NotMapped] + + public DI DIA { get; set; } + [NotMapped] + + public DI DIB { get; set; } + [NotMapped] + public DQ DQA { get; set; } + [NotMapped] + public DQ DQB { get; set; } + + + [NotMapped] + public DateTime ADelayTimer { get; set; } = DateTime.Now ; + + [NotMapped] + public DateTime BDelayTimer { get; set; } = DateTime.Now; + + #endregion + + } +} diff --git a/PLCSharpWpf/VVMs/Cylinders/CylinderModel.cs b/PLCSharpWpf/VVMs/Cylinders/CylinderModel.cs new file mode 100644 index 0000000..72d8013 --- /dev/null +++ b/PLCSharpWpf/VVMs/Cylinders/CylinderModel.cs @@ -0,0 +1,188 @@ +using PLCSharp.Core.Prism; +using PLCSharp.Models; +using Prism.Dialogs; +using Prism.Events; +using Prism.Ioc; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Threading; + +namespace PLCSharp.VVMs.Cylinders +{ + /// + /// 气缸模型 + /// + [Model] + public class CylinderModel : ModelBase + { + /// + /// 气缸模型 + /// + public CylinderModel(IContainerExtension container, IEventAggregator ea, IDialogService dialogService) : base(container, ea, dialogService) + { + bkgWorker = new BackgroundWorker { WorkerSupportsCancellation = true }; + bkgWorker.DoWork += BackgroundWork; + if (!bkgWorker.IsBusy) + bkgWorker.RunWorkerAsync(); + } + + private readonly BackgroundWorker bkgWorker; + + private void BackgroundWork(object sender, DoWorkEventArgs e) + { + var worker = (BackgroundWorker)sender; + while (!worker.CancellationPending) + { + Thread.Sleep(50); + try + { + + // 快照遍历,避免与 UI 线程增删气缸产生集合并发问题 + var list = Cylinders.ToList(); + for (int i = 0; i < list.Count; i++) + { + if (list[i] == null + || list[i].Enabled == false)//未启的气缸不进行处理 + + continue; + + var controller = GlobalModel.ControllersModel.Controllers.FirstOrDefault(c => c.ControllerNo == list[i].IOController); + if (controller == null) continue; + + // 更新 DI DQ + if (list[i].SensorType == SensorType.Two || list[i].SensorType == SensorType.OnlyA) + { + if (string.IsNullOrEmpty(list[i].InA) == false) + { + + if (list[i].DIA == null || list[i].DIA.Name != list[i].InA) + list[i].DIA = controller.DI.FirstOrDefault(d => d.Name == list[i].InA); + + } + + } + if (string.IsNullOrEmpty(list[i].OutA) == false) + { + if (list[i].DQA == null || list[i].DQA.Name != list[i].OutA) + list[i].DQA = controller.DQ.FirstOrDefault(d => d.Name == list[i].OutA); + } + + if (list[i].SensorType == SensorType.Two || list[i].SensorType == SensorType.OnlyB) + { + if (string.IsNullOrEmpty(list[i].InB) == false) + { + if (list[i].DIB == null || list[i].DIB.Name != list[i].InB) + list[i].DIB = controller.DI.FirstOrDefault(d => d.Name == list[i].InB); + } + + } + if (string.IsNullOrEmpty(list[i].OutB) == false) + { + if (list[i].DQB == null || list[i].DQB.Name != list[i].OutB) + list[i].DQB = controller.DQ.FirstOrDefault(d => d.Name == list[i].OutB); + } + + //气缸功能块 + FB(list[i]); + + } + + + } + catch (Exception ex) + { + SendErr(ex.Message); + } + } + } + + private void FB(Cylinder cylinder) + { + //如果气缸没有传感器 则判断气缸输出延时是否完成 + if (cylinder.SensorType == SensorType.None || cylinder.SensorType == SensorType.OnlyB) + { + if (cylinder.DQA != null && cylinder.DQA.Status) + { + cylinder.AComplit = cylinder.ADelayTimer.AddSeconds(cylinder.ADelay) < DateTime.Now; + } + else + { + cylinder.AComplit = false; + cylinder.ADelayTimer = DateTime.Now; + } + } + if (cylinder.SensorType == SensorType.None || cylinder.SensorType == SensorType.OnlyA) + { + if (cylinder.DQB != null && cylinder.DQB.Status) + { + cylinder.BComplit = cylinder.BDelayTimer.AddSeconds(cylinder.BDelay) < DateTime.Now; + } + else + { + cylinder.BComplit = false; + cylinder.BDelayTimer = DateTime.Now; + } + } + + + //如果气缸有传感器 则判断传感器倒位延时是否完成 + if (cylinder.SensorType == SensorType.Two || cylinder.SensorType == SensorType.OnlyA) + { + if (cylinder.DIA != null && cylinder.DIA.Status) + { + cylinder.AComplit = cylinder.ADelayTimer.AddSeconds(cylinder.ADelay) < DateTime.Now; + } + else + { + cylinder.AComplit = false; + cylinder.ADelayTimer = DateTime.Now; + } + } + if (cylinder.SensorType == SensorType.Two || cylinder.SensorType == SensorType.OnlyB) + { + if (cylinder.DIB != null && cylinder.DIB.Status) + { + cylinder.BComplit = cylinder.BDelayTimer.AddSeconds(cylinder.BDelay) < DateTime.Now; + } + else + { + cylinder.BComplit = false; + cylinder.BDelayTimer = DateTime.Now; + } + } + + + } + + + /// + /// 全局模型 + /// + public GlobalModel GlobalModel { get; set; } + + private ObservableCollection _Cylinders = []; + + /// + /// 气缸列表 + /// + public ObservableCollection Cylinders + { + get { return _Cylinders; } + set { SetProperty(ref _Cylinders, value); } + } + + /// + /// 初始化 + /// + /// 全局模型 + public void Init(GlobalModel globalModel) + { + GlobalModel = globalModel; + foreach (var item in _DatasContext.Cylinders) + { + Cylinders.Add(item); + } + } + + } +} diff --git a/PLCSharpWpf/VVMs/Cylinders/Cylinders.xaml b/PLCSharpWpf/VVMs/Cylinders/Cylinders.xaml new file mode 100644 index 0000000..0fcadd8 --- /dev/null +++ b/PLCSharpWpf/VVMs/Cylinders/Cylinders.xaml @@ -0,0 +1,147 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +