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

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

		//================================================================================
		// DncDaq設定表示ボタンクリックイベント
		//================================================================================
		private void button_DaqProperty_Click(object sender, EventArgs e)
		{
			//----------------------------------------
			// DncDaq設定画面を表示
			//----------------------------------------
			dncDaq1.ShowProperty();
		}

		//================================================================================
		// Exitボタンクリックイベント
		//================================================================================
		private void button_Exit_Click(object sender, EventArgs e)
		{
			//----------------------------------------
			// ダイアログを終了します
			//----------------------------------------
			this.Close();
		}

		//================================================================================
		// DncDaqInitイベント
		//================================================================================
		private void dncDaq1_OnInit(object sender, EventArgs e)
		{
			//----------------------------------------
			// エラー詳細をクリアします
			//----------------------------------------
			textBox_ErrorDetail.Text = "";
			//----------------------------------------
			// DncDaqにデバイス設定が存在するかのチェック
			//----------------------------------------
			if (dncDaq1.DaqProperty.Device.Count <= 0)
			{
				textBox_ErrorDetail.Text = "デバイス設定がありません。";
				return;
			}
			//----------------------------------------
			// DncDaqに入力タスク設定が存在するかのチェック
			//----------------------------------------
			if (dncDaq1.DaqProperty.InputTask.Count <= 0)
			{
				textBox_ErrorDetail.Text = "入力タスク設定がありません。";
				return;
			}
		}

		//================================================================================
		// DncDaqエラーイベント
		//================================================================================
		private void dncDaq1_OnError(object sender, short DeviceNo, dnc.common.DncErrorEventArgs e)
		{
			int		ret;				// 戻り値取得用変数
			string	error_string;		// エラー文字列

			//----------------------------------------
			// イベント引数内のエラーNo.よりエラー文字列取得
			//----------------------------------------
			ret = dncDaq1.DaqErrorString(DeviceNo, e.Error, out error_string);
			//----------------------------------------
			// エラー文字列取得に失敗した場合はエラー文字列を初期化
			//----------------------------------------
			if (ret != 0)
			{
				error_string = "";
			}
			//----------------------------------------
			// 取得できたエラー文字列およびイベント引数内のステータス情報をラベルに表示します
			// エラーイベントは別スレッドで実行される場合があるためInvokeが必要か確認し、
			// 必要な場合はInvokeでフォームコントロールへアクセスします
			//----------------------------------------
			if (InvokeRequired == true)
			{
				//----------------------------------------
				// エラーパラメータをテキストボックスに表示します
				// Invokeで呼び出すメソッドはラムダ式[=>]で記述しています
				//----------------------------------------
				Invoke(new Action(() => textBox_ErrorDetail.Text =	"デバイスNo.：" + DeviceNo.ToString() + "\r\n" +
																	"エラータイプ：" + e.ErrorType.ToString() + "\r\n" +
																	"エラーコード(値).：" + e.Error.ToString() + "\r\n" +
																	"エラーコード(文字列)：" + error_string + "\r\n" +
																	"エラーパラメータ：" + e.Param.ToString()
																	));
			}
			else
			{
				//----------------------------------------
				// エラーパラメータをテキストボックスに表示します
				//----------------------------------------
				textBox_ErrorDetail.Text =	"デバイスNo.：" + DeviceNo.ToString() + "\r\n" +
											"エラータイプ：" + e.ErrorType.ToString() + "\r\n" +
											"エラーコード(値).：" + e.Error.ToString() + "\r\n" +
											"エラーコード(文字列)：" + error_string + "\r\n" +
											"エラーパラメータ：" + e.Param.ToString();
			}
		}

		//================================================================================
		// AiInputChannelボタンクリックイベント
		//================================================================================
		private void button_InputChannel_Click(object sender, EventArgs e)
		{
			int		ret1;			// 戻り値取得用変数
			int		ret2;			// 戻り値取得用変数2
			float	aidata;			// アナログデータ格納用変数
			short	device_no;      // デバイスNo.格納用変数
			short	channel;        // チャネルデータ格納用変数
			string	error_string;   // エラー文字列

			//------------------------------------
			// テキストボックスよりデバイスNo.と変換チャネルを取得
			//------------------------------------
			try
			{
				device_no	= short.Parse(textBox_DeviceNo.Text);
				channel		= short.Parse(textBox_ChannelNo.Text);
			}
			catch (Exception ex)
			{
				textBox_ErrorDetail.Text = ex.Message;
				return;
			}
			//------------------------------------
			// AiInputChannelメソッドの実行
			//------------------------------------
			ret1 = dncDaq1.AiInputChannel(device_no, channel, out aidata);
			//------------------------------------
			// エラー処理
			//------------------------------------
			if (ret1 != 0)
			{
				//------------------------------------
				// ErrorStringメソッドを失敗した場合はエラー内容をクリア
				//------------------------------------
				ret2 = dncDaq1.DaqErrorString(device_no, ret1, out error_string);
				if (ret2 != 0)
				{
					error_string = "";
				}
				textBox_ErrorDetail.Text = "AiInputChannel= " + ret1.ToString() + " " + error_string;
				return;
			}
			//------------------------------------
			// 取得データをラベルに表示
			//------------------------------------
			textBox_Data.Text = channel.ToString()+"\t"+ aidata.ToString()+"\r\n";
			textBox_ErrorDetail.Text = "AiInputChannel：" + ret1.ToString() + " 正常終了";
		}
	}
}
