using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using nRTMC;

namespace 二维比较
{
    public partial class Form1 : Form
    {
        private ushort cardConnectNo = 0; //卡的连接号
        private UInt32 functionReturn = 0; //用于接收函数返回值

        enum enum_Compare_Dim
        {
            Compare_1D = 1, //一维软件比较
            Compare_2D = 2 //二维软件比较
        }

        public Form1()
        {
            InitializeComponent();
            string[] axisNumStr = new string[12];
            for (int i = 0; i < axisNumStr.Length; i++)
            {
                axisNumStr[i] = "轴" + i;
            }
            comboBox_1axisNo.Items.AddRange(axisNumStr);
            comboBox_2axisNo.Items.AddRange(axisNumStr);
            comboBox_1axisNo.SelectedIndex = 0;
            comboBox_2axisNo.SelectedIndex = 1;

            comboBox_1compareSource.Items.AddRange(new object[] { "指令位置", "反馈位置" });
            comboBox_2compareSource.Items.AddRange(new object[] { "指令位置", "反馈位置" });
            comboBox_1compareSource.SelectedIndex = 0;
            comboBox_2compareSource.SelectedIndex = 0;

            comboBox_1cmpMode.Items.AddRange(new object[] { "小于", "大于等于" });
            comboBox_2CmpMode.Items.AddRange(new object[] { "小于", "大于等于" });
            comboBox_1cmpMode.SelectedIndex = 0;
            comboBox_2CmpMode.SelectedIndex = 0;

            comboBox_triggerMode.Items.AddRange(new object[] { "IO输出低", "IO输出高", "IO输出反向", "IO输出脉冲", "停止轴" });
            comboBox_triggerMode.SelectedIndex = 0;

            string[] outputIOStr = new string[2];//界面上只显示out0,out1
            for (int i = 0; i < outputIOStr.Length; i++)
            {
                outputIOStr[i] = "OutPut " + i;
            }
            comboBox_triggerPrm.Items.Clear();
            comboBox_triggerPrm.Items.AddRange(outputIOStr);
            comboBox_triggerPrm.SelectedIndex = 0;
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            #region 开卡
            ushort pciNum = 0; //用于接收开卡成功的卡数
            ushort[] pcieIndex = new ushort[12];
            functionReturn = RTMC.MC_Open(0, nRTMC.class_Controller_Connect_Type.CONNECTION_PCI, "", ref pciNum, pcieIndex);
            if (functionReturn == 0)//函数成功执行
            {
                if (pciNum > 0)//至少一张卡被成功开卡
                {
                    cardConnectNo = pcieIndex[0]; //如果有多卡的情况下这里也只用卡1演示

                    #region 获取卡信息
                    //--------------------------获取卡信息------------------------------------------------------------------------
                    ushort totalAxisNum = 0; //用于获取卡的轴数
                    ushort linerNum = 0;//用于获取卡的插补系个数
                    ushort totalInNum = 0;//用于获取卡的输入口数量
                    ushort totalOutNum = 0;//用于获取卡的输出口数量
                    ushort totalPWMNum = 0;//用于获取卡的PWM通道数量
                    functionReturn = RTMC.MC_GetTotalAxesAndIONum(cardConnectNo, ref totalAxisNum, ref linerNum, ref totalInNum, ref totalOutNum, ref totalPWMNum);

                    //------------------------------------------------------------------------------------------------------------
                    #endregion

                    timer1.Start(); //启动界面监控定时器
                }
                else
                {
                    MessageBox.Show("未找到卡!");
                    return;
                }
            }
            else
            {
                MessageBox.Show("开卡失败!");
                return;
            }
            #endregion
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();
            timer1.Dispose();
            RTMC.MC_Close(cardConnectNo); //关卡释放资源
        }

