﻿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 CaioCs;

namespace DigitalIO
{
    public partial class Form1 : Form
    {
        //----------------------------------------
        // クラスCaioインスタンス作成
        //----------------------------------------
        Caio aio = new Caio();

        //----------------------------------------
        // グローバル変数の宣言
        //----------------------------------------
        public short g_id;    // デバイスアクセス用ID

        public Form1()
        {
            InitializeComponent();
        }

        private void DigitalIO_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // デフォルトボタンを設定
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // デバイス名の初期表示を更新
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // デジタルI/Oポートの入出力方向の初期表示を更新
            //----------------------------------------
            textBox_IoDirection.Text = "0";
            //----------------------------------------
            // 指定デジタル入力ポート番号の初期表示を更新
            //----------------------------------------
            textBox_InputByteNo.Text = "0";
            //----------------------------------------
            // 指定デジタル入力ポートデータの初期表示を更新
            //----------------------------------------
            textBox_InputByteData.Text = "00";
            //----------------------------------------
            // 指定デジタル入力ビット番号の初期表示を更新
            //----------------------------------------
            textBox_InputBitNo.Text = "0";
            //----------------------------------------
            // 指定デジタル入力ビットデータの初期表示を更新
            //----------------------------------------
            textBox_InputBitData.Text = "0";
            //----------------------------------------
            // 指定デジタル出力ポート番号の初期表示を更新
            //----------------------------------------
            textBox_OutputByteNo.Text = "0";
            //----------------------------------------
            // 指定デジタル出力ポートデータの初期表示を更新
            //----------------------------------------
            textBox_OutputByteData.Text = "00";
            //----------------------------------------
            // 指定デジタル出力ビット番号の初期表示を更新
            //----------------------------------------
            textBox_OutputBitNo.Text = "0";
            //----------------------------------------
            // 指定デジタル出力ビットデータの初期表示を更新
            //----------------------------------------
            textBox_OutputBitData.Text = "0";
            //----------------------------------------
            // グローバル変数の初期化
            //----------------------------------------
            g_id = 0;
        }

