using nRTMC;
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;
using System.Configuration;

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

        public Form1()
        {
            InitializeComponent();
            comboBox1.SelectedIndex= 0;
            comboBox2.SelectedIndex= 0;
            comboBox3.SelectedIndex = 8;
        }

        /// <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

                    //------------------使用轴专用信号前需要使能并且设置有效电平—————————————————————————
                    ushort _useAxis = 0; //这里只设置轴0，演示用

                    //默认设置，低电平有效，减速停止
                    RTMC.MC_SetAxisInIoPrm(cardConnectNo, _useAxis, 0, 1, 0, 0);//负限位
                    RTMC.MC_SetAxisInIoPrm(cardConnectNo, _useAxis, 1, 1, 0, 0);//正限位
                    RTMC.MC_SetAxisInIoPrm(cardConnectNo, _useAxis, 2, 1, 0, 0);//原点
                    RTMC.MC_SetAxisInIoPrm(cardConnectNo, _useAxis, 3, 1, 0, 0);//伺服到位
                    RTMC.MC_SetAxisInIoPrm(cardConnectNo, _useAxis, 5, 1, 0, 0);//伺服报警
                    RTMC.MC_SetAxisInIoPrm(cardConnectNo, _useAxis, 6, 1, 0, 0);//伺服准备

                    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 timer1_Tick(object sender, EventArgs e)
        {
            #region 通用输入
            const int GetinputIOType = 0; //获取通用输入有三种写法
            switch (GetinputIOType)
            {
                case 0:
                    for (uint inputIndex = 0; inputIndex < 12; inputIndex++)
                    {
                        byte inputState_byte = 0;
                        RTMC.MC_GetInIoBit(cardConnectNo, inputIndex, ref inputState_byte);//按位获取通用输入口的状态，一次读一位
                        BitSetPictureBoxState(inputIndex, inputState_byte);
                    }
                    break;
                case 1:
                    uint inputState_uint = 0;
                    RTMC.MC_GetInIoPort(cardConnectNo, 0, ref inputState_uint); //按端口获取通用输入，一次获取32位
                    PortSetPictureBoxState(inputState_uint);
                    break;
                case 2:
                    ulong inputState_ulong = 0;
                    RTMC.MC_GetInIoPtr(cardConnectNo, 0, 12, ref inputState_ulong);//获取通用输入口状态，输入起始索引和需要获取的个数，最多一次获取64bit
                    PtrSetPictureBoxState(0, 12, inputState_ulong);
                    break;
                default:
                    break;
            }
            #endregion

            #region 通用输出
            const int GetOutputIOType = 0;//获取通用输出的状态也有三种写法
            switch (GetOutputIOType)
            {
                case 0:
                    for(uint outputIndex = 0;outputIndex< 12;outputIndex++)
                    {
                        byte outputState_byte = 0;
                        RTMC.MC_GetOutIoBit(cardConnectNo, outputIndex, ref outputState_byte);//按位获取通用输出口的状态，一次读一位
                        BitSetOutputState(outputIndex, outputState_byte);
                    }
                    break;
                case 1:
                    uint outputIOState_uint = 0;
                    RTMC.MC_GetOutIoPort(cardConnectNo, 0, ref outputIOState_uint);//按端口获取通用输出，一次获取32位
                    PortSetOutputState(outputIOState_uint);
                    break;
                case 2:
                    ulong outputIOState_ulong = 0;
                    RTMC.MC_GetOutIoPtr(cardConnectNo, 0, 12, ref outputIOState_ulong);//获取通用输出口状态，输入起始索引和需要获取的个数，最多一次获取64bit
                    PtrSetOutputState(0,12,outputIOState_ulong);
                    break;
                default:
                    break;
            }
            #endregion

            #region 轴0专用信号
            uint inIoState = 0;
            RTMC.MC_GetAxisInIoState(cardConnectNo, 0, ref inIoState);
            //负限位
            bool state = false;
            state = (inIoState & (uint)0x1) > 0 ? true : false;
            if (state) pictureBox17.Image = Properties.Resources.red;
            else pictureBox17.Image = Properties.Resources.green;

            //正限位
            state = ((inIoState >> 1) & (uint)0x1) > 0 ? true : false;
            if (state) pictureBox16.Image = Properties.Resources.red;
            else pictureBox16.Image = Properties.Resources.green;

            //原点
            state = ((inIoState >> 2) & (uint)0x1) > 0 ? true : false;
            if (state) pictureBox18.Image = Properties.Resources.red;
            else pictureBox18.Image = Properties.Resources.green;

            //伺服到位
            state = ((inIoState >> 3) & (uint)0x1) > 0 ? true : false;
            if (state) pictureBox21.Image = Properties.Resources.red;
            else pictureBox21.Image = Properties.Resources.green;

            //报警(ALM)
            state = ((inIoState >> 5) & (uint)0x1) > 0 ? true : false;
            if (state) pictureBox15.Image = Properties.Resources.red;
            else pictureBox15.Image = Properties.Resources.green;

            //伺服准备
            state = ((inIoState >> 6) & (uint)0x1) > 0 ? true : false;
            if (state) pictureBox22.Image = Properties.Resources.red;
            else pictureBox22.Image = Properties.Resources.green;

            //软限位 负
            state = ((inIoState >> 9) & (uint)0x1) > 0 ? true : false;
            if (state) pictureBox20.Image = Properties.Resources.red;
            else pictureBox20.Image = Properties.Resources.green;

            //软限位 正
            state = ((inIoState >> 10) & (uint)0x1) > 0 ? true : false;
            if (state) pictureBox19.Image = Properties.Resources.red;
            else pictureBox19.Image = Properties.Resources.green;

            //SERVON
            ushort _servon = 0;
            RTMC.MC_GetAxisOutIo(cardConnectNo, 0, 0, ref _servon);
            if (_servon == 1) pictureBox24.Image = Properties.Resources.red;
            else pictureBox24.Image = Properties.Resources.green;

            //ERC
            ushort _erc = 0;
            RTMC.MC_GetAxisOutIo(cardConnectNo, 0, 1, ref _erc);
            if (_erc == 1) pictureBox23.Image = Properties.Resources.red;
            else pictureBox23.Image = Properties.Resources.green;

            #endregion
        }

        /// <summary>
        /// 按位设置界面通用输入口的状态
        /// </summary>
        /// <param name="inputIndex"></param>
        /// <param name="state"></param>
        private void BitSetPictureBoxState(uint inputIndex,byte state)
        {
            Bitmap useImage = null;
            if(state == 0)
            {
                useImage = IO.Properties.Resources.green;
            }
            else
            {
                useImage = IO.Properties.Resources.red;
            }
            switch (inputIndex)
            {
                case 0:
                    pictureBox3.Image = useImage;
                    break;
                case 1:
                    pictureBox4.Image = useImage;
                    break;
                case 2:
                    pictureBox5.Image = useImage;
                    break;
                case 3:
                    pictureBox8.Image = useImage;
                    break;
                case 4:
                    pictureBox7.Image = useImage;
                    break;
                case 5:
                    pictureBox6.Image = useImage;
                    break;
                case 6:
                    pictureBox14.Image = useImage;
                    break;
                case 7:
                    pictureBox13.Image = useImage;
                    break;
                case 8:
                    pictureBox12.Image = useImage;
                    break;
                case 9:
                    pictureBox11.Image = useImage;
                    break;
                case 10:
                    pictureBox10.Image = useImage;
                    break;
                case 11:
                    pictureBox9.Image = useImage;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 按port设置界面通用输入口状态
        /// </summary>
        /// <param name="inputState"></param>
        private void PortSetPictureBoxState(uint inputState)
        {
            for(int i =0;i<32;i++)
            {
                bool bitValue = (inputState & (0x1 << i)) == (0x1 << i); //获取32位数中的第i位
                switch (i)
                {
                    case 0:
                        if(bitValue) //为1，高电平
                        {
                            pictureBox3.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox3.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 1:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox4.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox4.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 2:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox5.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox5.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 3:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox8.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox8.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 4:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox7.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox7.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 5:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox6.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox6.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 6:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox14.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox14.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 7:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox13.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox13.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 8:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox12.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox12.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 9:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox11.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox11.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 10:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox10.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox10.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 11:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox9.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox9.Image = IO.Properties.Resources.green;
                        }
                        break;
                    default:
                        break;
                }
            }
            
        }

        /// <summary>
        /// 按照输入的起始和个数获取到的输入口状态
        /// </summary>
        private void PtrSetPictureBoxState(int startIndex,int inputNum,ulong inputState)
        {
            for (int i = startIndex; i < inputNum; i++)
            {
                bool bitValue = (inputState & (ulong)(0x1 << i)) == (ulong)(0x1 << i); //获取64位数中的第i位
                switch (i)
                {
                    case 0:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox3.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox3.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 1:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox4.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox4.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 2:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox5.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox5.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 3:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox8.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox8.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 4:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox7.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox7.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 5:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox6.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox6.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 6:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox14.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox14.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 7:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox13.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox13.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 8:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox12.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox12.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 9:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox11.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox11.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 10:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox10.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox10.Image = IO.Properties.Resources.green;
                        }
                        break;
                    case 11:
                        if (bitValue) //为1，高电平
                        {
                            pictureBox9.Image = IO.Properties.Resources.red;
                        }
                        else//为0，低电平
                        {
                            pictureBox9.Image = IO.Properties.Resources.green;
                        }
                        break;
                    default:
                        break;
                }
            }
        }



        /// <summary>
        /// 按位设置界面通用输出的状态显示
        /// </summary>
        /// <param name="ouputIndex"></param>
        /// <param name="state"></param>
        private void BitSetOutputState(uint ouputIndex,byte state)
        {
            Color useColor;
            if (state == 0)
            {
                useColor = Color.Green;
            }
            else
            {
                useColor = Color.Red;
            }
            switch (ouputIndex)
            {
                case 0:
                    button1.BackColor = useColor;
                    break;
                case 1:
                    button2.BackColor = useColor;
                    break;
                case 2:
                    button3.BackColor = useColor;
                    break;
                case 3:
                    button6.BackColor = useColor;
                    break;
                case 4:
                    button5.BackColor = useColor;
                    break;
                case 5:
                    button4.BackColor = useColor;
                    break;
                case 6:
                    button12.BackColor = useColor;
                    break;
                case 7:
                    button11.BackColor = useColor;
                    break;
                case 8:
                    button10.BackColor = useColor;
                    break;
                case 9:
                    button9.BackColor = useColor;
                    break;
                case 10:
                    button8.BackColor = useColor;
                    break;
                case 11:
                    button7.BackColor = useColor;
                    break;
                default:
                    break;
            }
        }


        /// <summary>
        /// 按port设置界面通用输出口状态
        /// </summary>
        /// <param name="outputState"></param>
        private void PortSetOutputState(uint outputState)
        {
            for (int i = 0; i < 32; i++)
            {
                bool bitValue = (outputState & (0x1 << i)) == (0x1 << i); //获取32位数中的第i位
                switch (i)
                {
                    case 0:
                        if (bitValue) //为1，高电平
                        {
                            button1.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button1.BackColor = Color.Green;
                        }
                        break;
                    case 1:
                        if (bitValue) //为1，高电平
                        {
                            button2.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button2.BackColor = Color.Green;
                        }
                        break;
                    case 2:
                        if (bitValue) //为1，高电平
                        {
                            button3.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button3.BackColor = Color.Green;
                        }
                        break;
                    case 3:
                        if (bitValue) //为1，高电平
                        {
                            button6.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button6.BackColor = Color.Green;
                        }
                        break;
                    case 4:
                        if (bitValue) //为1，高电平
                        {
                            button5.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button5.BackColor = Color.Green;
                        }
                        break;
                    case 5:
                        if (bitValue) //为1，高电平
                        {
                            button4.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button4.BackColor = Color.Green;
                        }
                        break;
                    case 6:
                        if (bitValue) //为1，高电平
                        {
                            button12.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button12.BackColor = Color.Green;
                        }
                        break;
                    case 7:
                        if (bitValue) //为1，高电平
                        {
                            button11.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button11.BackColor = Color.Green;
                        }
                        break;
                    case 8:
                        if (bitValue) //为1，高电平
                        {
                            button10.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button10.BackColor = Color.Green;
                        }
                        break;
                    case 9:
                        if (bitValue) //为1，高电平
                        {
                            button9.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button9.BackColor = Color.Green;
                        }
                        break;
                    case 10:
                        if (bitValue) //为1，高电平
                        {
                            button8.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button8.BackColor = Color.Green;
                        }
                        break;
                    case 11:
                        if (bitValue) //为1，高电平
                        {
                            button7.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button7.BackColor = Color.Green;
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        /// <summary>
        /// 按输入的起始和个数获取到的输出口状态
        /// </summary>
        /// <param name="startIndex"></param>
        /// <param name="outputNum"></param>
        /// <param name="outputState"></param>
        private void PtrSetOutputState(int startIndex,int outputNum,ulong outputState)
        {
            for (int i = startIndex; i < outputNum; i++)
            {
                bool bitValue = (outputState & (ulong)(0x1 << i)) == (ulong)(0x1 << i); //获取64位数中的第i位
                switch (i)
                {
                    case 0:
                        if (bitValue) //为1，高电平
                        {
                            button1.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button1.BackColor = Color.Green;
                        }
                        break;
                    case 1:
                        if (bitValue) //为1，高电平
                        {
                            button2.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button2.BackColor = Color.Green;
                        }
                        break;
                    case 2:
                        if (bitValue) //为1，高电平
                        {
                            button3.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button3.BackColor = Color.Green;
                        }
                        break;
                    case 3:
                        if (bitValue) //为1，高电平
                        {
                            button6.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button6.BackColor = Color.Green;
                        }
                        break;
                    case 4:
                        if (bitValue) //为1，高电平
                        {
                            button5.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button5.BackColor = Color.Green;
                        }
                        break;
                    case 5:
                        if (bitValue) //为1，高电平
                        {
                            button4.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button4.BackColor = Color.Green;
                        }
                        break;
                    case 6:
                        if (bitValue) //为1，高电平
                        {
                            button12.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button12.BackColor = Color.Green;
                        }
                        break;
                    case 7:
                        if (bitValue) //为1，高电平
                        {
                            button11.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button11.BackColor = Color.Green;
                        }
                        break;
                    case 8:
                        if (bitValue) //为1，高电平
                        {
                            button10.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button10.BackColor = Color.Green;
                        }
                        break;
                    case 9:
                        if (bitValue) //为1，高电平
                        {
                            button9.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button9.BackColor = Color.Green;
                        }
                        break;
                    case 10:
                        if (bitValue) //为1，高电平
                        {
                            button8.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button8.BackColor = Color.Green;
                        }
                        break;
                    case 11:
                        if (bitValue) //为1，高电平
                        {
                            button7.BackColor = Color.Red;
                        }
                        else//为0，低电平
                        {
                            button7.BackColor = Color.Green;
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        /// <summary>
        /// 通用输出按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Click(object sender, EventArgs e)
        {
            Button useButton = (sender as Button);
            string buttonName = useButton.Name;
            switch (buttonName)
            {
                case "button1":
                    if(useButton.BackColor == Color.Red)
                    {
                        const int SetOutIoType = 0; //设置通用输出口也有三种方式
                        switch (SetOutIoType)
                        {
                            case 0:
                                RTMC.MC_SetOutIoBit(cardConnectNo, 0, 0); //方式1
                                break;
                            case 1:
                                uint ioState = 0;
                                RTMC.MC_GetOutIoPort(cardConnectNo,0,ref ioState);
                                ioState &= 0xFFFFFFFE; //将bit0设置为0
                                RTMC.MC_SetOutIoPort(cardConnectNo, 0, ioState);//方式2
                                break;
                            case 2:
                                RTMC.MC_SetOutIoPtr(cardConnectNo, 0, 0, 0x1);//方式3，IoMask = 0x1,bit0写入ioState = 0，其他位保持不变
                                break;
                            default:
                                break;
                        }
                    }
                    else
                    {
                        RTMC.MC_SetOutIoBit(cardConnectNo, 0, 1);
                    }
                    break;
                case "button2":
                    if (useButton.BackColor == Color.Red)
                    {

                        RTMC.MC_SetOutIoBit(cardConnectNo, 1, 0); 

                    }
                    else
                    {
                        RTMC.MC_SetOutIoBit(cardConnectNo, 1, 1);
                    }
                    break;
                case "button3":
                    if (useButton.BackColor == Color.Red)
                    {

                        RTMC.MC_SetOutIoBit(cardConnectNo, 2, 0);

                    }
                    else
                    {
                        RTMC.MC_SetOutIoBit(cardConnectNo, 2, 1);
                    }
                    break;
                case "button6":
                    if (useButton.BackColor == Color.Red)
                    {

                        RTMC.MC_SetOutIoBit(cardConnectNo, 3, 0);

                    }
                    else
                    {
                        RTMC.MC_SetOutIoBit(cardConnectNo, 3, 1);
                    }
                    break;
                case "button5":
                    if (useButton.BackColor == Color.Red)
                    {

                        RTMC.MC_SetOutIoBit(cardConnectNo, 4, 0);

                    }
                    else
                    {
                        RTMC.MC_SetOutIoBit(cardConnectNo, 4, 1);
                    }
                    break;
                case "button4":
                    if (useButton.BackColor == Color.Red)
                    {

                        RTMC.MC_SetOutIoBit(cardConnectNo, 5, 0);

                    }
                    else
                    {
                        RTMC.MC_SetOutIoBit(cardConnectNo, 5, 1);
                    }
                    break;
                case "button12":
                    if (useButton.BackColor == Color.Red)
                    {

                        RTMC.MC_SetOutIoBit(cardConnectNo, 6, 0);

                    }
                    else
                    {
                        RTMC.MC_SetOutIoBit(cardConnectNo, 6, 1);
                    }
                    break;
                case "button11":
                    if (useButton.BackColor == Color.Red)
                    {

                        RTMC.MC_SetOutIoBit(cardConnectNo, 7, 0);

                    }
                    else
                    {
                        RTMC.MC_SetOutIoBit(cardConnectNo, 7, 1);
                    }
                    break;
                case "button10":
                    if (useButton.BackColor == Color.Red)
                    {

                        RTMC.MC_SetOutIoBit(cardConnectNo, 8, 0);

                    }
                    else
                    {
                        RTMC.MC_SetOutIoBit(cardConnectNo, 8, 1);
                    }
                    break;
                case "button9":
                    if (useButton.BackColor == Color.Red)
                    {

                        RTMC.MC_SetOutIoBit(cardConnectNo, 9, 0);

                    }
                    else
                    {
                        RTMC.MC_SetOutIoBit(cardConnectNo, 9, 1);
                    }
                    break;
                case "button8":
                    if (useButton.BackColor == Color.Red)
                    {

                        RTMC.MC_SetOutIoBit(cardConnectNo, 10, 0);

                    }
                    else
                    {
                        RTMC.MC_SetOutIoBit(cardConnectNo, 10, 1);
                    }
                    break;
                case "button7":
                    if (useButton.BackColor == Color.Red)
                    {

                        RTMC.MC_SetOutIoBit(cardConnectNo, 11, 0);

                    }
                    else
                    {
                        RTMC.MC_SetOutIoBit(cardConnectNo, 11, 1);
                    }
                    break;
                default:
                    break;
            }
        }


        /// <summary>
        /// 设置虚拟IO按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button13_Click(object sender, EventArgs e)
        {
            ushort ioIndex = Convert.ToUInt16(textBox1.Text);
            ushort map_io_type = (ushort)comboBox1.SelectedIndex;
            if (map_io_type == 8) map_io_type = 9;//map_io_type = 9时为通用IO信号
            ushort map_index = Convert.ToUInt16(textBox2.Text);
            RTMC.MC_SetReuseIoMap(cardConnectNo, ioIndex, map_io_type, map_index);
        }

        /// <summary>
        /// 获取虚拟IO口的状态按钮点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button14_Click(object sender, EventArgs e)
        {
            ushort ioIndex = Convert.ToUInt16(textBox1.Text);
            byte ioState = 0;
            RTMC.MC_GetReuseInIoBit(cardConnectNo, ioIndex, ref ioState);
            textBox3.Text = ioState.ToString();
        }

        /// <summary>
        /// 设置IO映射按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button15_Click(object sender, EventArgs e)
        {
            ushort axis_io_type = ((ushort)comboBox2.SelectedIndex);
            ushort map_io_type = ((ushort)comboBox3.SelectedIndex);
            ushort map_index = Convert.ToUInt16(textBox4.Text);

            //默认界面上参数时将轴0的负限位映射到通用输入0;
            RTMC.MC_SetAxisInIoMap(cardConnectNo, 0, axis_io_type, map_io_type, map_index);
        }
    }
}