        /// <summary>
        /// 触发模式选择项改变之后引发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox_triggerMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox useCombobox = sender as ComboBox;
            //这里如果触发模式选择了停止轴的话，触发参数的选项栏就该改为轴号选择
            if (useCombobox.SelectedIndex == 4)
            {
                comboBox_triggerPrm.Items.Clear();
                string[] axisNumStr = new string[12];
                for (int i = 0; i < axisNumStr.Length; i++)
                {
                    axisNumStr[i] = "轴" + i;
                }
                comboBox_triggerPrm.Items.AddRange(axisNumStr);
                comboBox_triggerPrm.SelectedIndex = 0;
            }
            else
            {
                comboBox_triggerPrm.Items.Clear();
                string[] outputIOStr = new string[2];
                for (int i = 0; i < outputIOStr.Length; i++)
                {
                    outputIOStr[i] = "OutPut " + i;
                }
                comboBox_triggerPrm.Items.AddRange(outputIOStr);
                comboBox_triggerPrm.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// 启动比较按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {

            ushort cmp2DAxis0 = Convert.ToUInt16(comboBox_1axisNo.SelectedIndex);
            ushort cmp2DAxis1 = Convert.ToUInt16(comboBox_2axisNo.SelectedIndex);

            ushort cmp2DposSrc0 = Convert.ToUInt16(comboBox_1compareSource.SelectedIndex);
            ushort cmp2DposSrc1 = Convert.ToUInt16(comboBox_2compareSource.SelectedIndex);

            double pos0 = Convert.ToDouble(numericUpDown_1cmpPos.Value);
            double pos1 = Convert.ToDouble(numericUpDown_2CmpPos.Value);

            ushort cmp2DMode0 = Convert.ToUInt16(comboBox_1cmpMode.SelectedIndex);
            ushort cmp2DMode1 = Convert.ToUInt16(comboBox_2CmpMode.SelectedIndex);

            ushort triggerMode = Convert.ToUInt16(comboBox_triggerMode.SelectedIndex);
            ushort triggerPrm = Convert.ToUInt16(comboBox_triggerPrm.SelectedIndex);
            double outTime = Convert.ToDouble(numericUpDown_ioOutTime.Value);

            ushort[] cmpAxis = new ushort[2] { cmp2DAxis0, cmp2DAxis1 };
            ushort[] posSrc = new ushort[2] { cmp2DposSrc0, cmp2DposSrc1 };
            double[] pos = new double[2] { pos0, pos1 };
            ushort[] cmpMode = new ushort[2] { cmp2DMode0, cmp2DMode1 };

            uint rtVal = RTMC.MC_StartSoft2DCmp(cardConnectNo, //启动比较
                0,
                cmpAxis,
                posSrc,
                1,
                pos,
                cmpMode,
                triggerMode,
                triggerPrm,
                outTime
                );

            //启动比较之后启动相关轴运动
            ushort axis0 = ((ushort)comboBox_1axisNo.SelectedIndex);
            RTMC.MC_Power(cardConnectNo, axis0, 1);
            RTMC.MC_MoveVelocity(cardConnectNo, axis0, 100, 100, 1000, 0.1, 0.1, 0, 1000000);
            groupBox4.Text = "轴" + axis0;


            ushort axis1 = ((ushort)comboBox_2axisNo.SelectedIndex);
            RTMC.MC_Power(cardConnectNo, axis1, 1);
            RTMC.MC_MoveVelocity(cardConnectNo, axis1, 100, 100, 1000, 0.1, 0.1, 0, 1000000);
            groupBox5.Text = "轴" + axis1;



        }

        /// <summary>
        /// 停止比较按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            RTMC.MC_StopSoftCmp(cardConnectNo,((ushort)enum_Compare_Dim.Compare_2D), 0);
            RTMC.MC_StopEmgImd(cardConnectNo);
        }

