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 sc_io;

namespace UsbIO
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private IntPtr devHandle;
        private IntPtr devHandle1;
        private void button1_Click(object sender, EventArgs e)
        {
            //拨码地址不用处理
            //打开模块
            //搜寻扩展模块
            short rtn = 0;
            ushort devNum = 0;
            scio32.TDevInfo devInfo=new scio32.TDevInfo();
           // byte[] devInfo=new byte[4*84];
            // rtn = scio32.NIO_Search(ref devNum, devInfo);
            scio32.NIO_Search(ref devNum, ref devInfo);
           // MessageBox.Show(devInfo.idStr.ToString());
            //打开
            //if(devNum>0)
            //{
           
            rtn = scio32.NIO_OpenByID("CARD1", ref devHandle);
           // rtn = scio32.NIO_OpenByID("CARD2", ref devHandle1);
            if (rtn!=0)
                {
                    //打开失败
                    MessageBox.Show("打开失败");
                    return;
                }
            //}
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //获取输入状态,0代表有输入
            short rtn = 0;
            ushort input=0,output = 0;
            rtn = scio32.NIO_GetDI(devHandle, 0, 1, ref input);
            textBox1.Text = "" + input;

            //获取输出状态，0代表有输出
            rtn = scio32.NIO_GetDOBit(devHandle, 0, ref output);
            label4.Text = "" + output;
            rtn = scio32.NIO_GetDOBit(devHandle, 1, ref output);
            label5.Text = "" + output;
            rtn = scio32.NIO_GetDOBit(devHandle, 2, ref output);
            label6.Text = "" + output;
            rtn = scio32.NIO_GetDOBit(devHandle, 3, ref output);
            label7.Text = "" + output;
        }

        private void button2_Click(object sender, EventArgs e)
        {
          
            short rtn = 0;
            ushort output = 65535;  
            rtn = scio32.NIO_SetDO(devHandle, 0, 1, ref output);
            if (rtn != 0) {
                MessageBox.Show("NIO_SetDO ERR" + rtn);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //按bit输出
            short rtn = 0;//仅输出1
            rtn = scio32.NIO_SetDOBit(devHandle, 0, 0);
            rtn = scio32.NIO_SetDOBit(devHandle, 1, 0);
            rtn = scio32.NIO_SetDOBit(devHandle, 2, 0);
            rtn = scio32.NIO_SetDOBit(devHandle, 3, 0);
            rtn = scio32.NIO_SetDOBit(devHandle, 4, 0);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //按bit输出
            short rtn = 0;//仅输出1
            ushort pv = 0;
            rtn = scio32.NIO_GetIOLogic(devHandle, ref pv);
            label3.Text = "" + pv;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            scio32.NIO_Close(devHandle1);
        }


      

        private void button6_Click_1(object sender, EventArgs e)
        {
            //按bit输出
            short rtn = 0;//仅输出1
            rtn = scio32.NIO_SetDOBit(devHandle, 0, 1);
            rtn = scio32.NIO_SetDOBit(devHandle, 1, 1);
            rtn = scio32.NIO_SetDOBit(devHandle, 2, 1);
            rtn = scio32.NIO_SetDOBit(devHandle, 3, 1);
            rtn = scio32.NIO_SetDOBit(devHandle, 4, 1);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            short rtn = 0;
            ushort output = 0;
            rtn = scio32.NIO_SetDO(devHandle, 0, 1, ref output);
            if (rtn != 0)
            {
                MessageBox.Show("NIO_SetDO ERR" + rtn);
            }
        }
    }
}
