using PLCSharp.Core.Prism; using PLCSharp.Core.Tools; using PLCSharp.Models; using Prism.Commands; using Prism.Mvvm; using System.Collections.ObjectModel; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading; using System.Windows; namespace PLCSharp.VVMs.MotionController { /// /// Controller /// public class Controller : BindableBase { private static DateTime _lastConnErrLog = DateTime.MinValue; /// /// 唯一标识 /// [Key] public Guid ID { get; set; } = Guid.NewGuid(); private ushort _ControllerNo; /// /// ControllerNo /// public ushort ControllerNo { get { return _ControllerNo; } set { SetProperty(ref _ControllerNo, value); } } private ControllerType _Type; /// /// 类型 /// public ControllerType Type { get { return _Type; } set { if (_Type == ControllerType.Undefined) { if (value != ControllerType.Undefined) { SetProperty(ref _Type, value); } } else if (value == ControllerType.Undefined) { // 允许重置为Undefined(删除控制器) SetProperty(ref _Type, value); } else if (_Type != value) { MessageBox.Show("选定型号后不可改变!"); } } } /// /// 初始化 /// public void Init() { bkgWorker = WorkerGuard.Watch(new BackgroundWorker { WorkerSupportsCancellation = true }, "控制器扫描"); bkgWorker.DoWork += BackgroundWork; if (!bkgWorker.IsBusy) bkgWorker.RunWorkerAsync(); } /// /// Axes /// [NotMapped] public ObservableCollection Axes { get; set; } = []; /// /// DI /// [NotMapped] public ObservableCollection DI { get; set; } = []; /// /// DQ /// [NotMapped] public ObservableCollection DQ { get; set; } = []; private void BackgroundWork(object sender, DoWorkEventArgs e) { var worker = (BackgroundWorker)sender; while (!worker.CancellationPending) { Thread.Sleep(1); if (_Type == ControllerType.Undefined) { Thread.Sleep(1000); continue; } if (SDK == null) { switch (_Type) { case ControllerType.SMC304: SDK = new SMC(); break; case ControllerType.EMC_E3064_A08: //case ControllerType.EMC_E3064_A16: //case ControllerType.EMC_E3064_A12: //case ControllerType.EMC_E3064_A24: //case ControllerType.EMC_E3064_A32: //case ControllerType.EMC_E3064_A64: //case ControllerType.EMC_E5064_A08: //case ControllerType.EMC_E5064_A12: //case ControllerType.EMC_E5064_A16: //case ControllerType.EMC_E5064_A24: //case ControllerType.EMC_E5064_A32: //case ControllerType.EMC_E5064_A64: SDK = new EMC(); break; default: MessageBox.Show("未定义的控制器类型,请检查代码!"); _Type = ControllerType.Undefined; break; } } if (SDK == null) { Thread.Sleep(1000); continue; } if (Connected == false) { Thread.Sleep(1000); Online = NetTool.PingIP(IP); if (SDK == null) { continue; } if (Online) { Connected = SDK.Init(ControllerNo, IP); if (Connected) { } else { if ((DateTime.Now - _lastConnErrLog).TotalSeconds > 10) { _lastConnErrLog = DateTime.Now; GlobalModel.SendErr("连接失败,请检查IP地址或控制器是否在线"); } Thread.Sleep(1000); } } else { Connected = false; if ((DateTime.Now - _lastConnErrLog).TotalSeconds > 10) { _lastConnErrLog = DateTime.Now; GlobalModel.SendErr("连接失败,请检查IP地址或控制器是否在线"); } Thread.Sleep(1000); } } else { AxesRun(); IORun(); } } } private void IORun() { if (DI.Count > 0) { SDK.DI_Count = DI.Count; SDK.GetDI(); for (int i = 0; i < DI.Count; i++) { var group = i / 32; var bit = i % 32; DI[i].Status = !SDK.DI[group].GetBit(bit); } } if (DQ.Count > 0) { SDK.DQ_Count = DQ.Count; SDK.GetDQ(); for (int i = 0; i < DQ.Count; i++) { var group = i / 32; var bit = i % 32; DQ[i].Status = !SDK.DQ[group].GetBit(bit); } } } private DelegateCommand _DQON; public DelegateCommand DQON => _DQON ??= new DelegateCommand(ExecuteDQON); void ExecuteDQON(object param) { if (param is DQ dq) { SetDQ(dq.Number, 1); } } private DelegateCommand _DQOFF; public DelegateCommand DQOFF => _DQOFF ??= new DelegateCommand(ExecuteDQOFF); void ExecuteDQOFF(object param) { if (param is DQ dq) { SetDQ(dq.Number, 0); } } /// /// 设置DQ /// /// 端口 /// off_on public void SetDQ(ushort port, ushort off_on) { SDK.SetDQ(port, off_on); } private void AxesRun() { for (int i = 0; i < Axes.Count; i++) { SDK.Get_Axis_Status(Axes[i].AxisNo); if (SDK.AxisIOs.Count >= Axes.Count) { Axes[i].ORG = SDK.AxisIOs[Axes[i].AxisNo].ORG; Axes[i].PowerOn = SDK.AxisIOs[Axes[i].AxisNo].PowerOn; Axes[i].ALM = SDK.AxisIOs[Axes[i].AxisNo].ALM; Axes[i].LimitN = SDK.AxisIOs[Axes[i].AxisNo].LimitN; Axes[i].LimitP = SDK.AxisIOs[Axes[i].AxisNo].LimitP; Axes[i].Moving = SDK.AxisIOs[Axes[i].AxisNo].Moving; Axes[i].CommandPosition = SDK.AxisIOs[Axes[i].AxisNo].CommandPosition; Axes[i].EncoderPosition = SDK.AxisIOs[Axes[i].AxisNo].EncoderPosition; Axes[i].Velocity = SDK.AxisIOs[Axes[i].AxisNo].Velocity; } else { } if (GlobalModel.ModeState.Mode.Production) { if (Axes[i].PowerOn == false) { SDK.Power(Axes[i].AxisNo, true); } } } //运动流程 for (int i = 0; i < Axes.Count; i++) { var axis = Axes[i]; if (axis.MotionFlow.Step > 0) { switch (axis.MotionFlow.Step) { case 1: axis.ErrCode = SDK.CreateORG(axis.AxisNo, axis.Params); if (axis.ErrCode != 0) { axis.MotionFlow.Step = 0; } else { axis.MotionFlow.Step++; } break; case 2: if (SDK.CheckORG(axis.AxisNo)) { axis.ORGCreated = true; axis.MotionFlow.Step = 0; } break; case 11: axis.ErrCode = SDK.Move(axis.AxisNo, 1, axis.Params); if (axis.ErrCode != 0) { axis.MotionFlow.Step = 0; } else { axis.MotionFlow.Step++; } break; case 12: if (axis.Moving == false) { if (axis.CommandPosition == axis.Params.TargetPos) { axis.AbsoluteDone = true; } axis.MotionFlow.Step = 0; } break; case 21: axis.ErrCode = SDK.Move(axis.AxisNo, 0, axis.Params); if (axis.ErrCode != 0) { axis.MotionFlow.Step = 0; } else { axis.MotionFlow.Step++; } break; case 22: if (axis.Moving == false) { axis.RelativeDone = true; axis.MotionFlow.Step = 0; } break; case 31: axis.ErrCode = SDK.MulticoorMove(axis.Interpolations, axis.InterpolationGroupParams); if (axis.ErrCode != 0) { axis.MotionFlow.Step = 0; } else { axis.MotionFlow.Step++; } break; case 32: axis.MotionFlow.Step = 0; break; case 101: Stop(axis); break; } } } } private ControllerSDK SDK; private BackgroundWorker bkgWorker; private string _IP; /// /// IP /// public string IP { get { return _IP; } set { SetProperty(ref _IP, value); } } private string _Comment; /// /// 注释 /// public string Comment { get { return _Comment; } set { if (_Comment != value) { Prompt = "已修改,请保存"; } SetProperty(ref _Comment, value); } } /// /// 序列化Params /// [Column("Params")] public string SerializedParams { get => JsonSerializer.Serialize(Params, JsonOptions.Default); // 自动序列化 set => Params = JsonSerializer.Deserialize(value, JsonOptions.Default); // 自动反序列化 } private string _Prompt; /// /// 提示 /// [NotMapped] public string Prompt { get { return _Prompt; } set { SetProperty(ref _Prompt, value); } } private bool _Online; /// /// Online /// [NotMapped] public bool Online { get { return _Online; } set { SetProperty(ref _Online, value); } } private bool _Connected; /// /// Connected /// [NotMapped] public bool Connected { get { return _Connected; } set { SetProperty(ref _Connected, value); } } private void ParamsChanged() { Prompt = "已修改,请保存"; } /// /// 关闭 /// public virtual void Close() { SDK?.Close(); } /// /// 删除控制器:停轴、关闭SDK、复位型号为Undefined并清空运行时数据 /// public void Reset() { foreach (var axis in Axes) { axis.Stop(); } Thread.Sleep(100); Type = ControllerType.Undefined; Close(); SDK = null; Connected = false; Online = false; Prompt = ""; Axes.Clear(); DI.Clear(); DQ.Clear(); } /// /// 初始化 /// /// ControllerNo /// ip /// 返回布尔值 public virtual bool Init(ushort ControllerNo, string ip) { return false; } /// /// 停止 /// /// 目标轴 internal void Stop(Axis axis) { axis.MotionFlow.Step = 0; SDK.Stop(axis.AxisNo); if (axis.ALM) { SDK.Reset(axis.AxisNo); } } /// /// PowerOn /// /// 目标轴 internal void PowerOn(Axis axis) { axis.MotionFlow.Step = 0; SDK.Power(axis.AxisNo, true); } /// /// PowerOff /// /// 目标轴 internal void PowerOff(Axis axis) { axis.MotionFlow.Step = 0; SDK.Power(axis.AxisNo, false); } /// /// 保存 /// /// 目标轴 internal void Save(Axis axis) { SDK.Save(axis.AxisNo, axis.Params); } private ControllerParams _Params; /// /// 参数集合 /// [NotMapped] public ControllerParams Params { get { _Params ??= new ControllerParams(); return _Params; } set { SetProperty(ref _Params, value); } } /// /// ControllerParams /// public class ControllerParams : BindableBase { private string _Prompt; /// /// 提示 /// [JsonIgnore] public string Prompt { get { return _Prompt; } set { SetProperty(ref _Prompt, value); } } } /// /// 全局模型 /// [NotMapped] public GlobalModel GlobalModel { get; set; } } }