FrmMain.cs 6.1 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    public partial class FrmMain : Form
    {
        private readonly log4net.ILog LOG;
        private Neotel.Rmaxis axis;
        private bool testLoop;
        private FrmMain frmSecond;
        private System.Threading.Thread tStatus;
        private System.Threading.Thread tTest;

        public FrmMain()
        {
            InitializeComponent();
            LOG = log4net.LogManager.GetLogger("Test");
        }

        public FrmMain(bool sameTime) : this()
        {
            BtnNewWindow.Visible = !sameTime;
            BtnSameTime.Visible = !sameTime;
        }

        public void LoopTestTrigger()
        {
            if (testLoop)
            {
                StopTest();
                return;
            }

            testLoop = true;
            BtnStability.Text = "停止";
            int[] param = new int[5];
            param[0] = Convert.ToInt32(NudForce2.Value);
            param[1] = Convert.ToInt32(NudDistance2.Value);
            param[2] = Convert.ToInt32(NudVelocity3.Value);
            param[3] = Convert.ToInt32(NudTimes.Value);
            param[4] = Convert.ToInt32(NudInterval.Value);
            tTest = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(PushTest));
            tTest.Start(param);
        }





        private void ReadStatus()
        {
            while (true)
            {
                if (!axis.IsPortOpen) break;
                float t = axis.GetTorque();
                System.Threading.Thread.Sleep(20);
                float v = axis.GetVelocity();
                System.Threading.Thread.Sleep(20);
                float p = axis.GetPosition();
                System.Threading.Thread.Sleep(20);
                LblStatus.Invoke(new Action(() =>
                {
                    LblStatus.Text = string.Format("出力:{0}\r\n速度:{1}\r\n位置:{2}\r\n错误代码:{3}", t, v, p, axis.ErrorCode);
                }));
                System.Threading.Thread.Sleep(200);
            }
        }

        private void PushTest(object obj)
        {
            int[] param = (int[])obj;
            int times = 0;
            int timeout = 5000;
            bool push = false;
            bool rtn;
            
            while (testLoop)
            {
                if (times == param[3])
                {
                    StopTest();
                    break;
                }
                if (push)
                {
                    rtn = axis.PushWait(param[0], param[1], param[2], timeout);
                    times++;
                }
                else
                {
                    rtn = axis.GoHomeWait(timeout);
                }

                Invoke(new Action(() => { TsslStatus.Text = string.Format("PushWait {0} 次数{1}", rtn, times); }));

                if (!rtn)
                {
                    StopTest();
                    break;
                }

                push = !push;
                System.Threading.Thread.Sleep(param[4]);
            }

        }

        private void StopTest()
        {
            testLoop = false;
            BtnStability.Invoke(new Action(() => { BtnStability.Text = "测试"; }));
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            for (int i = 1; i <= 30; i++)
                CboPort.Items.Add("COM" + i);
            CboPort.SelectedIndex = 0;
            for (int i = 0; i < 10; i++)
                CboAxisNo.Items.Add(i.ToString());
            CboAxisNo.SelectedIndex = 0;
            axis = new Neotel.Rmaxis();
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            axis.ClosePort();
            if (tStatus != null) tStatus.Abort();
        }

        private void BtnOpenPort_Click(object sender, EventArgs e)
        {
            bool rtn = axis.OpenPort(CboPort.Text, Convert.ToUInt16(CboAxisNo.Text));
            TsslStatus.Text = BtnOpenPort.Text + " " + rtn;
            if (rtn)
            {
                LblVersion.Text = "版本号\r\n" + axis.GetVersion();
                tStatus = new System.Threading.Thread(new System.Threading.ThreadStart(ReadStatus));
                tStatus.Start();
            }
        }

        private void BtnClosePort_Click(object sender, EventArgs e)
        {
            axis.ClosePort();
            if (tStatus != null) tStatus.Abort();
            TsslStatus.Text = BtnClosePort.Text;
        }

        private void BtnGoHome_Click(object sender, EventArgs e)
        {
            bool rtn = axis.GoHomeWait(5000);
            TsslStatus.Text = "GoHomeWait " + rtn;
        }

        private void BtnPush_Click(object sender, EventArgs e)
        {
            bool rtn = axis.PushWait(Convert.ToSingle(NudForce1.Value),
                Convert.ToSingle(NudDistance1.Value), Convert.ToSingle(NudVelocity1.Value), 5000);
            TsslStatus.Text = "PushWait " + rtn + "  IsPushEmpty=" + axis.IsPushEmpty;
        }

        private void BtnAbsolute_Click(object sender, EventArgs e)
        {
            bool rtn = axis.MoveAbsoluteWait(Convert.ToSingle(NudPosition.Value),
               Convert.ToSingle(NudVelocity2.Value), Convert.ToSingle(NudAcceleration.Value),
               Convert.ToSingle(NudDeacceleration.Value), Convert.ToSingle(NudBand.Value), 5000);
            TsslStatus.Text = "MoveAbsoluteWait " + rtn;
        }

        private void BtnReset_Click(object sender, EventArgs e)
        {
            axis.ResetError();
        }

        private void BtnStability_Click(object sender, EventArgs e)
        {
            LoopTestTrigger();
        }

        private void BtnNewWindow_Click(object sender, EventArgs e)
        {
            frmSecond = new FrmMain(true);
            frmSecond.Show();
        }

        private void BtnSameTime_Click(object sender, EventArgs e)
        {
            LoopTestTrigger();
            frmSecond.LoopTestTrigger();
        }
    }
}