        /// <summary>
        /// 比较器清零按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            RTMC.MC_ClearSoftCmp(cardConnectNo, ((ushort)enum_Compare_Dim.Compare_2D), 0);
        }

        /// <summary>
        /// 位置清除按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button13_Click(object sender, EventArgs e)
        {
            ushort axis0 = ((ushort)comboBox_1axisNo.SelectedIndex);
            RTMC.MC_SetAxisCmdPos(cardConnectNo, axis0, 0);

            ushort axis1 = ((ushort)comboBox_2axisNo.SelectedIndex);
            RTMC.MC_SetAxisCmdPos(cardConnectNo, axis1, 0);
        }


        /// <summary>
        /// output0点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (pictureBox1.BackColor == Color.Green)
            {
                RTMC.MC_SetOutIoBit(cardConnectNo, 0, 1);
            }
            else
            {
                RTMC.MC_SetOutIoBit(cardConnectNo, 0, 0);
            }
        }

        /// <summary>
        /// output1点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            if (pictureBox2.BackColor == Color.Green)
            {
                RTMC.MC_SetOutIoBit(cardConnectNo, 1, 1);
            }
            else
            {
                RTMC.MC_SetOutIoBit(cardConnectNo, 1, 0);
            }
        }


        /// <summary>
        /// 用于更新界面的定时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            #region 比较器状态
            ushort enable = 0;
            double running_pos = 0;
            UInt32 completed_points = 0;
            UInt32 remained_points = 0;
            uint rtVal = RTMC.MC_GetSoftCmpStatus(cardConnectNo,
                ((ushort)enum_Compare_Dim.Compare_2D), //比较维数
                0, //比较通道
                ref enable, //比较器使能
                ref running_pos, //当前比较点位置
                ref completed_points, //已完成比较点
                ref remained_points); //剩余比较点

            switch (enable)
            {
                case 0:
                    textBox_enableState.Text = "未启用";
                    break;
                case 1:
                    textBox_enableState.Text = "使能";
                    break;
                default:
                    textBox_enableState.Text = "未定义";
                    break;
            }

            textBox_runningPoint.Text = running_pos.ToString();
            textBox_yetPoints.Text = completed_points.ToString();
            textBox_usablePoints.Text = remained_points.ToString();
            #endregion

            #region 轴状态
            ushort axis0 = ((ushort)comboBox_1axisNo.SelectedIndex);
            double cmdPos = 0;
            RTMC.MC_GetAxisCmdPos(cardConnectNo, axis0, ref cmdPos);//获取当前指令位置
            textBox2.Text = cmdPos.ToString();

            double cmdSpeed = 0;
            RTMC.MC_GetAxisCmdSpeed(cardConnectNo, axis0, ref cmdSpeed); //获取当前指令速度
            textBox1.Text = cmdSpeed.ToString();

            ushort checkDone = 1;
            RTMC.MC_GetAxisCheckDone(cardConnectNo, axis0, ref checkDone);//获取轴运动状态
            switch (checkDone)
            {
                case 0:
                    textBox3.Text = "运动中";
                    break;
                case 1:
                    textBox3.Text = "空闲";
                    break;
                default:
                    textBox3.Text = checkDone.ToString();
                    break;
            }


            ushort axis1 = ((ushort)comboBox_2axisNo.SelectedIndex);
            RTMC.MC_GetAxisCmdPos(cardConnectNo, axis1, ref cmdPos);//获取当前指令位置
            textBox4.Text = cmdPos.ToString();

            RTMC.MC_GetAxisCmdSpeed(cardConnectNo, axis1, ref cmdSpeed); //获取当前指令速度
            textBox6.Text = cmdSpeed.ToString();

            RTMC.MC_GetAxisCheckDone(cardConnectNo, axis1, ref checkDone);//获取轴运动状态
            switch (checkDone)
            {
                case 0:
                    textBox5.Text = "运动中";
                    break;
                case 1:
                    textBox5.Text = "空闲";
                    break;
                default:
                    textBox5.Text = checkDone.ToString();
                    break;
            }

            #endregion

            #region 输出口状态
            byte ioState = 0;
            RTMC.MC_GetOutIoBit(cardConnectNo, 0, ref ioState);
            if (ioState == 0) pictureBox1.BackColor = Color.Green;
            else pictureBox1.BackColor = Color.Red;

            RTMC.MC_GetOutIoBit(cardConnectNo, 1, ref ioState);
            if (ioState == 0) pictureBox2.BackColor = Color.Green;
            else pictureBox2.BackColor = Color.Red;

            #endregion

        }
    }
}
