﻿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_MultiChannel_Method
{
	public partial class SimpleInput_MultiChannel_Method : Form
	{
		public SimpleInput_MultiChannel_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エラーイベント
		//================================================================================
		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();
			}
		}

		//================================================================================
		// AiInputMultiChannelボタン押下時の処理
		//================================================================================
		private void button_AiInputMultiChannel_Click_1(object sender, EventArgs e)
		{
			int ret1;				// 戻り値取得用変数
			int ret2;				// 戻り値取得用変数2
			float[] aidata;			// アナログデータ格納用配列
			short device_no;		// デバイスNo.格納用変数
			short channels;			// チャネルデータ格納用変数
			string error_string;	// エラー文字列

			//------------------------------------
			// テキストボックスよりデバイスNo.と変換チャネル数を取得
			//------------------------------------
			try
			{
				device_no = short.Parse(textBox_DeviceNo.Text);
				channels = short.Parse(textBox_ChannelNum.Text);
			}
			catch (Exception ex)
			{
				textBox_ErrorDetail.Text = ex.Message;
				return;
			}
			//------------------------------------
			// データ格納用にチャネル数分配列を初期化
			//------------------------------------
			aidata = new float[channels];
			//------------------------------------
			// AiInputMultiChannelメソッドの実行
			//------------------------------------
			ret1 = dncDaq1.AiInputMultiChannel(device_no, channels, aidata);
			//------------------------------------
			// エラー処理
			//------------------------------------
			if (ret1 != 0)
			{
				//------------------------------------
				// ErrorStringメソッドを失敗した場合はエラー内容をクリア
				//------------------------------------
				ret2 = dncDaq1.DaqErrorString(device_no, ret1, out error_string);
				if (ret2 != 0)
				{
					error_string = "";
				}
				textBox_ErrorDetail.Text = "AiInputMultiChannel= " + ret1.ToString() + " " + error_string;
				return;
			}
			//------------------------------------
			// 変換データ表示処理
			//------------------------------------
			short i;
			textBox_Data.Text = "";
			for (i = 0; i < channels; i++)
			{
				textBox_Data.Text += i + "\t" + aidata[i] + "\r\n";
			}
			textBox_ErrorDetail.Text = "AiInputMultiChannel：" + ret1.ToString() + " 正常終了";
		}
	}
}
