diff --git a/HexcalMC/MainFrom.cs b/HexcalMC/MainFrom.cs index 37cdc87..8405c6f 100644 --- a/HexcalMC/MainFrom.cs +++ b/HexcalMC/MainFrom.cs @@ -4,8 +4,8 @@ using System.Diagnostics; using System.IO; using System.Net; using System.Runtime.InteropServices; +using System.Threading; using System.Windows.Forms; -using System.Windows.Threading; using ACS.SPiiPlusNET; using HexcalMC.Base; using HexcalMC.Form; @@ -47,13 +47,11 @@ namespace HexcalMC InitializeComponent(); - //处理未捕获的异常 - Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); - //处理UI线程异常 - Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); - //处理非UI线程异常 - AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); + // 处理未被捕获的线程异常 + Application.ThreadException += Application_ThreadException; + // 处理未被捕获的非UI线程异常 + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; } private void MainFrom_Load(object sender, EventArgs e) @@ -70,8 +68,6 @@ namespace HexcalMC //启动界面刷新 timer_RefreshUI.Start(); - - } private void MainFrom_Shown(object sender, EventArgs e) //窗体显示准备好接受用户输入时发生 @@ -543,23 +539,27 @@ namespace HexcalMC #region 异常抓取 //实现函数 - private void DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) - { - MessageBox.Show($"Application_ThreadException:{e.Exception}"); - e.Handled = true; // 标记为 “已处理”,避免异常进一步传递而引起崩溃 - } - private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) + + private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { - // 在这里处理未被捕获的异常 - // 您可以记录异常、显示错误信息等等 // 防止程序终止 MessageBox.Show("发生了未处理的异常:" + e.Exception.Message); } - private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + + + private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { - MessageBox.Show($"CurrentDomain_UnhandledException: {e.ExceptionObject}"); + if (e.ExceptionObject is Exception ex) + { + HandleException(ex); + } + } + + private static void HandleException(Exception ex) + { + MessageBox.Show($"发生了未处理的异常:{ex.Message}"); } @@ -572,12 +572,33 @@ namespace HexcalMC _acs.EnableEvent(Interrupts.ACSC_INTR_ETHERCAT_ERROR); _acs.EnableEvent(Interrupts.ACSC_INTR_MOTOR_FAILURE); _acs.EnableEvent(Interrupts.ACSC_INTR_MOTION_FAILURE); + _acs.EnableEvent(Interrupts.ACSC_INTR_PHYSICAL_MOTION_END); _acs.COMMCHANNELCLOSED += _acs_COMMCHANNELCLOSED; _acs.MOTORFAILURE += _acs_MOTORFAILURE; _acs.MOTIONFAILURE += _acs_MOTIONFAILURE; _acs.SYSTEMERROR += _acs_SYSTEMERROR; _acs.ETHERCATERROR += _acs_ETHERCATERROR; _acs.EMERGENCY += _acs_EMERGENCY; + _acs.PHYSICALMOTIONEND += _acs_PHYSICAL_MOTION_END; + } + + private void DisableFaultEvent() + { + _acs.COMMCHANNELCLOSED -= _acs_COMMCHANNELCLOSED; + _acs.MOTORFAILURE -= _acs_MOTORFAILURE; + _acs.MOTIONFAILURE -= _acs_MOTIONFAILURE; + _acs.SYSTEMERROR -= _acs_SYSTEMERROR; + _acs.ETHERCATERROR -= _acs_ETHERCATERROR; + _acs.EMERGENCY -= _acs_EMERGENCY; + _acs.PHYSICALMOTIONEND -= _acs_PHYSICAL_MOTION_END; + + _acs.DisableEvent(Interrupts.ACSC_INTR_COMM_CHANNEL_CLOSED); + _acs.DisableEvent(Interrupts.ACSC_INTR_EMERGENCY); + _acs.DisableEvent(Interrupts.ACSC_INTR_SYSTEM_ERROR); + _acs.DisableEvent(Interrupts.ACSC_INTR_ETHERCAT_ERROR); + _acs.DisableEvent(Interrupts.ACSC_INTR_MOTOR_FAILURE); + _acs.DisableEvent(Interrupts.ACSC_INTR_MOTION_FAILURE); + _acs.DisableEvent(Interrupts.ACSC_INTR_PHYSICAL_MOTION_END); } //关联函数 @@ -638,6 +659,31 @@ namespace HexcalMC DebugDfn.AddLogText($"[CommError] Error Message:{_acs.GetErrorString((int)param)}"); } + private void _acs_PHYSICAL_MOTION_END(AxisMasks axis) + { + int bit = 0x01; + int axisNo = 0; + + //DebugDfn.AddLogText(string.Format(" {0}", axis)); + + for (int i = 0; i < 64; i++) + { + if ((int)axis == bit) + { + axisNo = i; + break; + } + + bit = bit << 1; + } + + if (axisNo == 2) + { + return; + } + + //DebugDfn.AddLogText(string.Format(" - Axis {0}, Stoppped", axisNo)); + } #endregion @@ -792,10 +838,14 @@ namespace HexcalMC private void Btn_ACSStop_Click(object sender, EventArgs e) //断开连接 { - if (_mAcsConnected) _acs.CloseComm(); + DisableFaultEvent(); + if (_mAcsConnected) + { + _acs.CloseComm(); + } tmrMonitor.Stop(); - + _mAcsConnected = false; btn_ACSStart.Enabled = true; btn_ACSStop.Enabled = false; } @@ -1019,9 +1069,10 @@ namespace HexcalMC _mlblEnable[1] = lblEnable1; _mlblEnable[2] = lblEnable2; - EnableFaultEvent();//订阅错误事件 + EnableFaultEvent(); //订阅错误事件 } + public static bool IsWithinLimit(Point3D point) //判断点是否在行程范围内 { if (point.X >= XMinstrokesw && point.X <= XMaxstrokesw && @@ -1136,6 +1187,16 @@ namespace HexcalMC private void rtb_quick_loc_Click(object sender, EventArgs e) { //获取输入 + + // 获取文本框的值 + double x = double.Parse(rtb_SetX.Text); + double y = double.Parse(rtb_Sety.Text); + double z = double.Parse(rtb_SetZ.Text); + + // 构造 Point3D 对象 + Point3D point = new Point3D(x, y, z); + + SetPositionXyz(point); } #endregion ACS平台相关 diff --git a/HexcalMC/Motion/Motion.Designer.cs b/HexcalMC/Motion/Motion.Designer.cs index 91f274a..a427460 100644 --- a/HexcalMC/Motion/Motion.Designer.cs +++ b/HexcalMC/Motion/Motion.Designer.cs @@ -198,6 +198,7 @@ this.btn_X_left = new System.Windows.Forms.Button(); this.btn_Y_Forward = new System.Windows.Forms.Button(); this.groupBox6 = new System.Windows.Forms.GroupBox(); + this.button1 = new System.Windows.Forms.Button(); this.btn_home = new System.Windows.Forms.Button(); this.groupBox10 = new System.Windows.Forms.GroupBox(); this.label48 = new System.Windows.Forms.Label(); @@ -207,7 +208,6 @@ this.checkBox_Z = new System.Windows.Forms.CheckBox(); this.checkBox_y = new System.Windows.Forms.CheckBox(); this.checkBox_X = new System.Windows.Forms.CheckBox(); - this.button1 = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.grpMotionTest.SuspendLayout(); this.grpMst.SuspendLayout(); @@ -1954,6 +1954,16 @@ this.groupBox6.TabStop = false; this.groupBox6.Text = "动作"; // + // button1 + // + this.button1.Location = new System.Drawing.Point(94, 24); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(75, 30); + this.button1.TabIndex = 3; + this.button1.Text = "立即停止"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.BtnHallAll_Click); + // // btn_home // this.btn_home.Location = new System.Drawing.Point(7, 24); @@ -2046,16 +2056,6 @@ this.checkBox_X.Text = "X"; this.checkBox_X.UseVisualStyleBackColor = true; // - // button1 - // - this.button1.Location = new System.Drawing.Point(94, 24); - this.button1.Name = "button1"; - this.button1.Size = new System.Drawing.Size(75, 30); - this.button1.TabIndex = 3; - this.button1.Text = "立即停止"; - this.button1.UseVisualStyleBackColor = true; - this.button1.Click += new System.EventHandler(this.BtnHallAll_Click); - // // Motion // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; diff --git a/HexcalMC/Motion/Motion.cs b/HexcalMC/Motion/Motion.cs index b3c3bc6..2ef44b0 100644 --- a/HexcalMC/Motion/Motion.cs +++ b/HexcalMC/Motion/Motion.cs @@ -1150,6 +1150,7 @@ namespace HexcalMC } } + private void btn_Y_Back_MouseDown(object sender, MouseEventArgs e) { double lfVelocity = 0.0f;