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 static sc_io.scio32;
using HAND = System.UInt16;

namespace IO_DEMO
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        HAND[] devHandle = new HAND[4];                   // 模块句柄
        short rtn = 0;                          // 指令返回值
        private void button1_Click(object sender, EventArgs e)
        {
            short i;
            ushort dev_no = 0;

            TDevInfo[] dev_info_list = new TDevInfo[32];

            rtn = NIO_Search(ref dev_no, dev_info_list);
            if (rtn != RTN_CMD_SUCCESS) { MessageBox.Show("NIO_Search :" + rtn); }

            rtn = NIO_OpenByID(dev_info_list[0].idStr, ref devHandle[0]);
            rtn = NIO_OpenByID(dev_info_list[1].idStr, ref devHandle[1]);
            if (rtn != RTN_CMD_SUCCESS) { MessageBox.Show("NIO_OpenByID :" + rtn); } else
            {
                MessageBox.Show("打开模块成功！"); 
            }

           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ushort portStart = 0;
            ushort portCount = 1;                       // 前16位输出
            //ushort portCount = 2;                     // 后16位输出 
            ushort value = 0xFFF3;                      // 同时打开 0 - 3 输出
           // ushort value = 0xFF00;                // 同时打开 0 - 7 输出
            rtn = NIO_SetDO(devHandle[0], portStart, portCount, ref value);
            rtn = NIO_SetDO(devHandle[1], portStart, portCount, ref value);
            if (rtn != RTN_CMD_SUCCESS) { MessageBox.Show("NIO_SetDO ON:" + rtn); }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ushort portStart = 0;
            ushort portCount = 1;                       // 前16位输出
            //ushort portCount = 2;                     // 后16位输出 
            ushort value = 0xFFFF;                      // 同时关闭 0 - 15 输出
            rtn = NIO_SetDO(devHandle[0], portStart, portCount, ref value);
            if (rtn != RTN_CMD_SUCCESS) { MessageBox.Show("NIO_SetDO OFF:" + rtn); }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            rtn = NIO_SetDOBit(devHandle[0], 0,0 );
            rtn = NIO_SetDOBit(devHandle[0], 15, 0);
            rtn = NIO_SetDOBit(devHandle[0], 16, 0);
            rtn = NIO_SetDOBit(devHandle[0], 31, 0);
            if (rtn != RTN_CMD_SUCCESS) { MessageBox.Show("NIO_SetDOBit ON:" + rtn); }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            rtn = NIO_SetDOBit(devHandle[0], 0, 1);
            if (rtn != RTN_CMD_SUCCESS) { MessageBox.Show("NIO_SetDOBit OFF:" + rtn); }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            ushort val = 1;
            rtn = NIO_GetDOBit(devHandle[0], 0, ref val);
            if (rtn != RTN_CMD_SUCCESS) { MessageBox.Show("NIO_GetDOBit:" + rtn); }
            MessageBox.Show(" DO0 = ： "+ val);
        }
        // 获取DI0
        private void button7_Click(object sender, EventArgs e)
        {
            ushort val = 0;
            rtn = NIO_GetDI(devHandle[0], 0, 1,ref val);
            if (rtn != RTN_CMD_SUCCESS) { MessageBox.Show("1 NIO_GetDI:" + rtn); }
            rtn = NIO_GetDI(devHandle[0], 0, 2, ref val);
            if (rtn != RTN_CMD_SUCCESS) { MessageBox.Show("2 NIO_GetDI:" + rtn); }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            ushort val = 32765;
            rtn = NIO_SetDO(devHandle[0], 0, 1, ref val);
            if (rtn != RTN_CMD_SUCCESS) { MessageBox.Show("1 NIO_SetDO:" + rtn); }
        }
    }
}
