﻿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>
	/// Summary description for Sampling.
	/// </summary>
	public class Sampling : System.Windows.Forms.Form
	{
		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;
		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;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		const int			DATA_NUM		= 1000;								// Buffer size for bus-master
		public const int	CHANNEL_NUM		= 4;

		public static short	Id;													// Device ID
		uint[ , ]			SamplingData	= new uint[DATA_NUM, CHANNEL_NUM];	// Buffer for bus-master

		Ccnt				cnt				= new Ccnt();

		GCHandle			gCh;
		IntPtr				pSamplingData;

		public Sampling()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </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(320, 333);
			this.btnExit.Name = "btnExit";
			this.btnExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnExit.Size = new System.Drawing.Size(94, 25);
			this.btnExit.TabIndex = 74;
			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(320, 297);
			this.btnCntExit.Name = "btnCntExit";
			this.btnCntExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnCntExit.Size = new System.Drawing.Size(94, 25);
			this.btnCntExit.TabIndex = 73;
			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(320, 261);
			this.btnStatus.Name = "btnStatus";
			this.btnStatus.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnStatus.Size = new System.Drawing.Size(94, 25);
			this.btnStatus.TabIndex = 72;
			this.btnStatus.Text = "Status";
			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(320, 225);
			this.btnCount.Name = "btnCount";
			this.btnCount.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnCount.Size = new System.Drawing.Size(94, 25);
			this.btnCount.TabIndex = 71;
			this.btnCount.Text = "Count";
			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(320, 189);
			this.btnData.Name = "btnData";
			this.btnData.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnData.Size = new System.Drawing.Size(94, 25);
			this.btnData.TabIndex = 70;
			this.btnData.Text = "Data...";
			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(320, 153);
			this.btnStop.Name = "btnStop";
			this.btnStop.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnStop.Size = new System.Drawing.Size(94, 25);
			this.btnStop.TabIndex = 69;
			this.btnStop.Text = "Stop";
			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(320, 117);
			this.btnStart.Name = "btnStart";
			this.btnStart.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnStart.Size = new System.Drawing.Size(94, 25);
			this.btnStart.TabIndex = 68;
			this.btnStart.Text = "Start";
			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(320, 81);
			this.btnCondition.Name = "btnCondition";
			this.btnCondition.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnCondition.Size = new System.Drawing.Size(94, 25);
			this.btnCondition.TabIndex = 67;
			this.btnCondition.Text = "Condition...";
			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(320, 45);
			this.btnCntMode.Name = "btnCntMode";
			this.btnCntMode.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnCntMode.Size = new System.Drawing.Size(94, 25);
			this.btnCntMode.TabIndex = 66;
			this.btnCntMode.Text = "Counter Mode";
			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(320, 9);
			this.btnCntInit.Name = "btnCntInit";
			this.btnCntInit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnCntInit.Size = new System.Drawing.Size(94, 25);
			this.btnCntInit.TabIndex = 65;
			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(87, 335);
			this.txtReturn.MaxLength = 0;
			this.txtReturn.Name = "txtReturn";
			this.txtReturn.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtReturn.Size = new System.Drawing.Size(217, 20);
			this.txtReturn.TabIndex = 79;
			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(87, 308);
			this.txtError.MaxLength = 0;
			this.txtError.Name = "txtError";
			this.txtError.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtError.Size = new System.Drawing.Size(217, 20);
			this.txtError.TabIndex = 78;
			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(87, 282);
			this.txtStatus.MaxLength = 0;
			this.txtStatus.Name = "txtStatus";
			this.txtStatus.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtStatus.Size = new System.Drawing.Size(217, 20);
			this.txtStatus.TabIndex = 77;
			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(87, 256);
			this.txtTransCount.MaxLength = 0;
			this.txtTransCount.Name = "txtTransCount";
			this.txtTransCount.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtTransCount.Size = new System.Drawing.Size(217, 20);
			this.txtTransCount.TabIndex = 76;
			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(92, 10);
			this.txtDeviceName.MaxLength = 0;
			this.txtDeviceName.Name = "txtDeviceName";
			this.txtDeviceName.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtDeviceName.Size = new System.Drawing.Size(68, 20);
			this.txtDeviceName.TabIndex = 75;
			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, 339);
			this.Label5.Name = "Label5";
			this.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label5.Size = new System.Drawing.Size(73, 13);
			this.Label5.TabIndex = 84;
			this.Label5.Text = "Return Value:";
			// 
			// 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, 312);
			this.Label4.Name = "Label4";
			this.Label4.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label4.Size = new System.Drawing.Size(32, 13);
			this.Label4.TabIndex = 83;
			this.Label4.Text = "Error:";
			// 
			// 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, 286);
			this.Label3.Name = "Label3";
			this.Label3.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label3.Size = new System.Drawing.Size(39, 13);
			this.Label3.TabIndex = 82;
			this.Label3.Text = "Status:";
			// 
			// 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, 260);
			this.Label2.Name = "Label2";
			this.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label2.Size = new System.Drawing.Size(38, 13);
			this.Label2.TabIndex = 81;
			this.Label2.Text = "Count:";
			// 
			// 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, 13);
			this.Label1.Name = "Label1";
			this.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label1.Size = new System.Drawing.Size(75, 13);
			this.Label1.TabIndex = 80;
			this.Label1.Text = "Device Name:";
			// 
			// Sampling
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(424, 366);
			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 = "Sampling";
			this.Load += new System.EventHandler(this.Sampling_Load);
			this.Closed += new System.EventHandler(this.Sampling_Closed);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Sampling());
		}

		//===================================================
		// Dialog iniitialization
		//===================================================
		private void Sampling_Load(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Initialize screen display
			//-----------------------------------
			txtDeviceName.Text = "CNT000";

			//-----------------------------------
			// Specify Pinned handle to SamplingData
			//-----------------------------------
			if (gCh.IsAllocated == false)
			{
				gCh = GCHandle.Alloc(SamplingData, GCHandleType.Pinned);
			}
		}

		//===================================================
		// Termination processing
		//===================================================
		private void Sampling_Closed(object sender, System.EventArgs e)
		{
			int	ret;

			//-----------------------------------
			// GCHandle.Free()
			//-----------------------------------
			if (gCh.IsAllocated == true)
			{
				gCh.Free();
			}
			
			//-----------------------------------
			// Termination processing
			//-----------------------------------
			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;
	
			//-----------------------------------
			// Get device name
			//-----------------------------------
			buf = txtDeviceName.Text;
			//-----------------------------------
			// Initialization
			//-----------------------------------
			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;
		}

		//---------------------------------------------
		// Dispaly "Counter Mode Setting" dialog
		//---------------------------------------------
		private void btnCntMode_Click(object sender, System.EventArgs e)
		{
			//------------------------------
			// Dispaly "Counter Mode Setting" dialog
			//------------------------------
			ModeDlg dlg = new ModeDlg();
			dlg.ShowDialog();
			dlg.Dispose();
		}

		//===================================================
		// Display "Infinite Sampling Condition Setting" dialog
		//===================================================
		private void btnCondition_Click(object sender, System.EventArgs e)
		{
			ConditionDlg	dlg = new ConditionDlg();
			//-----------------------------------
			// Display "Infinite Sampling Condition Setting" dialog
			//-----------------------------------
			dlg.ShowDialog();
			dlg.Dispose();
		}

		//===================================================
		// Transfer start
		//===================================================
		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;

			//-----------------------------------
			// Set message notification
			//-----------------------------------
			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;
			}
			//-----------------------------------
			// Reset FIFO (Input)
			//-----------------------------------
			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;
			}
			//-----------------------------------
			// Set data buffer
			//-----------------------------------
			Array.Clear(SamplingData, 0, SamplingData.Length);
			//-----------------------------------
			// Get object address by Pinned handle
			//-----------------------------------
			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;
			}
			//------------------------------
			// Set parameter
			//------------------------------
			for (i=0; i<CHANNEL_NUM; i++) 
			{
				ChNo[i] = i;
			}
			//------------------------------
			// Start count
			//------------------------------
			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;
			}
			//-----------------------------------
			// Transfer start
			//-----------------------------------
			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 = "";
		}

		//===================================================
		// Transfer stop
		//===================================================
		private void btnStop_Click(object sender, System.EventArgs e)
		{
			string	error_string = new string(' ', 256);
			string	str;
			int		ret;

			//-----------------------------------
			// Transfer stop
			//-----------------------------------
			ret = cnt.SamplingStop(Id);
			cnt.GetErrorString(ret, out error_string);
			str = string.Format("CntSamplingStop ret = {0:d}: {1}", ret, error_string);
			txtReturn.Text = str;
		}

		//===================================================
		// Display data file
		//===================================================
		private void btnData_Click(object sender, System.EventArgs e)
		{
			string		buf = new string(' ', 256);
			short		i, j;
			TextWriter	fp;
			string		file_name = "SamplingData.csv";

			//-----------------------------------
			// Write transferred data to file
			//-----------------------------------
			//-----------------------------------
			// Open file
			//-----------------------------------
			try
			{
				fp = File.CreateText(file_name);
			}
			catch (System.IO.IOException)
			{
				txtReturn.Text = "fopen : file open error.";
				return;
			}
			//-----------------------------------
			// Write data
			//-----------------------------------
			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); 
			}
			//-----------------------------------
			// Close file
			//-----------------------------------
			fp.Close();
	
			//-----------------------------------
			// notepad
			//-----------------------------------
			Process Pro = Process.Start("notepad.exe", file_name);

		}

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

			//-----------------------------------
			// Get transfer count
			//-----------------------------------
			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();
			}
		}

		//===================================================
		// Transfer status
		//===================================================
		private void btnStatus_Click(object sender, System.EventArgs e)
		{
			uint	status, err;
			string	error_string = new string(' ', 256);
			string	str;
			int		ret;

			//-----------------------------------
			// Get transfer status
			//-----------------------------------
			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();
		}
	
		//===================================================
		// Processing of counter event notification
		//===================================================
		protected override void WndProc(ref Message m)
		{
			switch( m.Msg ) 
			{
				case (int)CcntConst.CNTM_SAMPLING_COUNT:	// Stop transfer by specified times
					btnCount_Click(btnCount, new System.EventArgs());
					break;
				case (int)CcntConst.CNTM_SAMPLING_STOP:		// Transfer stopped
					txtStatus.Text = "Stopped!";
					btnCount_Click(btnCount, new System.EventArgs());
					break;
			}

			base.WndProc(ref m);
		}
	}
}
