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

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

        public Form1()
        {
            InitializeComponent();
        }

        /// <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);
                    numericUpDown1.Minimum = 0;
                    numericUpDown1.Maximum = totalAxisNum - 1; //设置界面最大能选择的轴号，轴号参数从0开始。

                    comboBox_countMode.Items.AddRange(new object[] { "脉冲方向计数", "AB相计数，1倍频", "AB相计数，2倍频", "AB相计数，4倍频" });
                    comboBox_countDir.Items.AddRange(new object[] { "计数方向不取反", "计数方向取反" });
                    ushort mode = 0;
                    ushort dir = 0;
                    RTMC.MC_GetAuxEncoderPrm(cardConnectNo, //获取卡默认设置的编码器模式
                        0,
                        ref mode,
                        ref dir);
                    comboBox_countMode.SelectedIndex = mode;
                    comboBox_countDir.SelectedIndex = dir;
                    //------------------------------------------------------------------------------------------------------------
                    #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();
            RTMC.MC_Close(cardConnectNo); //关卡释放资源
        }


        /// <summary>
        /// 启动运动按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            ushort axisNo = ((ushort)numericUpDown1.Value);//轴号

            RTMC.MC_Power(cardConnectNo, axisNo, 1); //轴使能，轴使能之后才能够通过指令启动运动发出脉冲

            RTMC.MC_SetAuxEncoderPrm(cardConnectNo,
                        0,
                        (ushort)comboBox_countMode.SelectedIndex,
                        (ushort)comboBox_countDir.SelectedIndex); //设置辅助编码器计数模式

            double multi = Convert.ToDouble(numericUpDown_axis0.Value); //手轮倍率
            
            RTMC.MC_SetHandleWheelPrm(cardConnectNo,
                axisNo, 0, multi); //设置手轮参数
           RTMC.MC_StartHandleWheel(cardConnectNo,axisNo); //启动手轮运动
        }

        /// <summary>
        /// 停止运动按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            ushort axisNo = ((ushort)numericUpDown1.Value);//轴号

            //参数三，stop_mode = 0时停止模式为减速停止，=1时为立即停止
            //参数四，stop_time为减速停止时的减速时间
            RTMC.MC_StopAxis(cardConnectNo, axisNo, 0, 0.1);

        }

        /// <summary>
        /// 紧急停止按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            RTMC.MC_StopEmgImd(cardConnectNo); //急停函数，所有轴立即停止
        }

        /// <summary>
        /// 用于更新界面信息的定时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            ushort axisNo = ((ushort)numericUpDown1.Value);//轴号
            double cmdPos = 0;
            RTMC.MC_GetAxisCmdPos(cardConnectNo, axisNo, ref cmdPos);//获取当前指令位置
            textBox10.Text = cmdPos.ToString();

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

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

            double handWheelEncoderPos = 0;
            RTMC.MC_GetAuxEncoderPositon(cardConnectNo, 0, ref handWheelEncoderPos); //获取辅助编码器值
            textBox1.Text = handWheelEncoderPos.ToString();
        }

        /// <summary>
        /// 位置清除按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            ushort axisNo = ((ushort)numericUpDown1.Value);
            RTMC.MC_SetAxisCmdPos(cardConnectNo, axisNo, 0);//指令位置清零

            RTMC.MC_SetAuxEncoderPositon(cardConnectNo,0,0); //辅助编码器值清零
        }
    }
}
