﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using CcntCs;

namespace Sampling
{
	/// <summary>
	/// Sampling の概要の説明です。
	/// </summary>
	public class Sampling : System.Windows.Forms.Form
	{
		public System.Windows.Forms.TextBox txtReturn;
		public System.Windows.Forms.TextBox txtError;
		public System.Windows.Forms.TextBox txtStatus;
		public System.Windows.Forms.TextBox txtTransCount;
		public System.Windows.Forms.TextBox txtDeviceName;
		public System.Windows.Forms.Label Label5;
		public System.Windows.Forms.Label Label4;
		public System.Windows.Forms.Label Label3;
		public System.Windows.Forms.Label Label2;
		public System.Windows.Forms.Label Label1;
		public System.Windows.Forms.Button btnExit;
		public System.Windows.Forms.Button btnCntExit;
		public System.Windows.Forms.Button btnStatus;
		public System.Windows.Forms.Button btnCount;
		public System.Windows.Forms.Button btnData;
		public System.Windows.Forms.Button btnStop;
		public System.Windows.Forms.Button btnStart;
		public System.Windows.Forms.Button btnCondition;
		public System.Windows.Forms.Button btnCntMode;
		public System.Windows.Forms.Button btnCntInit;
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.Container components = null;

		const int			DATA_NUM		= 1000;								// バスマスタ用バッファサイズ
		public const int	CHANNEL_NUM		= 4;

		public static short	Id;													// デバイスID
		uint[ , ]			SamplingData	= new uint[DATA_NUM, CHANNEL_NUM];	// バスマスタ用バッファ

		Ccnt				cnt				= new Ccnt();

		GCHandle			gCh;
		IntPtr				pSamplingData;

		public Sampling()
		{
			//
			// Windows フォーム デザイナ サポートに必要です。
			//
			InitializeComponent();

			//
			// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
			//
		}

