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;
using GC.Frame.Motion.Privt;

namespace ExIOmodule
{
    public partial class Form1 : Form
    {
        Button[] btnin = new Button[32];
        Button[] btnout = new Button[32];
        bool conn = false;
        public Form1()
        {
            InitializeComponent();
            for(int i=0;i<32;i++)
            {
                if(i<16)
                {
                    btnin[i] = new Button();
                    btnin[i].Name = "btnI" + i;
                    btnin[i].Size = new Size(26, 26);
                    btnin[i].Location = new Point(20 + i * 30, 114);
                    btnin[i].Enabled = false;
                    btnin[i].BackColor = Color.Transparent;
                    this.Controls.Add(btnin[i]);
                }
                else
                {
                    btnin[i] = new Button();
                    btnin[i].Name = "btnI" + i;
                    btnin[i].Size = new Size(26, 26);
                    btnin[i].Location = new Point(20 + (i-16) * 30, 146);
                    btnin[i].Enabled = false;
                    btnin[i].BackColor = Color.Transparent;
                    this.Controls.Add(btnin[i]);
                }                
            }
            for (int i = 0; i < 32; i++)
            {
                if (i < 16)
                {
                    btnout[i] = new Button();
                    btnout[i].Name = "btnO" + i;
                    btnout[i].Size = new Size(26, 26);
                    btnout[i].Location = new Point(20 + i * 30, 244);
                    btnout[i].Enabled = true;
                    btnout[i].BackColor = Color.Transparent;
                    this.Controls.Add(btnout[i]);
                }
                else
                {
                    btnout[i] = new Button();
                    btnout[i].Name = "btnO" + i;
                    btnout[i].Size = new Size(26, 26);
                    btnout[i].Location = new Point(20 + (i - 16) * 30, 276);
                    btnout[i].Enabled = true;
                    btnout[i].BackColor = Color.Transparent;
                    this.Controls.Add(btnout[i]);
                }
                btnout[i].Click += new EventHandler(btnout_Click);
            }
        }
        private void btnout_Click(object sender, EventArgs e)
        {
            if(conn==false)
            {
                MessageBox.Show("请先初始化");
                return;
            }
            short rtn = 0;
            Button b = (Button)sender;
            if(b.BackColor==Color.Green)
            {
                //置为高电平
                rtn = CNMCLib20.NMC_SetDOBit(devhandle,(short)(64 + Convert.ToInt32(b.Name.Remove(0, 4))), 1);
            }
            else
            {
                //置为低电平
                rtn = CNMCLib20.NMC_SetDOBit(devhandle, (short)(64 + Convert.ToInt32(b.Name.Remove(0, 4))), 0);
            }
                       
        }
        ushort devhandle = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            short rtn = 0;
            rtn = CNMCLib20.NMC_DevOpenByID("CARD1", ref devhandle);
            if(rtn!=0)
            {
                MessageBox.Show("控制器打开错误");
                return;
            }
            //硬件表示0，代表模块号2
            rtn = CNMCLib20.NMC_SetIOModuleEn(devhandle, 2);
            if (rtn != 0)
            {
                MessageBox.Show("拓展模块打开错误");
                return;
            }
            conn = true;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //读取通讯模块1的状态
            short rtn = 0;
            uint sts = 0;
            int io = 0;
            int iou = 0;
            rtn = CNMCLib20.NMC_GetIOModuleSts(devhandle, ref sts);
            if((sts&(1<<2))==0)
            {
                button34.BackColor = Color.Transparent;
                timer1.Stop();
            }
            else
            {
                button34.BackColor = Color.Green;
            }
            //获取输入状态,拓展模块从2组开始
            rtn = CNMCLib20.NMC_GetDI(devhandle, 2, ref io);
            for(int k=0;k<32;k++)
            {
                if ((io & (1 << k)) == 0)
                {
                    //第K位为低电平
                    btnin[k].BackColor = Color.Green;
                }
                else
                {
                    //第k位为高电平
                    btnin[k].BackColor = Color.Transparent;
                }
            }
            //获取输出状态
            rtn = CNMCLib20.NMC_GetDO(devhandle, 2, ref iou);
            for (int k = 0; k < 32; k++)
            {
                if ((iou & (1 << k)) == 0)
                {
                    //第K位为低电平
                    btnout[k].BackColor = Color.Green;
                }
                else
                {
                    //第k位为高电平
                    btnout[k].BackColor = Color.Transparent;
                }
            }
        }
    }
}
