using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Threading; using System.Windows; using System.Windows.Forms; using System.Windows.Input; using GummingCommon; using GummingEntity; using GummingSupport; using GummingControl; using System.Text; using GummingBusiness; using Newtonsoft.Json; using GummingLine; using System.Threading.Tasks; namespace Gumming { public class ControlDEVArmViewModule : ViewModelBase { #region base parameters private int rows = 20; private int pagenumber = 1; public ControlDEVArmView View { get; set; } private bool IsConfirm; private bool isLoading; private System.Timers.Timer timer; private HardFormulaFlowEntity HomeFlow; private bool Homing = false; private int MovingPostion = 0; #endregion #region constructor public ControlDEVArmViewModule() { View = new ControlDEVArmView(); View.DataContext = this; InitializeCommands(); InitializeParameters(); } private void InitializeCommands() { CancelCommand = new DelegateCommand(OnCancelCommand); SVONCommand = new DelegateCommand(OnSVONCommand); //JogForwardCommand = new DelegateCommand(OnJogForwardCommand); //JogBackCommand = new DelegateCommand(OnJogBackCommand); HomeCommand = new DelegateCommand(OnHomeCommand); PosMoveCommand = new DelegateCommand(OnPosMoveCommand); } public override void InitializeParameters(object content = null) { IsConfirm = false; WindowTitle = ""; //* if (timer == null) { timer = new System.Timers.Timer(); timer.Interval = 300; timer.Elapsed += timer_Elapsed; timer.Start(); }//*/ } public void ShowDialog() { IsConfirm = false; View.ShowDialog(); } ~ControlDEVArmViewModule() { } public void LoadDefaultValue() { SafeStart = Common.ToDecimal(HardParameters.Read(StationCode + (IsDevArm ? Constant.DEVSafeStart : Constant.DIN2SafeStart))); SafeEnd = Common.ToDecimal(HardParameters.Read(StationCode + (IsDevArm ? Constant.DEVSafeEnd : Constant.DIN2SafeEnd))); AxisSpeed = Common.ToDecimal(HardParameters.Read(StationCode + (IsDevArm ? Constant.DEVAxisSpeed : Constant.DIN2AxisSpeed))); string postionkey = (IsDevArm ? string.Format("{0}_Arm1DevHomingOffset", StationCode) : string.Format("{0}_Arm2RinseHomingOffset", StationCode)); var postionSet = Global.Settings.FirstOrDefault(q => q.RecId == postionkey); HomePosition = postionSet.SettingValue; HomeSpeed = Common.ToDecimal(HardParameters.Read(StationCode + (IsDevArm ? Constant.DEVHomeSpeed : Constant.DIN2HomeSpeed))); MovePosition = Common.ToDecimal(HardParameters.Read(StationCode + (IsDevArm ? Constant.DEVMovePosition : Constant.DIN2MovePosition))); MoveSpeed = Common.ToDecimal(HardParameters.Read(StationCode + (IsDevArm ? Constant.DEVMoveSpeed : Constant.DIN2MoveSpeed))); //默认Arm轴参数 ArmFlow = (IsDevArm ? HardParameters.GetDEVDEVAxis(StationCode) : HardParameters.GetDEVDIN2Axis(StationCode)); HomeFlow = (!IsDevArm ? HardParameters.GetCotPrAxis(StationCode) : HardParameters.GetCotEbrAxis(StationCode)); //设置使能状态 var axis = Global.Axises.FirstOrDefault(q => q.NodeId == ArmFlow.NodeId && q.SlotId == ArmFlow.SlotId); if (axis != null) { SVONStatus = axis.Energy; } } #endregion #region Binding Properties public string WindowTitle { get; private set; } public bool IsDevArm { get; set; } private ushort _MainAxisTopStatus; public ushort MainAxisTopStatus { get { return _MainAxisTopStatus; } set { _MainAxisTopStatus = value; OnPropertyChanged("MainAxisTopStatus"); } } private string _HeaderTitle; public string HeaderTitle { get { return _HeaderTitle; } set { _HeaderTitle = value; OnPropertyChanged("HeaderTitle"); } } private string _StationCode; public string StationCode { get { return _StationCode; } set { _StationCode = value; OnPropertyChanged("StationCode"); } } private ushort _SVONStatus; public ushort SVONStatus { get { return _SVONStatus; } set { _SVONStatus = value; OnPropertyChanged("SVONStatus"); } } private ushort _ForwardStatus; public ushort ForwardStatus { get { return _ForwardStatus; } set { _ForwardStatus = value; OnPropertyChanged("ForwardStatus"); } } private ushort _BackStatus; public ushort BackStatus { get { return _BackStatus; } set { _BackStatus = value; OnPropertyChanged("BackStatus"); } } private ushort _UpDownStatus; public ushort UpDownStatus { get { return _UpDownStatus; } set { _UpDownStatus = value; OnPropertyChanged("UpDownStatus"); } } private decimal _CurPosition; public decimal CurPosition { get { return _CurPosition; } set { _CurPosition = value; OnPropertyChanged("CurPosition"); } } private decimal _AxisSpeed; public decimal AxisSpeed { get { return _AxisSpeed; } set { _AxisSpeed = value; OnPropertyChanged("AxisSpeed"); } } private decimal _HomePosition; public decimal HomePosition { get { return _HomePosition; } set { _HomePosition = value; OnPropertyChanged("HomePosition"); } } private decimal _HomeSpeed; public decimal HomeSpeed { get { return _HomeSpeed; } set { _HomeSpeed = value; OnPropertyChanged("HomeSpeed"); } } private decimal _MovePosition; public decimal MovePosition { get { return _MovePosition; } set { _MovePosition = value; OnPropertyChanged("MovePosition"); } } private decimal _MoveSpeed; public decimal MoveSpeed { get { return _MoveSpeed; } set { _MoveSpeed = value; OnPropertyChanged("MoveSpeed"); } } private decimal _SafeStart; public decimal SafeStart { get { return _SafeStart; } set { _SafeStart = value; OnPropertyChanged("SafeStart"); } } private decimal _SafeEnd; public decimal SafeEnd { get { return _SafeEnd; } set { _SafeEnd = value; OnPropertyChanged("SafeEnd"); } } private string _ControlName; public string ControlName { get { return _ControlName; } set { _ControlName = value; OnPropertyChanged("ControlName"); } } private HardFormulaFlowEntity _ArmFlow; public HardFormulaFlowEntity ArmFlow { get { return _ArmFlow; } set { _ArmFlow = value; OnPropertyChanged("ArmFlow"); } } private bool _IsSub1Open; public bool IsSub1Open { get { return _IsSub1Open; } set { _IsSub1Open = value; OnPropertyChanged("IsSub1Open"); } } private bool _IsSub2Open; public bool IsSub2Open { get { return _IsSub2Open; } set { _IsSub2Open = value; OnPropertyChanged("IsSub2Open"); } } private bool _IsSub3Open; public bool IsSub3Open { get { return _IsSub3Open; } set { _IsSub3Open = value; OnPropertyChanged("IsSub3Open"); } } private ushort _HOMEStatus; public ushort HOMEStatus { get { return _HOMEStatus; } set { _HOMEStatus = value; OnPropertyChanged("HOMEStatus"); } } #endregion #region Binding Commands private ICommand _CancelCommand; public ICommand CancelCommand { get { return _CancelCommand; } set { _CancelCommand = value; OnPropertyChanged("CancelCommand"); } } private ICommand _PosMoveCommand; public ICommand PosMoveCommand { get { return _PosMoveCommand; } set { _PosMoveCommand = value; OnPropertyChanged("PosMoveCommand"); } } private ICommand _HomeCommand; public ICommand HomeCommand { get { return _HomeCommand; } set { _HomeCommand = value; OnPropertyChanged("HomeCommand"); } } private ICommand _JogBackCommand; public ICommand JogBackCommand { get { return _JogBackCommand; } set { _JogBackCommand = value; OnPropertyChanged("JogBackCommand"); } } private ICommand _JogForwardCommand; public ICommand JogForwardCommand { get { return _JogForwardCommand; } set { _JogForwardCommand = value; OnPropertyChanged("JogForwardCommand"); } } private ICommand _SVONCommand; public ICommand SVONCommand { get { return _SVONCommand; } set { _SVONCommand = value; OnPropertyChanged("SVONCommand"); } } #endregion #region Private Methods private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { int position = 0; if (ArmFlow != null && !Helpering.IsTerminate && this.IsActiveChildPage) { DeviceControl.GetPosition(ArmFlow.NodeId, ArmFlow.SlotId, ref position); if (Homing && ControlCheck.IsHomeDone(ArmFlow.NodeId, ArmFlow.SlotId))//恢复Home状态 { HOMEStatus = 0; Homing = false; } if (MovingPostion == position) { MainAxisTopStatus = 0; } /* 回原成功 byte[] Data2 = new byte[4]; DeviceControl.ReadPDOParameter(ArmFlow.NodeId, 0, 0x6064, 1, 4, ref Data2[0]); int dataa = TypeHelper.ByteArrayToInt(Data2); DeviceControl.ReadPDOParameter(ArmFlow.NodeId, 0, 0x607A, 1, 4, ref Data2[0]); int data2 = TypeHelper.ByteArrayToInt(Data2);*/ //监测针脚信号 ushort Index = 0x2155; ushort Datasize = 4; byte[] Data = new byte[4]; DeviceControl.ReadPDOParameter(ArmFlow.NodeId, 0, Index, 0, Datasize, ref Data[0]); int c = TypeHelper.ByteArrayToShort(Data); string values = Convert.ToString(c, 2); IsSub1Open = false; IsSub2Open = false; IsSub3Open = false; //原点最右侧 if (values.Length > 0) { IsSub3Open = values.Substring(values.Length - 1) == "1"; } //中间负极 if (values.Length > 1) { IsSub2Open = values.Substring(values.Length - 2, 1) == "1"; } //左侧正极 if (values.Length > 2) { IsSub1Open = values.Substring(values.Length - 3, 1) == "1"; } DeviceControl.IsEnergy(ArmFlow.NodeId); } View.Dispatcher.BeginInvoke(new Action(() => { CurPosition = BusinessHelper.ConvertFromDEVDegree(StationCode, (double)position, IsDevArm); }), null); } private void OnCancelCommand(object sender) { View.DialogResult = true; } private void OnSVONCommand(Object sender) { if (ArmFlow != null) { SVONStatus = (ushort)(SVONStatus != 1 ? 1 : 0); DeviceControl.SetSvon(ArmFlow.NodeId, ArmFlow.SlotId, SVONStatus); } } public void OnJogForwardStart() { if (!ControlCheck.IsHomeDone(HomeFlow.NodeId, HomeFlow.SlotId)) { ShowErrorBox(string.Format("请确保{0}在原点位置", IsDevArm ? "漂洗臂控制" : "显影臂控制")); return; } if (ArmFlow != null) { int axisSpeed = BusinessHelper.ConvertDEVDegree(StationCode, (double)AxisSpeed, IsDevArm); double axisAcc = Global.ArmAcc; SVONStatus = 1; if (ControlCheck.PMCInvoke(false, "SetSvon", null, () => DeviceControl.SetSvon(ArmFlow.NodeId, ArmFlow.SlotId, SVONStatus))) { ForwardStatus = 1; //匀速运动 ControlCheck.PMCInvoke(false, "StartMove", null, () => DeviceControl.StartMove(ArmFlow.NodeId, ArmFlow.SlotId, 0, axisSpeed, axisSpeed, axisAcc)); } } } public void OnJogForwardStop() { if (ArmFlow != null) { int axisSpeed = BusinessHelper.ConvertDEVDegree(StationCode, (double)AxisSpeed, IsDevArm); double axisAcc = Global.ArmAcc; SVONStatus = 1; if (ControlCheck.PMCInvoke(false, "SetSvon", null, () => DeviceControl.SetSvon(ArmFlow.NodeId, ArmFlow.SlotId, SVONStatus))) { ForwardStatus = 0; //匀速运动 ControlCheck.PMCInvoke(false, "StopMove", null, () => DeviceControl.StopMove(ArmFlow.NodeId, ArmFlow.SlotId, axisAcc)); } } } public void OnJogBackStart() { if (!ControlCheck.IsHomeDone(HomeFlow.NodeId, HomeFlow.SlotId)) { ShowErrorBox(string.Format("请确保{0}在原点位置", IsDevArm ? "漂洗臂控制" : "显影臂控制")); return; } if (ArmFlow != null) { int axisSpeed = BusinessHelper.ConvertDEVDegree(StationCode, (double)AxisSpeed, IsDevArm); double axisAcc = Global.ArmAcc; SVONStatus = 1; if (ControlCheck.PMCInvoke(false, "SetSvon", null, () => DeviceControl.SetSvon(ArmFlow.NodeId, ArmFlow.SlotId, SVONStatus))) { BackStatus = 1; //匀速运动 ControlCheck.PMCInvoke(false, "StartMove", null, () => DeviceControl.StartMove(ArmFlow.NodeId, ArmFlow.SlotId, 1, axisSpeed, axisSpeed, axisAcc)); } } } public void OnJogBackStop() { if (ArmFlow != null) { int axisSpeed = BusinessHelper.ConvertDEVDegree(StationCode, (double)AxisSpeed, IsDevArm); double axisAcc = Global.ArmAcc; SVONStatus = 1; if (ControlCheck.PMCInvoke(false, "SetSvon", null, () => DeviceControl.SetSvon(ArmFlow.NodeId, ArmFlow.SlotId, SVONStatus))) { BackStatus = 0; //匀速运动 ControlCheck.PMCInvoke(false, "StopMove", null, () => DeviceControl.StopMove(ArmFlow.NodeId, ArmFlow.SlotId, axisAcc)); } } } private void OnPosMoveCommand(Object sender) { if (!ControlCheck.IsHomeDone(HomeFlow.NodeId, HomeFlow.SlotId)) { ShowErrorBox(string.Format("请确保{0}在原点位置", IsDevArm ? "漂洗臂控制" : "显影臂控制")); return; } if (ArmFlow != null) { int axisSpeed = BusinessHelper.ConvertDEVDegree(StationCode, (double)AxisSpeed, IsDevArm); int axisPostion = BusinessHelper.ConvertDEVDegree(StationCode, (double)MovePosition, IsDevArm); double axisAcc = Global.ArmAcc; SVONStatus = 1; if (ControlCheck.PMCInvoke(false, "SetSvon", null, () => DeviceControl.SetSvon(ArmFlow.NodeId, ArmFlow.SlotId, SVONStatus))) { if (MainAxisTopStatus != 1) { MovingPostion = axisPostion; MainAxisTopStatus = 1; //匀速运动 ControlCheck.PMCInvoke(false, "StartMove", null, () => DeviceControl.StartMove(ArmFlow.NodeId, ArmFlow.SlotId, axisPostion, axisSpeed, axisSpeed, 0, (uint)axisAcc, (uint)axisAcc, 1)); } else { MainAxisTopStatus = 0; //停止运动 ControlCheck.PMCInvoke(false, "StopMove", null, () => DeviceControl.StopMove(ArmFlow.NodeId, ArmFlow.SlotId, Global.ArmAcc)); } } } } private void OnHomeCommand(Object sender) { if (ArmFlow != null) { if (HOMEStatus != 1) { Homing = true; HOMEStatus = 1; uint homeSpeed = (uint)BusinessHelper.ConvertDEVDegree(StationCode, (double)HomeSpeed, IsDevArm); int homePostion = BusinessHelper.ConvertDEVDegree(StationCode, (double)HomePosition, IsDevArm); if (ControlCheck.PMCInvoke(false, "SetSvon", null, () => DeviceControl.SetSvon(ArmFlow.NodeId, ArmFlow.SlotId, SVONStatus))) { ControlCheck.PMCInvoke(false, "StartMove", null, () => DeviceControl.GoArmHome(Global.ArmHomeWay, ArmFlow.NodeId, ArmFlow.SlotId, homePostion, homeSpeed)); } } else { HOMEStatus = 0; //匀速运动 ControlCheck.PMCInvoke(false, "StopMove", null, () => DeviceControl.StopMove(ArmFlow.NodeId, ArmFlow.SlotId, Global.StopAcc)); } } } public void OnSafeStartTextChangedCommand(string value) { HardParameterDA.Save(StationCode + (IsDevArm ? Constant.DEVSafeStart : Constant.DIN2SafeStart), Convert.ToString(value)); } public void OnSafeEndTextChangedCommand(string value) { HardParameterDA.Save(StationCode + (IsDevArm ? Constant.DEVSafeEnd : Constant.DIN2SafeEnd), Convert.ToString(value)); } public void OnAxisSpeedTextChangedCommand(string value) { HardParameterDA.Save(StationCode + (IsDevArm ? Constant.DEVAxisSpeed : Constant.DIN2AxisSpeed), Convert.ToString(value)); } public void OnHomePositionTextChangedCommand(string value) { //HardParameterDA.Save(StationCode + (IsDevArm ? Constant.DEVHomePosition : Constant.DIN2HomePosition), Convert.ToString(value)); string postionkey = (IsDevArm ? string.Format("{0}_Arm1DevHomingOffset", StationCode) : string.Format("{0}_Arm2RinseHomingOffset", StationCode)); var postionSet = Global.Settings.FirstOrDefault(q => q.RecId == postionkey); postionSet.SettingValue = Common.ToDecimal(value); SysSettingDA.Update(postionSet); } public void OnHomeSpeedTextChangedCommand(string value) { HardParameterDA.Save(StationCode + (IsDevArm ? Constant.DEVHomeSpeed : Constant.DIN2HomeSpeed), Convert.ToString(value)); } public void OnMovePositionTextChangedCommand(string value) { HardParameterDA.Save(StationCode + (IsDevArm ? Constant.DEVMovePosition : Constant.DIN2MovePosition), Convert.ToString(value)); } public void OnMoveSpeedTextChangedCommand(string value) { HardParameterDA.Save(StationCode + (IsDevArm ? Constant.DEVMoveSpeed : Constant.DIN2MoveSpeed), Convert.ToString(value)); } #endregion } }