		/// <summary>
		/// 使用されているリソースに後処理を実行します。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
		/// コード エディタで変更しないでください。
		/// </summary>
		private void InitializeComponent()
		{
			this.btnExit = new System.Windows.Forms.Button();
			this.btnCntExit = new System.Windows.Forms.Button();
			this.btnStatus = new System.Windows.Forms.Button();
			this.btnCount = new System.Windows.Forms.Button();
			this.btnData = new System.Windows.Forms.Button();
			this.btnStop = new System.Windows.Forms.Button();
			this.btnStart = new System.Windows.Forms.Button();
			this.btnCondition = new System.Windows.Forms.Button();
			this.btnCntMode = new System.Windows.Forms.Button();
			this.btnCntInit = new System.Windows.Forms.Button();
			this.txtReturn = new System.Windows.Forms.TextBox();
			this.txtError = new System.Windows.Forms.TextBox();
			this.txtStatus = new System.Windows.Forms.TextBox();
			this.txtTransCount = new System.Windows.Forms.TextBox();
			this.txtDeviceName = new System.Windows.Forms.TextBox();
			this.Label5 = new System.Windows.Forms.Label();
			this.Label4 = new System.Windows.Forms.Label();
			this.Label3 = new System.Windows.Forms.Label();
			this.Label2 = new System.Windows.Forms.Label();
			this.Label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// btnExit
			// 
			this.btnExit.BackColor = System.Drawing.SystemColors.Control;
			this.btnExit.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnExit.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnExit.Location = new System.Drawing.Point(352, 272);
			this.btnExit.Name = "btnExit";
			this.btnExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnExit.Size = new System.Drawing.Size(89, 23);
			this.btnExit.TabIndex = 9;
			this.btnExit.Text = "Exit";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// btnCntExit
			// 
			this.btnCntExit.BackColor = System.Drawing.SystemColors.Control;
			this.btnCntExit.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnCntExit.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnCntExit.Location = new System.Drawing.Point(352, 243);
			this.btnCntExit.Name = "btnCntExit";
			this.btnCntExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnCntExit.Size = new System.Drawing.Size(89, 23);
			this.btnCntExit.TabIndex = 8;
			this.btnCntExit.Text = "CntExit";
			this.btnCntExit.Click += new System.EventHandler(this.btnCntExit_Click);
			// 
			// btnStatus
			// 
			this.btnStatus.BackColor = System.Drawing.SystemColors.Control;
			this.btnStatus.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnStatus.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnStatus.Location = new System.Drawing.Point(352, 214);
			this.btnStatus.Name = "btnStatus";
			this.btnStatus.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnStatus.Size = new System.Drawing.Size(89, 23);
			this.btnStatus.TabIndex = 7;
			this.btnStatus.Text = "転送ステータス";
			this.btnStatus.Click += new System.EventHandler(this.btnStatus_Click);
			// 
			// btnCount
			// 
			this.btnCount.BackColor = System.Drawing.SystemColors.Control;
			this.btnCount.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnCount.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnCount.Location = new System.Drawing.Point(352, 184);
			this.btnCount.Name = "btnCount";
			this.btnCount.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnCount.Size = new System.Drawing.Size(89, 23);
			this.btnCount.TabIndex = 6;
			this.btnCount.Text = "転送カウント";
			this.btnCount.Click += new System.EventHandler(this.btnCount_Click);
			// 
			// btnData
			// 
			this.btnData.BackColor = System.Drawing.SystemColors.Control;
			this.btnData.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnData.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnData.Location = new System.Drawing.Point(352, 155);
			this.btnData.Name = "btnData";
			this.btnData.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnData.Size = new System.Drawing.Size(89, 23);
			this.btnData.TabIndex = 5;
			this.btnData.Text = "取得データ...";
			this.btnData.Click += new System.EventHandler(this.btnData_Click);
			// 
			// btnStop
			// 
			this.btnStop.BackColor = System.Drawing.SystemColors.Control;
			this.btnStop.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnStop.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnStop.Location = new System.Drawing.Point(352, 126);
			this.btnStop.Name = "btnStop";
			this.btnStop.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnStop.Size = new System.Drawing.Size(89, 23);
			this.btnStop.TabIndex = 4;
			this.btnStop.Text = "転送ストップ";
			this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
			// 
			// btnStart
			// 
			this.btnStart.BackColor = System.Drawing.SystemColors.Control;
			this.btnStart.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnStart.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnStart.Location = new System.Drawing.Point(352, 96);
			this.btnStart.Name = "btnStart";
			this.btnStart.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnStart.Size = new System.Drawing.Size(89, 23);
			this.btnStart.TabIndex = 3;
			this.btnStart.Text = "転送スタート";
			this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
			// 
			// btnCondition
			// 
			this.btnCondition.BackColor = System.Drawing.SystemColors.Control;
			this.btnCondition.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnCondition.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnCondition.Location = new System.Drawing.Point(352, 67);
			this.btnCondition.Name = "btnCondition";
			this.btnCondition.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnCondition.Size = new System.Drawing.Size(89, 23);
			this.btnCondition.TabIndex = 2;
			this.btnCondition.Text = "条件設定...";
			this.btnCondition.Click += new System.EventHandler(this.btnCondition_Click);
			// 
			// btnCntMode
			// 
			this.btnCntMode.BackColor = System.Drawing.SystemColors.Control;
			this.btnCntMode.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnCntMode.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnCntMode.Location = new System.Drawing.Point(352, 38);
			this.btnCntMode.Name = "btnCntMode";
			this.btnCntMode.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnCntMode.Size = new System.Drawing.Size(89, 23);
			this.btnCntMode.TabIndex = 1;
			this.btnCntMode.Text = "カウンタモード";
			this.btnCntMode.Click += new System.EventHandler(this.btnCntMode_Click);
			// 
			// btnCntInit
			// 
			this.btnCntInit.BackColor = System.Drawing.SystemColors.Control;
			this.btnCntInit.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnCntInit.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnCntInit.Location = new System.Drawing.Point(352, 8);
			this.btnCntInit.Name = "btnCntInit";
			this.btnCntInit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnCntInit.Size = new System.Drawing.Size(89, 23);
			this.btnCntInit.TabIndex = 0;
			this.btnCntInit.Text = "CntInit";
			this.btnCntInit.Click += new System.EventHandler(this.btnCntInit_Click);
			// 
			// txtReturn
			// 
			this.txtReturn.AcceptsReturn = true;
			this.txtReturn.AutoSize = false;
			this.txtReturn.BackColor = System.Drawing.SystemColors.Window;
			this.txtReturn.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.txtReturn.ForeColor = System.Drawing.SystemColors.WindowText;
			this.txtReturn.Location = new System.Drawing.Point(80, 275);
			this.txtReturn.MaxLength = 0;
			this.txtReturn.Name = "txtReturn";
			this.txtReturn.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtReturn.Size = new System.Drawing.Size(249, 18);
			this.txtReturn.TabIndex = 14;
			this.txtReturn.Text = "";
			// 
			// txtError
			// 
			this.txtError.AcceptsReturn = true;
			this.txtError.AutoSize = false;
			this.txtError.BackColor = System.Drawing.SystemColors.Window;
			this.txtError.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.txtError.ForeColor = System.Drawing.SystemColors.WindowText;
			this.txtError.Location = new System.Drawing.Point(80, 250);
			this.txtError.MaxLength = 0;
			this.txtError.Name = "txtError";
			this.txtError.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtError.Size = new System.Drawing.Size(249, 18);
			this.txtError.TabIndex = 13;
			this.txtError.Text = "";
			// 
			// txtStatus
			// 
			this.txtStatus.AcceptsReturn = true;
			this.txtStatus.AutoSize = false;
			this.txtStatus.BackColor = System.Drawing.SystemColors.Window;
			this.txtStatus.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.txtStatus.ForeColor = System.Drawing.SystemColors.WindowText;
			this.txtStatus.Location = new System.Drawing.Point(80, 226);
			this.txtStatus.MaxLength = 0;
			this.txtStatus.Name = "txtStatus";
			this.txtStatus.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtStatus.Size = new System.Drawing.Size(249, 18);
			this.txtStatus.TabIndex = 12;
			this.txtStatus.Text = "";
			// 
			// txtTransCount
			// 
			this.txtTransCount.AcceptsReturn = true;
			this.txtTransCount.AutoSize = false;
			this.txtTransCount.BackColor = System.Drawing.SystemColors.Window;
			this.txtTransCount.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.txtTransCount.ForeColor = System.Drawing.SystemColors.WindowText;
			this.txtTransCount.Location = new System.Drawing.Point(80, 202);
			this.txtTransCount.MaxLength = 0;
			this.txtTransCount.Name = "txtTransCount";
			this.txtTransCount.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtTransCount.Size = new System.Drawing.Size(249, 18);
			this.txtTransCount.TabIndex = 11;
			this.txtTransCount.Text = "";
			// 
			// txtDeviceName
			// 
			this.txtDeviceName.AcceptsReturn = true;
			this.txtDeviceName.AutoSize = false;
			this.txtDeviceName.BackColor = System.Drawing.SystemColors.Window;
			this.txtDeviceName.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.txtDeviceName.ForeColor = System.Drawing.SystemColors.WindowText;
			this.txtDeviceName.Location = new System.Drawing.Point(80, 11);
			this.txtDeviceName.MaxLength = 0;
			this.txtDeviceName.Name = "txtDeviceName";
			this.txtDeviceName.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtDeviceName.Size = new System.Drawing.Size(121, 18);
			this.txtDeviceName.TabIndex = 10;
			this.txtDeviceName.Text = "";
			// 
			// Label5
			// 
			this.Label5.AutoSize = true;
			this.Label5.BackColor = System.Drawing.SystemColors.Control;
			this.Label5.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label5.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label5.Location = new System.Drawing.Point(8, 278);
			this.Label5.Name = "Label5";
			this.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label5.Size = new System.Drawing.Size(43, 12);
			this.Label5.TabIndex = 24;
			this.Label5.Text = "戻り値：";
			// 
			// Label4
			// 
			this.Label4.AutoSize = true;
			this.Label4.BackColor = System.Drawing.SystemColors.Control;
			this.Label4.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label4.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label4.Location = new System.Drawing.Point(8, 253);
			this.Label4.Name = "Label4";
			this.Label4.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label4.Size = new System.Drawing.Size(39, 12);
			this.Label4.TabIndex = 23;
			this.Label4.Text = "エラー：";
			// 
			// Label3
			// 
			this.Label3.AutoSize = true;
			this.Label3.BackColor = System.Drawing.SystemColors.Control;
			this.Label3.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label3.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label3.Location = new System.Drawing.Point(8, 229);
			this.Label3.Name = "Label3";
			this.Label3.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label3.Size = new System.Drawing.Size(58, 12);
			this.Label3.TabIndex = 22;
			this.Label3.Text = "ステータス：";
			// 
			// Label2
			// 
			this.Label2.AutoSize = true;
			this.Label2.BackColor = System.Drawing.SystemColors.Control;
			this.Label2.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label2.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label2.Location = new System.Drawing.Point(8, 205);
			this.Label2.Name = "Label2";
			this.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label2.Size = new System.Drawing.Size(71, 12);
			this.Label2.TabIndex = 21;
			this.Label2.Text = "転送カウント：";
			// 
			// Label1
			// 
			this.Label1.AutoSize = true;
			this.Label1.BackColor = System.Drawing.SystemColors.Control;
			this.Label1.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label1.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label1.Location = new System.Drawing.Point(8, 14);
			this.Label1.Name = "Label1";
			this.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label1.Size = new System.Drawing.Size(62, 12);
			this.Label1.TabIndex = 20;
			this.Label1.Text = "デバイス名：";
			// 
			// Sampling
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(449, 302);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnExit,
																		  this.btnCntExit,
																		  this.btnStatus,
																		  this.btnCount,
																		  this.btnData,
																		  this.btnStop,
																		  this.btnStart,
																		  this.btnCondition,
																		  this.btnCntMode,
																		  this.btnCntInit,
																		  this.txtReturn,
																		  this.txtError,
																		  this.txtStatus,
																		  this.txtTransCount,
																		  this.txtDeviceName,
																		  this.Label5,
																		  this.Label4,
																		  this.Label3,
																		  this.Label2,
																		  this.Label1});
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Sampling";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "サンプリング";
			this.Load += new System.EventHandler(this.Sampling_Load);
			this.Closed += new System.EventHandler(this.Sampling_Closed);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// アプリケーションのメイン エントリ ポイントです。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Sampling());
		}

		//===================================================
		//	ダイアログ初期化
		//===================================================
		private void Sampling_Load(object sender, System.EventArgs e)
		{
			//-----------------------------------
			//	画面表示の初期化
			//-----------------------------------
			txtDeviceName.Text = "CNT000";

			//-----------------------------------
			// PinnedハンドルをSamplingDataに分配する
			//-----------------------------------
			if (gCh.IsAllocated == false)
			{
				gCh = GCHandle.Alloc(SamplingData, GCHandleType.Pinned);
			}
		}

		//===================================================
		//	終了時処理
		//===================================================
		private void Sampling_Closed(object sender, System.EventArgs e)
		{
			int	ret;

			//-----------------------------------
			// GCHandleの釈放
			//-----------------------------------
			if (gCh.IsAllocated == true)
			{
				gCh.Free();
			}
			
			//-----------------------------------
			//	終了処理
			//-----------------------------------
			ret	= cnt.Exit(Id);
		}

		//===================================================
		//	CntInit()
		//===================================================
		private void btnCntInit_Click(object sender, System.EventArgs e)
		{
			string	str;
			string	buf = new string(' ', 100);
			string	error_string = new string(' ', 256);
			int		ret;
	
			//-----------------------------------
			//	デバイス名取得
			//-----------------------------------
			buf = txtDeviceName.Text;
			//-----------------------------------
			//	初期化処理
			//-----------------------------------
			ret	= cnt.Init(buf, out Id);
			cnt.GetErrorString(ret, out error_string);
			str = string.Format("CntInit ret = {0:d}: {1}", ret, error_string);
			txtReturn.Text = str;
		}

		//---------------------------------------------
		// カウンタモード設定画面表示
		//---------------------------------------------
		private void btnCntMode_Click(object sender, System.EventArgs e)
		{
			//------------------------------
			// カウンタモード設定画面表示
			//------------------------------
			ModeDlg dlg = new ModeDlg();
			dlg.ShowDialog();
			dlg.Dispose();
		}

		//===================================================
		//	条件設定
		//===================================================
		private void btnCondition_Click(object sender, System.EventArgs e)
		{
			ConditionDlg	dlg = new ConditionDlg();
			//-----------------------------------
			//	条件設定ダイアログを開く
			//-----------------------------------
			dlg.ShowDialog();
			dlg.Dispose();
		}

		//===================================================
		//	転送スタート
		//===================================================
		private void btnStart_Click(object sender, System.EventArgs e)
		{
			short[]	ChNo = new short[CHANNEL_NUM];
			short	i;
			string	error_string = new string(' ', 256);
			string	str;
			int		ret;

			//-----------------------------------
			// メッセージ通知設定
			//-----------------------------------
			ret = cnt.NotifySamplingStop(Id, this.Handle);
			if (ret != (int)CcntConst.CNT_ERR_SUCCESS) 
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntNotifySamplingStop ret = {0:d}: {1}", ret, error_string);
				txtReturn.Text = str;
				return;
			}
			ret = cnt.NotifySamplingCount(Id, (int)ConditionDlg.NumberOfData, this.Handle);
			if (ret != (int)CcntConst.CNT_ERR_SUCCESS) 
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntNotifySamplingCount ret = {0:d}: {1}", ret, error_string);
				txtReturn.Text = str;
				return;
			}
			//-----------------------------------
			//	FIFOのリセット(入力)
			//-----------------------------------
			ret	= cnt.SamplingFifoReset(Id);
			if(ret != (int)CcntConst.CNT_ERR_SUCCESS)
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntSamplingFifoReset ret = {0:d}: {1}", ret, error_string);
				txtReturn.Text = str;
				return;
			}
			//-----------------------------------
			//	データバッファを設定
			//-----------------------------------
			Array.Clear(SamplingData, 0, SamplingData.Length);
			//-----------------------------------
			// Pinnedハンドルでオブジェクトのアドレスを取得
			//-----------------------------------
			pSamplingData = gCh.AddrOfPinnedObject();
			ret = cnt.SamplingSetBuffer(Id, pSamplingData, CHANNEL_NUM, DATA_NUM, (short)CcntConst.CNTS_WRITE_ONCE);
			if(ret != (int)CcntConst.CNT_ERR_SUCCESS)
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntSamplingSetBuffer ret = {0:d}: {1}", ret, error_string);
				txtReturn.Text = str;
				return;
			}
			//------------------------------
			// パラメータのセット
			//------------------------------
			for (i=0; i<CHANNEL_NUM; i++) 
			{
				ChNo[i] = i;
			}
			//------------------------------
			// カウント開始処理
			//------------------------------
			ret = cnt.StartCount(Id, ChNo, CHANNEL_NUM);
			if(ret != (int)CcntConst.CNT_ERR_SUCCESS)
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntStartCount ret = {0:d}: {1}", ret, error_string);
				txtReturn.Text = str;
				return;
			}
			//-----------------------------------
			//	転送スタート
			//-----------------------------------
			ret = cnt.SamplingStart(Id);
			if(ret != (int)CcntConst.CNT_ERR_SUCCESS)
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntSamplingStart ret = {0:d}: {1}", ret, error_string);
				txtReturn.Text = str;
				return;
			}
			txtTransCount.Text = "";
			txtStatus.Text = "";
			txtError.Text = "";
		}

		//===================================================
		//	転送ストップ
		//===================================================
		private void btnStop_Click(object sender, System.EventArgs e)
		{
			string	error_string = new string(' ', 256);
			string	str;
			int		ret;

			//-----------------------------------
			//	転送ストップ
			//-----------------------------------
			ret = cnt.SamplingStop(Id);
			cnt.GetErrorString(ret, out error_string);
			str = string.Format("CntSamplingStop ret = {0:d}: {1}", ret, error_string);
			txtReturn.Text = str;
		}

		//===================================================
		//	データファイル表示
		//===================================================
		private void btnData_Click(object sender, System.EventArgs e)
		{
			string		buf = new string(' ', 256);
			short		i, j;
			TextWriter	fp;
			string		file_name = "SamplingData.csv";

			//-----------------------------------
			// ファイルに転送されたデータを書き込みます
			//-----------------------------------
			//-----------------------------------
			// ファイルを開く
			//-----------------------------------
			try
			{
				fp = File.CreateText(file_name);
			}
			catch (System.IO.IOException)
			{
				txtReturn.Text = "fopen : file open error.";
				return;
			}
			//-----------------------------------
			// データ書き込み
			//-----------------------------------
			for (i = 0; i < DATA_NUM; i++)
			{
				buf = string.Format("{0:d8}: , ", i);
				for( j = 0; j < CHANNEL_NUM - 1; j++ )
				{
					buf = string.Format("{0}{1:X8}, ", buf, SamplingData[i, j]);
				}
				buf = string.Format("{0}{1:X8}", buf, SamplingData[i, j]);
				fp.WriteLine(buf); 
			}
			//-----------------------------------
			// ファイルを閉じる
			//-----------------------------------
			fp.Close();
	
			//-----------------------------------
			// notepad
			//-----------------------------------
			Process Pro = Process.Start("notepad.exe", file_name);

		}

		//===================================================
		//	転送カウント
		//===================================================
		private void btnCount_Click(object sender, System.EventArgs e)
		{
			uint	count = 0, carry = 0;
			string	error_string = new string(' ', 256);
			string	str;
			int		ret;

			//-----------------------------------
			// 転送カウント取得
			//-----------------------------------
			ret = cnt.GetSamplingCount(Id, out count, out carry);
			cnt.GetErrorString(ret, out error_string);
			str = string.Format("CntGetSamplingCount ret = {0:d}: {1}", ret, error_string);
			txtReturn.Text = str;
			if( ret == (int)CcntConst.CNT_ERR_SUCCESS)
			{
				txtTransCount.Text = count.ToString();
			}
		}

		//===================================================
		//	転送ステータス
		//===================================================
		private void btnStatus_Click(object sender, System.EventArgs e)
		{
			uint	status, err;
			string	error_string = new string(' ', 256);
			string	str;
			int		ret;

			//-----------------------------------
			// 転送ステータス取得
			//-----------------------------------
			ret = cnt.GetSamplingStatus(Id, out status, out err);
			cnt.GetErrorString(ret, out error_string);
			str = string.Format("CntGetSamplingStatus ret = {0:d}: {1}", ret, error_string);
			txtReturn.Text = str;
			if(ret != (int)CcntConst.CNT_ERR_SUCCESS)
			{
				return;
			}
	
			// Status
			str = "";
			if ((status & (uint)CcntConst.CNTS_STATUS_BMSTOP) != 0)		str += "BmStop,";
			if ((status & (uint)CcntConst.CNTS_STATUS_CNTSTART) != 0)	str += "CntStart,";
			if ((status & (uint)CcntConst.CNTS_STATUS_CNTSTOP) != 0)	str += "CntStop,";
			if ((status & (uint)CcntConst.CNTS_STATUS_TRGIN) != 0)		str += "TrgIn,";
			if ((status & (uint)CcntConst.CNTS_STATUS_OVERRUN) != 0)	str += "Overrun";
			txtStatus.Text = str;
			// Error
			str = "";
			if ((err & (uint)CcntConst.CNTS_STATUS_FIFOFULL) != 0)		str += "FifoFull,";
			if ((err & (uint)CcntConst.CNTS_STATUS_SGOVERIN) != 0)		str += "S/GOverIn,";
			if ((err & (uint)CcntConst.CNTS_STATUS_TRGERR) != 0)		str += "TrgErr,";
			if ((err & (uint)CcntConst.CNTS_STATUS_CLKERR) != 0)		str += "ClkErr,";
			if ((err & (uint)CcntConst.CNTS_STATUS_SLAVEHALT) != 0)		str += "SlaveHalt,";
			if ((err & (uint)CcntConst.CNTS_STATUS_MASTERHALT) != 0)	str += "MasterHalt";

			txtError.Text = str;
		}

		//===================================================
		//	CntExit
		//===================================================
		private void btnCntExit_Click(object sender, System.EventArgs e)
		{
			string	error_string = new string(' ', 256);
			string	str;
			int		ret;
	
			//-----------------------------------
			// CntExit
			//-----------------------------------
			ret = cnt.Exit(Id);
			cnt.GetErrorString(ret, out error_string);
			str = string.Format("CntExit ret = {0:d}: {1}", ret, error_string);
			txtReturn.Text = str;
		}

		//===================================================
		//	Exit
		//===================================================
		private void btnExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	
		//===================================================
		// カウンタ イベント通知処理
		//===================================================
		protected override void WndProc(ref Message m)
		{
			switch( m.Msg ) 
			{
				case (int)CcntConst.CNTM_SAMPLING_COUNT:	// 指定個数転送完了
					btnCount_Click(btnCount, new System.EventArgs());
					break;
				case (int)CcntConst.CNTM_SAMPLING_STOP:		// 転送完了
					txtStatus.Text = "Stopped!";
					btnCount_Click(btnCount, new System.EventArgs());
					break;
			}

			base.WndProc(ref m);
		}
	}
}
