AxisControl.cs 9.3 KB
using DeviceLibrary;
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TheMachine
{
    public partial class AxisControl : UserControl
    {
        public AxisControl()
        {
            InitializeComponent();
            RobotManage.LoadFinishEvent += RobotManage_LoadFinishEvent;
            if (DesignMode)
                return;
            crc.LanguageChangeEvent += Crc_LanguageChangeEvent;
            lblLed.Text = "";
            lblOkValue.Text = "";
            lblSensor.Text = "";
            bool showC = false;
            if (Setting_Init.Device_LedLight_PortName != "")
            {
                showC = true;
            }
            if (Setting_Init.Enable_HideColorTestBtn)
            {
                showC = false;
            }


            btnYellow.Visible = showC;
            btnGree.Visible = showC;
            btnChangeColor.Visible = showC;
            button1.Visible = showC;

            if (Setting_Init.Enable_HideAxisTestBtn)
            {
                btnAxisRTest.Visible = false;
                btnAxisRStop.Visible = false;
            }
            else
            {
                btnAxisRTest.Visible = true;
                btnAxisRStop.Visible = true;
            }
        }

        private void Crc_LanguageChangeEvent(object sender, EventArgs e)
        {
            RobotManage_LoadFinishEvent(true, "");
        }

        private void RobotManage_LoadFinishEvent(bool state, string msg)
        {
            if (this.InvokeRequired)
            {
                this.Invoke((EventHandler)delegate
                {
                    RobotManage_LoadFinishEvent(state, msg);
                });
                return;
            }

            if (!state)
                return;
            axisMoveControl1.LoadData(AxisBean.List);
            configControl1.Config = RobotManage.Config; 
            LogUtil.info("伺服面板加载完成.");
            timer1.Start();

        }





        private void AxisControl_Load(object sender, EventArgs e)
        {

        }
        volatile bool roateloop = false;
        private void button2_Click(object sender, EventArgs e)
        {
            //Task.Run(() => {
            //    var p1 = RobotManage.mainMachine.Config.UpDown_P1;
            //    var p1_speed = RobotManage.mainMachine.Config.UpDown_P1_speed;
            //    var axis = RobotManage.mainMachine.UpDown_Axis;
            //    roateloop = true;
            //    while (roateloop)
            //    {
            //        axis.AbsMove(null, p1, p1_speed);
            //        Task.Delay(200).Wait();
            //        while (axis.IsBusy)
            //        {
            //            Task.Delay(100).Wait();
            //        }
            //        if (!roateloop)
            //            break;
            //        Task.Delay(500).Wait();
            //        axis.AbsMove(null, 1, p1_speed);
            //        Task.Delay(200).Wait();
            //        while (axis.IsBusy)
            //        {
            //            Task.Delay(100).Wait();
            //        }
            //        Task.Delay(500).Wait();
            //    }

            //});

            Task.Run(() => {
                var p1 = RobotManage.mainMachine.Config.Middle_P1;
                var p1_speed = RobotManage.mainMachine.Config.Middle_P1_speed;
                var axis = RobotManage.mainMachine.Middle_Axis;
                roateloop = true;
                while (roateloop)
                {
                    axis.AbsMove(null, p1, p1_speed);
                    Task.Delay(200).Wait();
                    while (axis.IsBusy)
                    {
                        Task.Delay(100).Wait();
                    }
                    if (!roateloop)
                        break;
                    Task.Delay(500).Wait();
                    axis.AbsMove(null, 1, p1_speed);
                    Task.Delay(200).Wait();
                    while (axis.IsBusy)
                    {
                        Task.Delay(100).Wait();
                    }
                    Task.Delay(500).Wait();
                }

            });
        }

        private void button3_Click(object sender, EventArgs e)
        {
            roateloop = false;
        }
        private bool isPro = false;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!this.Visible|| RobotManage.mainMachine == null)
            {
                return;
            }
            if (isPro)
            {
                return;
            }
            isPro = true;
            Task.Run(() =>
            {
                try
                {
                    if (Setting_Init.Device_LedLight_PortName != "")
                    {
                        lblLed.Text = "LED" + Setting_Init.Device_LedLight_PortName +","+RobotManage.mainMachine.lastS+ ":" + RobotManage.mainMachine.lastColor.ToString();
                    }
                    else
                    {
                        lblLed.Text = "";
                    }

                    if (Setting_Init.Device_DauxiKS107_PortName != "")
                    {
                        RobotManage.dauxiKS107Controller.Quary(out int currValue, out string msg);
                        lblSensor.Text = $"{crc.GetString("shengbosensor", "声波传感器")}{Setting_Init.Device_DauxiKS107_PortName}:" + currValue + ", /" + Setting_Init.Device_DauxiKS107_BaseValue + "/" + Setting_Init.Device_DauxiKS107_ErrorValue;
                    }
                    else
                    {
                        lblSensor.Text = "";
                    }
                    if (Setting_Init.Device_WeightSensor_PortName != "")
                    {
                        lblOkValue.Text = $"{crc.GetString("yalisensor", "压力传感器")}{Setting_Init.Device_WeightSensor_PortName}: {Setting_Init.Device_WeightSensor_MaxValue}";
                    }
                    else
                    {
                        lblOkValue.Text = "";
                    }

                }
                catch (Exception ex)
                {
                    LogUtil.error("出错:" + ex.ToString());
                }
                finally
                {
                    isPro = false;
                }
            });
        }

        private void btnYellow_Click(object sender, EventArgs e)
        {
            RobotManage.mainMachine.ShowYellowLight("test");
        }

        private void btnGree_Click(object sender, EventArgs e)
        {

            RobotManage.mainMachine.ShowGreenLight("test");
        }

        private void btnChangeColor_Click(object sender, EventArgs e)
        {
            DialogResult result = colorDialog1.ShowDialog();
            if (result.Equals(DialogResult.OK))
            {
                Color color = colorDialog1.Color;
                btnChangeColor.BackColor = color;
                RobotManage.mainMachine.ShowColor(color,color.Name);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RobotManage.mainMachine.CloseColor();
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            //判断是否空闲中
            if (RobotManage.mainMachine.isInBusy())
            {
                MessageBox.Show(crc.GetString("Res0006.a91d68b9","操作失败:当前忙碌中"));
                return;
            }
            if (RobotManage.mainMachine.runStatus != RunStatus.Running)
            {
                MessageBox.Show(crc.GetString("Res0007.925e3b2f","操作失败:设备未在待机状态"));
                return;
            }
            DialogResult result = MessageBox.Show(crc.GetString("Res0018.65e96146", "启动料叉检测后需要复位设备,确定启动?"), crc.GetString("tips", "提示"), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result.Equals(DialogResult.Yes))
            {
                LogUtil.info("点击按钮:" + btnCheck.Text + ", 确认开始料叉检测");
                RobotManage.mainMachine.StartToCheck();
                MessageBox.Show(crc.GetString("Res0019.52571443", "操作成功:请在料叉到位后进行检查,确认后请点检测完成按钮!"));
            }
        }

        private void btnCheckOk_Click(object sender, EventArgs e)
        {
            if (RobotManage.mainMachine.ResetMoveInfo.IsStep(MoveStep.CH06_WaitCheck))
            {

                LogUtil.info("点击按钮:" + btnCheck.Text + ", 确认检查完成");
                bool result = RobotManage.mainMachine.CheckOk();
                LogUtil.info("点击按钮:" + btnCheck.Text + ", CheckOk 结果:" + result);
                if(result)
                {

                    MessageBox.Show(crc.GetString("Res0021.33130f5c","操作成功"));
                }
                else
                {

                    MessageBox.Show(crc.GetString("Res0022.5fa802be","操作失败"));
                }
            }
            else
            {
                MessageBox.Show(crc.GetString("Res0022.5fa802be","操作失败"));
            }
        }
    }
}