        //================================================================================
        // AioInitボタンをクリックした際の処理になります
        // デバイスの初期化処理を行います
        //================================================================================
        private void button_AioInit_Click(object sender, EventArgs e)
        {
            string device_name;     // デバイス名
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // デバイス名をテキストボックスから取得
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // デバイス初期化
            // 変更注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.Init(device_name, out g_id);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.Init = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // AioResetDeviceボタンをクリックした際の処理になります
        // デバイスのリセット、ドライバの初期化を行います
        //================================================================================
        private void button_AioResetDevice_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // デバイスのリセット、ドライバの初期化
            //----------------------------------------
            ret = aio.ResetDevice(g_id);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.ResetDevice = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // AioSetDioDirectionボタンをクリックした際の処理になります
        // デジタルI/Oポートの入出力方向を設定します
        //================================================================================
        private void button_SetDioDirection_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用
            int    io_direction;    // 入出力方向

            //----------------------------------------
            // テキストボックスから入出力方向を取得
            //----------------------------------------
            if (int.TryParse(textBox_IoDirection.Text, out io_direction) == false)
            {
                io_direction = 0;
            }
            //----------------------------------------
            // デジタルI/Oポートの入出力方向を設定
            //----------------------------------------
            ret = aio.SetDioDirection(g_id, io_direction);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetDioDirection = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // AioInputDiByteボタンをクリックした際の処理になります
        // 指定デジタル入力ポートからバイト単位でデータを入力します
        //================================================================================
        private void button_InputDiByte_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            string text_string;     // テキストボックス更新用
            int    ret;             // 戻り値取得用
            short  di_port;         // デジタル入力ポート番号
            short  di_data;         // 入力データ

            //----------------------------------------
            // テキストボックスからポート番号を取得
            //----------------------------------------
            if (short.TryParse(textBox_InputByteNo.Text, out di_port) == false)
            {
                di_port = 0;
            }
            //----------------------------------------
            // 指定ポートデータを取得
            //----------------------------------------
            ret = aio.InputDiByte(g_id, di_port, out di_data);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // エラー文字列取得
            //--------------------
            aio.GetErrorString(ret, out error_string);
            //--------------------
            // メッセージ表示
            //--------------------
            textBox_ReturnCode.Text = "aio.InputDiByte = " + ret.ToString() + "：" + error_string;
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                return;
            }
            //----------------------------------------
            // 指定ポートデータを表示
            //----------------------------------------
            text_string = string.Format("{0:x2}", di_data);
            //--------------------
            // 変換データ表示を更新
            //--------------------
            textBox_InputByteData.Text = text_string;
        }

        //================================================================================
        // AioInputDiBitボタンをクリックした際の処理になります
        // 指定デジタル入力ビットからデータを入力します
        //================================================================================
        private void button_InputDiBit_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            string text_string;     // テキストボックス更新用
            int    ret;             // 戻り値取得用
            short  di_bit;          // デジタル入力ビット番号
            short  di_data;         // 入力データ

            //----------------------------------------
            // テキストボックスからビット番号を取得
            //----------------------------------------
            if (short.TryParse(textBox_InputBitNo.Text, out di_bit) == false)
            {
                di_bit = 0;
            }
            //----------------------------------------
            // 指定ビットデータを取得
            //----------------------------------------
            ret = aio.InputDiBit(g_id, di_bit, out di_data);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // エラー文字列取得
            //--------------------
            aio.GetErrorString(ret, out error_string);
            //--------------------
            // メッセージ表示
            //--------------------
            textBox_ReturnCode.Text = "aio.InputDiBit = " + ret.ToString() + "：" + error_string;
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                return;
            }
            //----------------------------------------
            // 指定ビットデータを表示
            //----------------------------------------
            text_string = string.Format("{0:d}", di_data);
            //--------------------
            // 変換データ表示を更新
            //--------------------
            textBox_InputBitData.Text = text_string;
        }

        //================================================================================
        // AioOutputDoByteボタンをクリックした際の処理になります
        // 指定デジタル出力バイトにデータを出力します
        //================================================================================
        private void button_OutputDoByte_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用
            short  do_port;         // デジタル出力ポート番号
            short  do_data;         // 出力データ

            //----------------------------------------
            // テキストボックスからポート番号を取得
            //----------------------------------------
            if (short.TryParse(textBox_OutputByteNo.Text, out do_port) == false)
            {
                do_port = 0;
            }
            //----------------------------------------
            // テキストボックスから出力データを取得
            //----------------------------------------
            try
            {
                do_data = Convert.ToInt16(textBox_OutputByteData.Text, 16);
            }
            catch
            {
                do_data = 0;
            }
            //----------------------------------------
            // 指定デジタル出力バイトにデータを出力
            //----------------------------------------
            ret = aio.OutputDoByte(g_id, do_port, do_data);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.OutputDoByte = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // AioOutputDoBitボタンをクリックした際の処理になります
        // 指定デジタル出力ビットにデータを出力します
        //================================================================================
        private void button_OutputDoBit_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用
            short  do_bit;          // デジタル出力ビット番号
            short  do_data;         // 出力データ

            //----------------------------------------
            // テキストボックスからビット番号を取得
            //----------------------------------------
            if (short.TryParse(textBox_OutputBitNo.Text, out do_bit) == false)
            {
                do_bit = 0;
            }
            //----------------------------------------
            // テキストボックスから出力データを取得
            //----------------------------------------
            if (short.TryParse(textBox_OutputBitData.Text, out do_data) == false)
            {
                do_data = 0;
            }
            //----------------------------------------
            // 指定デジタル出力ビットにデータを出力
            //----------------------------------------
            ret = aio.OutputDoBit(g_id, do_bit, do_data);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.OutputDoBit = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // AioExitボタンをクリックした際の処理になります
        // デバイスの終了処理を行います
        //================================================================================
        private void button_AioExit_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // 終了処理の実行
            //----------------------------------------
            ret = aio.Exit(g_id);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.Exit = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // 閉じるボタンをクリックした際の処理になります
        // デバイスの終了処理を行い、アプリケーションを終了します
        //================================================================================
        private void button_Close_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // 終了処理の実行
            //----------------------------------------
            aio.Exit(g_id);
            //----------------------------------------
            // ダイアログを終了する
            //----------------------------------------
            this.Close();
        }
    }
}
