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

namespace Sync
{
	/// <summary>
	/// Summary description for Sync.
	/// </summary>
	public class Sync : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Button btnMDioExit;
		private System.Windows.Forms.Button btnMStatus;
		private System.Windows.Forms.Button btnMCount;
		private System.Windows.Forms.Button btnMData;
		private System.Windows.Forms.Button btnMStop;
		private System.Windows.Forms.Button btnMStart;
		private System.Windows.Forms.Button btnMCondition;
		private System.Windows.Forms.Button btnMDioInit;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.Button btnSDioExit;
		private System.Windows.Forms.Button btnSStatus;
		private System.Windows.Forms.Button btnSCount;
		private System.Windows.Forms.Button btnSData;
		private System.Windows.Forms.Button btnSStop;
		private System.Windows.Forms.Button btnSStart;
		private System.Windows.Forms.Button btnSCondition;
		private System.Windows.Forms.Button btnSDioInit;
		private System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.GroupBox groupBox5;
		private System.Windows.Forms.TextBox txtSDeviceName;
		private System.Windows.Forms.TextBox txtMDeviceName;
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.TextBox txtSReturn;
		private System.Windows.Forms.TextBox txtSError;
		private System.Windows.Forms.TextBox txtSStatus;
		private System.Windows.Forms.TextBox txtSCount;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.TextBox txtMReturn;
		private System.Windows.Forms.TextBox txtMError;
		private System.Windows.Forms.TextBox txtMStatus;
		private System.Windows.Forms.TextBox txtMCount;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label3;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		Cdio				dio = new Cdio();
		const int			DATA_SIZE =	1000;					// Buffer size
		public static short	M_Id;								// Master device ID
		public static short	S_Id;								// Slave device ID
		static uint[]		M_BmBuffer = new uint[DATA_SIZE];	// Master buffer
		static uint[]		S_BmBuffer = new uint[DATA_SIZE];	// Slave buffer

		GCHandle			gCh1, gCh2;
		IntPtr				pMBmBuffer, pSBmBuffer;

		public Sync()
		{
			//
			// 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.groupBox3 = new System.Windows.Forms.GroupBox();
			this.btnMDioExit = new System.Windows.Forms.Button();
			this.btnMStatus = new System.Windows.Forms.Button();
			this.btnMCount = new System.Windows.Forms.Button();
			this.btnMData = new System.Windows.Forms.Button();
			this.btnMStop = new System.Windows.Forms.Button();
			this.btnMStart = new System.Windows.Forms.Button();
			this.btnMCondition = new System.Windows.Forms.Button();
			this.btnMDioInit = new System.Windows.Forms.Button();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.btnSDioExit = new System.Windows.Forms.Button();
			this.btnSStatus = new System.Windows.Forms.Button();
			this.btnSCount = new System.Windows.Forms.Button();
			this.btnSData = new System.Windows.Forms.Button();
			this.btnSStop = new System.Windows.Forms.Button();
			this.btnSStart = new System.Windows.Forms.Button();
			this.btnSCondition = new System.Windows.Forms.Button();
			this.btnSDioInit = new System.Windows.Forms.Button();
			this.btnExit = new System.Windows.Forms.Button();
			this.groupBox5 = new System.Windows.Forms.GroupBox();
			this.txtSDeviceName = new System.Windows.Forms.TextBox();
			this.txtMDeviceName = new System.Windows.Forms.TextBox();
			this.label12 = new System.Windows.Forms.Label();
			this.label11 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.label7 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.label9 = new System.Windows.Forms.Label();
			this.label10 = new System.Windows.Forms.Label();
			this.txtSReturn = new System.Windows.Forms.TextBox();
			this.txtSError = new System.Windows.Forms.TextBox();
			this.txtSStatus = new System.Windows.Forms.TextBox();
			this.txtSCount = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.txtMReturn = new System.Windows.Forms.TextBox();
			this.txtMError = new System.Windows.Forms.TextBox();
			this.txtMStatus = new System.Windows.Forms.TextBox();
			this.txtMCount = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.groupBox3.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.groupBox5.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnMDioExit,
																					this.btnMStatus,
																					this.btnMCount,
																					this.btnMData,
																					this.btnMStop,
																					this.btnMStart,
																					this.btnMCondition,
																					this.btnMDioInit});
			this.groupBox3.Location = new System.Drawing.Point(360, 7);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(126, 338);
			this.groupBox3.TabIndex = 24;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Master:";
			// 
			// btnMDioExit
			// 
			this.btnMDioExit.Location = new System.Drawing.Point(12, 296);
			this.btnMDioExit.Name = "btnMDioExit";
			this.btnMDioExit.Size = new System.Drawing.Size(100, 26);
			this.btnMDioExit.TabIndex = 18;
			this.btnMDioExit.Text = "DioExit";
			this.btnMDioExit.Click += new System.EventHandler(this.btnMDioExit_Click);
			// 
			// btnMStatus
			// 
			this.btnMStatus.Location = new System.Drawing.Point(12, 256);
			this.btnMStatus.Name = "btnMStatus";
			this.btnMStatus.Size = new System.Drawing.Size(100, 26);
			this.btnMStatus.TabIndex = 17;
			this.btnMStatus.Text = "Transfer Status";
			this.btnMStatus.Click += new System.EventHandler(this.btnMStatus_Click);
			// 
			// btnMCount
			// 
			this.btnMCount.Location = new System.Drawing.Point(12, 216);
			this.btnMCount.Name = "btnMCount";
			this.btnMCount.Size = new System.Drawing.Size(100, 26);
			this.btnMCount.TabIndex = 16;
			this.btnMCount.Text = "Transfer Count";
			this.btnMCount.Click += new System.EventHandler(this.btnMCount_Click);
			// 
			// btnMData
			// 
			this.btnMData.Location = new System.Drawing.Point(12, 176);
			this.btnMData.Name = "btnMData";
			this.btnMData.Size = new System.Drawing.Size(100, 26);
			this.btnMData.TabIndex = 15;
			this.btnMData.Text = "Get Data...";
			this.btnMData.Click += new System.EventHandler(this.btnMData_Click);
			// 
			// btnMStop
			// 
			this.btnMStop.Location = new System.Drawing.Point(12, 136);
			this.btnMStop.Name = "btnMStop";
			this.btnMStop.Size = new System.Drawing.Size(100, 26);
			this.btnMStop.TabIndex = 14;
			this.btnMStop.Text = "Stop Transfer";
			this.btnMStop.Click += new System.EventHandler(this.btnMStop_Click);
			// 
			// btnMStart
			// 
			this.btnMStart.Location = new System.Drawing.Point(12, 96);
			this.btnMStart.Name = "btnMStart";
			this.btnMStart.Size = new System.Drawing.Size(100, 26);
			this.btnMStart.TabIndex = 13;
			this.btnMStart.Text = "Start  Transfer ";
			this.btnMStart.Click += new System.EventHandler(this.btnMStart_Click);
			// 
			// btnMCondition
			// 
			this.btnMCondition.Location = new System.Drawing.Point(12, 56);
			this.btnMCondition.Name = "btnMCondition";
			this.btnMCondition.Size = new System.Drawing.Size(100, 26);
			this.btnMCondition.TabIndex = 12;
			this.btnMCondition.Text = "Set Condition...";
			this.btnMCondition.Click += new System.EventHandler(this.btnMCondition_Click);
			// 
			// btnMDioInit
			// 
			this.btnMDioInit.Location = new System.Drawing.Point(12, 16);
			this.btnMDioInit.Name = "btnMDioInit";
			this.btnMDioInit.Size = new System.Drawing.Size(100, 26);
			this.btnMDioInit.TabIndex = 11;
			this.btnMDioInit.Text = "DioInit";
			this.btnMDioInit.Click += new System.EventHandler(this.btnMDioInit_Click);
			// 
			// groupBox4
			// 
			this.groupBox4.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnSDioExit,
																					this.btnSStatus,
																					this.btnSCount,
																					this.btnSData,
																					this.btnSStop,
																					this.btnSStart,
																					this.btnSCondition,
																					this.btnSDioInit});
			this.groupBox4.Location = new System.Drawing.Point(494, 7);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(126, 338);
			this.groupBox4.TabIndex = 25;
			this.groupBox4.TabStop = false;
			this.groupBox4.Text = "Slave:";
			// 
			// btnSDioExit
			// 
			this.btnSDioExit.Location = new System.Drawing.Point(12, 296);
			this.btnSDioExit.Name = "btnSDioExit";
			this.btnSDioExit.Size = new System.Drawing.Size(100, 26);
			this.btnSDioExit.TabIndex = 26;
			this.btnSDioExit.Text = "DioExit";
			this.btnSDioExit.Click += new System.EventHandler(this.btnSDioExit_Click);
			// 
			// btnSStatus
			// 
			this.btnSStatus.Location = new System.Drawing.Point(12, 256);
			this.btnSStatus.Name = "btnSStatus";
			this.btnSStatus.Size = new System.Drawing.Size(100, 26);
			this.btnSStatus.TabIndex = 25;
			this.btnSStatus.Text = "Transfer Status";
			this.btnSStatus.Click += new System.EventHandler(this.btnSStatus_Click);
			// 
			// btnSCount
			// 
			this.btnSCount.Location = new System.Drawing.Point(12, 216);
			this.btnSCount.Name = "btnSCount";
			this.btnSCount.Size = new System.Drawing.Size(100, 26);
			this.btnSCount.TabIndex = 24;
			this.btnSCount.Text = "Transfer Count";
			this.btnSCount.Click += new System.EventHandler(this.btnSCount_Click);
			// 
			// btnSData
			// 
			this.btnSData.Location = new System.Drawing.Point(12, 176);
			this.btnSData.Name = "btnSData";
			this.btnSData.Size = new System.Drawing.Size(100, 26);
			this.btnSData.TabIndex = 23;
			this.btnSData.Text = "Get Data...";
			this.btnSData.Click += new System.EventHandler(this.btnSData_Click);
			// 
			// btnSStop
			// 
			this.btnSStop.Location = new System.Drawing.Point(12, 136);
			this.btnSStop.Name = "btnSStop";
			this.btnSStop.Size = new System.Drawing.Size(100, 26);
			this.btnSStop.TabIndex = 22;
			this.btnSStop.Text = "Stop Transfer";
			this.btnSStop.Click += new System.EventHandler(this.btnSStop_Click);
			// 
			// btnSStart
			// 
			this.btnSStart.Location = new System.Drawing.Point(12, 96);
			this.btnSStart.Name = "btnSStart";
			this.btnSStart.Size = new System.Drawing.Size(100, 26);
			this.btnSStart.TabIndex = 21;
			this.btnSStart.Text = "Start  Transfer ";
			this.btnSStart.Click += new System.EventHandler(this.btnSStart_Click);
			// 
			// btnSCondition
			// 
			this.btnSCondition.Location = new System.Drawing.Point(12, 56);
			this.btnSCondition.Name = "btnSCondition";
			this.btnSCondition.Size = new System.Drawing.Size(100, 26);
			this.btnSCondition.TabIndex = 20;
			this.btnSCondition.Text = "Set Condition...";
			this.btnSCondition.Click += new System.EventHandler(this.btnSCondition_Click);
			// 
			// btnSDioInit
			// 
			this.btnSDioInit.Location = new System.Drawing.Point(12, 16);
			this.btnSDioInit.Name = "btnSDioInit";
			this.btnSDioInit.Size = new System.Drawing.Size(100, 26);
			this.btnSDioInit.TabIndex = 19;
			this.btnSDioInit.Text = "DioInit";
			this.btnSDioInit.Click += new System.EventHandler(this.btnSDioInit_Click);
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(443, 355);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(100, 26);
			this.btnExit.TabIndex = 27;
			this.btnExit.Text = "Exit";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// groupBox5
			// 
			this.groupBox5.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtSDeviceName,
																					this.txtMDeviceName,
																					this.label12,
																					this.label11});
			this.groupBox5.Location = new System.Drawing.Point(8, 7);
			this.groupBox5.Name = "groupBox5";
			this.groupBox5.Size = new System.Drawing.Size(344, 49);
			this.groupBox5.TabIndex = 21;
			this.groupBox5.TabStop = false;
			this.groupBox5.Text = "Device Name";
			// 
			// txtSDeviceName
			// 
			this.txtSDeviceName.Location = new System.Drawing.Point(237, 18);
			this.txtSDeviceName.Name = "txtSDeviceName";
			this.txtSDeviceName.Size = new System.Drawing.Size(96, 20);
			this.txtSDeviceName.TabIndex = 2;
			this.txtSDeviceName.Text = "";
			// 
			// txtMDeviceName
			// 
			this.txtMDeviceName.Location = new System.Drawing.Point(81, 18);
			this.txtMDeviceName.Name = "txtMDeviceName";
			this.txtMDeviceName.Size = new System.Drawing.Size(95, 20);
			this.txtMDeviceName.TabIndex = 1;
			this.txtMDeviceName.Text = "";
			// 
			// label12
			// 
			this.label12.Location = new System.Drawing.Point(191, 20);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(37, 22);
			this.label12.TabIndex = 1;
			this.label12.Text = "Slave:";
			// 
			// label11
			// 
			this.label11.Location = new System.Drawing.Point(27, 20);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(45, 22);
			this.label11.TabIndex = 0;
			this.label11.Text = "Master:";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.label7,
																					this.label8,
																					this.label9,
																					this.label10,
																					this.txtSReturn,
																					this.txtSError,
																					this.txtSStatus,
																					this.txtSCount});
			this.groupBox2.Location = new System.Drawing.Point(8, 207);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(344, 139);
			this.groupBox2.TabIndex = 23;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Slave Status";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(8, 107);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(72, 18);
			this.label7.TabIndex = 11;
			this.label7.Text = "Return Code:";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(8, 79);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(64, 18);
			this.label8.TabIndex = 10;
			this.label8.Text = "Error:";
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(8, 51);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(64, 18);
			this.label9.TabIndex = 9;
			this.label9.Text = "Status:";
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(8, 23);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(64, 19);
			this.label10.TabIndex = 8;
			this.label10.Text = "Count:";
			// 
			// txtSReturn
			// 
			this.txtSReturn.Location = new System.Drawing.Point(81, 107);
			this.txtSReturn.Name = "txtSReturn";
			this.txtSReturn.Size = new System.Drawing.Size(252, 20);
			this.txtSReturn.TabIndex = 10;
			this.txtSReturn.Text = "";
			// 
			// txtSError
			// 
			this.txtSError.Location = new System.Drawing.Point(81, 79);
			this.txtSError.Name = "txtSError";
			this.txtSError.Size = new System.Drawing.Size(252, 20);
			this.txtSError.TabIndex = 9;
			this.txtSError.Text = "";
			// 
			// txtSStatus
			// 
			this.txtSStatus.Location = new System.Drawing.Point(81, 51);
			this.txtSStatus.Name = "txtSStatus";
			this.txtSStatus.Size = new System.Drawing.Size(252, 20);
			this.txtSStatus.TabIndex = 8;
			this.txtSStatus.Text = "";
			// 
			// txtSCount
			// 
			this.txtSCount.Location = new System.Drawing.Point(81, 23);
			this.txtSCount.Name = "txtSCount";
			this.txtSCount.Size = new System.Drawing.Size(252, 20);
			this.txtSCount.TabIndex = 7;
			this.txtSCount.Text = "";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtMReturn,
																					this.txtMError,
																					this.txtMStatus,
																					this.txtMCount,
																					this.label6,
																					this.label5,
																					this.label4,
																					this.label3});
			this.groupBox1.Location = new System.Drawing.Point(8, 62);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(344, 139);
			this.groupBox1.TabIndex = 22;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Master Status";
			// 
			// txtMReturn
			// 
			this.txtMReturn.Location = new System.Drawing.Point(81, 107);
			this.txtMReturn.Name = "txtMReturn";
			this.txtMReturn.Size = new System.Drawing.Size(252, 20);
			this.txtMReturn.TabIndex = 6;
			this.txtMReturn.Text = "";
			// 
			// txtMError
			// 
			this.txtMError.Location = new System.Drawing.Point(81, 79);
			this.txtMError.Name = "txtMError";
			this.txtMError.Size = new System.Drawing.Size(252, 20);
			this.txtMError.TabIndex = 5;
			this.txtMError.Text = "";
			// 
			// txtMStatus
			// 
			this.txtMStatus.Location = new System.Drawing.Point(81, 51);
			this.txtMStatus.Name = "txtMStatus";
			this.txtMStatus.Size = new System.Drawing.Size(252, 20);
			this.txtMStatus.TabIndex = 4;
			this.txtMStatus.Text = "";
			// 
			// txtMCount
			// 
			this.txtMCount.Location = new System.Drawing.Point(81, 23);
			this.txtMCount.Name = "txtMCount";
			this.txtMCount.Size = new System.Drawing.Size(252, 20);
			this.txtMCount.TabIndex = 3;
			this.txtMCount.Text = "";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(8, 107);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(72, 18);
			this.label6.TabIndex = 3;
			this.label6.Text = "Return Code:";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(8, 79);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(64, 18);
			this.label5.TabIndex = 2;
			this.label5.Text = "Error:";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 51);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(64, 18);
			this.label4.TabIndex = 1;
			this.label4.Text = "Status:";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 23);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 19);
			this.label3.TabIndex = 0;
			this.label3.Text = "Count:";
			// 
			// Sync
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(631, 390);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox3,
																		  this.groupBox4,
																		  this.btnExit,
																		  this.groupBox5,
																		  this.groupBox2,
																		  this.groupBox1});
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Sync";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Sync Sampling";
			this.Load += new System.EventHandler(this.Sync_Load);
			this.Closed += new System.EventHandler(this.Sync_Closed);
			this.groupBox3.ResumeLayout(false);
			this.groupBox4.ResumeLayout(false);
			this.groupBox5.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		//===================================================
		//	Dialog init
		//===================================================
		private void Sync_Load(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Dialog init
			//-----------------------------------
			txtMDeviceName.Text = "DIO000";
			txtSDeviceName.Text = "DIO001";

			//-----------------------------------
			// Pinned type handle is allocated to M_BmBuffer
			//-----------------------------------
			if (gCh1.IsAllocated == false)
			{
				gCh1 = GCHandle.Alloc(M_BmBuffer, GCHandleType.Pinned);
			}
			//-----------------------------------
			// Pinned type handle is allocated to S_BmBuffer
			//-----------------------------------
			if (gCh2.IsAllocated == false)
			{
				gCh2 = GCHandle.Alloc(S_BmBuffer, GCHandleType.Pinned);
			}
		}

		//===================================================
		//	Close handle
		//===================================================
		private void Sync_Closed(object sender, System.EventArgs e)
		{
			int	ret;
			//-----------------------------------
			// DioExit()
			//-----------------------------------
			ret = dio.Exit(M_Id);
			ret	= dio.Exit(S_Id);

			//-----------------------------------
			// GCHandle is freed
			//-----------------------------------
			if (gCh1.IsAllocated == true)
			{
				gCh1.Free();
			}
			if (gCh2.IsAllocated == true)
			{
				gCh2.Free();
			}
		}

		//===================================================
		//	Master dioInit()
		//===================================================
		private void btnMDioInit_Click(object sender, System.EventArgs e)
		{
			string	str;
			string	error_string = new string(' ', 256);
			int		ret;
			//-----------------------------------
			// DioInit()
			//-----------------------------------
			ret	= dio.Init(txtMDeviceName.Text, out M_Id);
			dio.GetErrorString(ret, out error_string);
			str = string.Format("DioInit ret = {0:d}: {1}", ret, error_string);
			txtMReturn.Text = str;
			return;
		}

		//===================================================
		//	Master set condition
		//===================================================
		private void btnMCondition_Click(object sender, System.EventArgs e)
		{
			MCondition	MConditionDlg = new MCondition();
			//-----------------------------------
			// Show set condition dialog
			//-----------------------------------
			MConditionDlg.ShowDialog();
			MConditionDlg.Dispose();
			return;
		}

		//===================================================
		//	Master start transfer
		//===================================================
		private void btnMStart_Click(object sender, System.EventArgs e)
		{
			string	error_string = new string(' ', 256);
			string	str;
			int		ret;

			//-----------------------------------
			// Set notification of message
			//-----------------------------------
			ret = dio.DmSetStopEvent(M_Id, (short)CdioConst.DIODM_DIR_IN, this.Handle);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				str = string.Format("DioDmSetStopEvent ret = {0:d}: {1}", ret, error_string);
				txtMReturn.Text = str;
				return;
			}
			ret = dio.DmSetCountEvent(M_Id, (short)CdioConst.DIODM_DIR_IN, MCondition.M_NumberOfData, this.Handle);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				str = string.Format("DioDmSetCountEvent ret = {0:d}: {1}", ret, error_string);
				txtMReturn.Text = str;
				return;
			}
			//-----------------------------------
			// Reset FIFO (input)
			//-----------------------------------
			ret	= dio.DmFifoReset(M_Id, (short)CdioConst.DIODM_RESET_FIFO_IN);
			if(ret != (int)CdioConst.DIO_ERR_SUCCESS)
			{
				dio.GetErrorString(ret, out error_string);
				str = string.Format("DioDmFifoReset ret = {0:d}: {1}", ret, error_string);
				txtMReturn.Text = str;
				return;
			}
			//-----------------------------------
			// Set data buffer
			//-----------------------------------
			Array.Clear(M_BmBuffer, 0, DATA_SIZE);
			//-----------------------------------
			// The fixed pointer to an object is acquired
			//-----------------------------------
			pMBmBuffer = gCh1.AddrOfPinnedObject();
			ret = dio.DmSetBuffer(M_Id, (short)CdioConst.DIODM_DIR_IN, pMBmBuffer, DATA_SIZE, MCondition.M_IsRing);
			if(ret != (int)CdioConst.DIO_ERR_SUCCESS)
			{
				dio.GetErrorString(ret, out error_string);
				str = string.Format("DioDmSetBuffer ret = {0:d}: {1}", ret, error_string);
				txtMReturn.Text = str;
				return;
			}
			//-----------------------------------
			// Start transfer
			//-----------------------------------
			ret = dio.DmTransferStart(M_Id, (short)CdioConst.DIODM_DIR_IN);
			if(ret != (int)CdioConst.DIO_ERR_SUCCESS)
			{
				dio.GetErrorString(ret, out error_string);
				str = string.Format("DioDmTransferStart ret = {0:d}: {1}", ret, error_string);
				txtMReturn.Text = str;
				return;
			}
			txtMCount.Text = "";
			txtMStatus.Text = "";
			txtMError.Text = "";
			return;
		}

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

			//-----------------------------------
			// Stop transfer
			//-----------------------------------
			ret = dio.DmTransferStop(M_Id, (short)CdioConst.DIODM_DIR_IN);
			dio.GetErrorString(ret, out error_string);
			str = string.Format("DioDmTransferStop ret = {0:d}: {1}", ret, error_string);
			txtMReturn.Text = str;
			return;
		}

		//===================================================
		//	Master get data
		//===================================================
		private void btnMData_Click(object sender, System.EventArgs e)
		{
			StreamWriter	fp;
			string			file_name = "MSamplingData.csv";
			string			buf = new string(' ', 100);
			uint			i;

			//-----------------------------------
			// Open file
			//-----------------------------------
			fp = File.CreateText(file_name);
			//-----------------------------------
			// Write data into file
			//-----------------------------------
			for (i = 0; i < DATA_SIZE; i++) 
			{
				buf = string.Format("{0:d8}: ,{1:X8}", i, M_BmBuffer[i]);
				fp.WriteLine(buf); 
			}
			//-----------------------------------
			// Close file
			//-----------------------------------
			fp.Close();
			//-----------------------------------
			// notepad
			//-----------------------------------
			Process Pro = Process.Start("notepad.exe", file_name);
			return;
		}

		//===================================================
		//	Master transfer count
		//===================================================
		private void btnMCount_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 = dio.DmGetCount(M_Id, (short)CdioConst.DIODM_DIR_IN, out count, out carry);
			dio.GetErrorString(ret, out error_string);
			str = string.Format("DioDmGetCount ret = {0:d}: {1}", ret, error_string);
			txtMReturn.Text = str;
			if( ret == (int)CdioConst.DIO_ERR_SUCCESS)
			{
				str = string.Format("{0:d}", count);
				txtMCount.Text = str;
			}
			return;
		}

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

			//-----------------------------------
			// Get transfer status
			//-----------------------------------
			ret = dio.DmGetStatus(M_Id, (short)CdioConst.DIODM_DIR_IN, out status, out err);
			dio.GetErrorString(ret, out error_string);
			str = string.Format("DioDmGetStatus ret = {0:d}: {1}", ret, error_string);
			txtMReturn.Text = str;
			if(ret != (int)CdioConst.DIO_ERR_SUCCESS)
			{
				return;
			}
			// Status
			str = "";
			if ((status & (uint)CdioConst.DIODM_STATUS_BMSTOP) != 0)	str += "BmStop,";
			if ((status & (uint)CdioConst.DIODM_STATUS_PIOSTART) != 0)	str += "PioStart,";
			if ((status & (uint)CdioConst.DIODM_STATUS_PIOSTOP) != 0)	str += "PioStop,";
			if ((status & (uint)CdioConst.DIODM_STATUS_TRGIN) != 0)		str += "TrgIn,";
			if ((status & (uint)CdioConst.DIODM_STATUS_OVERRUN) != 0)	str += "Overrun";
			txtMStatus.Text = str;
			// Error
			str = "";
			if ((err & (uint)CdioConst.DIODM_STATUS_FIFOEMPTY) != 0)	str += "FifoEmpty,";
			if ((err & (uint)CdioConst.DIODM_STATUS_FIFOFULL) != 0)		str += "FifoFull,";
			if ((err & (uint)CdioConst.DIODM_STATUS_SGOVERIN) != 0)		str += "S/GOverIn,";
			if ((err & (uint)CdioConst.DIODM_STATUS_TRGERR) != 0)		str += "TrgErr,";
			if ((err & (uint)CdioConst.DIODM_STATUS_CLKERR) != 0)		str += "ClkErr,";
			if ((err & (uint)CdioConst.DIODM_STATUS_SLAVEHALT) != 0)	str += "SlaveHalt,";
			if ((err & (uint)CdioConst.DIODM_STATUS_MASTERHALT) != 0)	str += "MasterHalt";
			txtMError.Text = str;
			return;
		}

		//===================================================
		//	Master DioExit
		//===================================================
		private void btnMDioExit_Click(object sender, System.EventArgs e)
		{
			string	error_string = new string(' ', 256);
			string	str;
			int		ret;
	
			//-----------------------------------
			// DioExit
			//-----------------------------------
			ret = dio.Exit(M_Id);
			dio.GetErrorString(ret, out error_string);
			str = string.Format("DioExit ret = {0:d}: {1}", ret, error_string);
			txtMReturn.Text = str;
			return;
		}

		//===================================================
		//	Slave dioInit()
		//===================================================
		private void btnSDioInit_Click(object sender, System.EventArgs e)
		{
			string	str;
			string	error_string = new string(' ', 256);
			int		ret;
			//-----------------------------------
			// DioInit()
			//-----------------------------------
			ret	= dio.Init(txtSDeviceName.Text, out S_Id);
			dio.GetErrorString(ret, out error_string);
			str = string.Format("DioInit ret = {0:d}: {1}", ret, error_string);
			txtSReturn.Text = str;
			return;
		}

		//===================================================
		//	Slave set condition
		//===================================================
		private void btnSCondition_Click(object sender, System.EventArgs e)
		{
			SCondition	SConditionDlg = new SCondition();
			//-----------------------------------
			// Show set condition dialog
			//-----------------------------------
			SConditionDlg.ShowDialog();
			SConditionDlg.Dispose();
			return;
		}

		//===================================================
		//	Slave start transfer
		//===================================================
		private void btnSStart_Click(object sender, System.EventArgs e)
		{
			string	error_string = new string(' ', 256);
			string	str;
			int		ret;

			//-----------------------------------
			// Set notification of message
			//-----------------------------------
			ret = dio.DmSetStopEvent(S_Id, (short)CdioConst.DIODM_DIR_IN, this.Handle);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				str = string.Format("DioDmSetStopEvent ret = {0:d}: {1}", ret, error_string);
				txtSReturn.Text = str;
				return;
			}
			ret = dio.DmSetCountEvent(S_Id, (short)CdioConst.DIODM_DIR_IN, SCondition.S_NumberOfData, this.Handle);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				str = string.Format("DioDmSetCountEvent ret = {0:d}: {1}", ret, error_string);
				txtSReturn.Text = str;
				return;
			}
			//-----------------------------------
			// Reset FIFO (input)
			//-----------------------------------
			ret	= dio.DmFifoReset(S_Id, (short)CdioConst.DIODM_RESET_FIFO_IN);
			if(ret != (int)CdioConst.DIO_ERR_SUCCESS)
			{
				dio.GetErrorString(ret, out error_string);
				str = string.Format("DioDmFifoReset ret = {0:d}: {1}", ret, error_string);
				txtSReturn.Text = str;
				return;
			}
			//-----------------------------------
			// Set data buffer
			//-----------------------------------
			Array.Clear(S_BmBuffer, 0, DATA_SIZE);
			//-----------------------------------
			// The fixed pointer to an object is acquired
			//-----------------------------------
			pSBmBuffer = gCh2.AddrOfPinnedObject();
			ret = dio.DmSetBuffer(S_Id, (short)CdioConst.DIODM_DIR_IN, pSBmBuffer, DATA_SIZE, SCondition.S_IsRing);
			if(ret != (int)CdioConst.DIO_ERR_SUCCESS)
			{
				dio.GetErrorString(ret, out error_string);
				str = string.Format("DioDmSetBuffer ret = {0:d}: {1}", ret, error_string);
				txtSReturn.Text = str;
				return;
			}
			//-----------------------------------
			// Start transfer
			//-----------------------------------
			ret = dio.DmTransferStart(S_Id, (short)CdioConst.DIODM_DIR_IN);
			if(ret != (int)CdioConst.DIO_ERR_SUCCESS)
			{
				dio.GetErrorString(ret, out error_string);
				str = string.Format("DioDmTransferStart ret = %d: %s", ret, error_string);
				txtSReturn.Text = str;
				return;
			}
			txtSCount.Text = "";
			txtSStatus.Text = "";
			txtSError.Text = "";
			return;
		}

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

			//-----------------------------------
			// Stop transfer
			//-----------------------------------
			ret = dio.DmTransferStop(S_Id, (short)CdioConst.DIODM_DIR_IN);
			dio.GetErrorString(ret, out error_string);
			str = string.Format("DioDmTransferStop ret = {0:d}: {1}", ret, error_string);
			txtSReturn.Text = str;
			return;
		}

		//===================================================
		//	Slave get data
		//===================================================
		private void btnSData_Click(object sender, System.EventArgs e)
		{
			StreamWriter	fp;
			string			file_name = "SSamplingData.csv";
			string			buf = new string(' ', 100);
			uint			i;

			//-----------------------------------
			// Open file
			//-----------------------------------
			fp = File.CreateText(file_name);
			//-----------------------------------
			// Write data into file
			//-----------------------------------
			for (i = 0; i < DATA_SIZE; i++) 
			{
				buf = string.Format("{0:d8}: ,{1:X8}", i, S_BmBuffer[i]);
				fp.WriteLine(buf); 
			}
			//-----------------------------------
			// Close file
			//-----------------------------------
			fp.Close();
			//-----------------------------------
			// notepad
			//-----------------------------------
			Process Pro = Process.Start("notepad.exe", file_name);
			return;
		}

		//===================================================
		//	Slave transfer count
		//===================================================
		private void btnSCount_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 = dio.DmGetCount(S_Id, (short)CdioConst.DIODM_DIR_IN, out count, out carry);
			dio.GetErrorString(ret, out error_string);
			str = string.Format("DioDmGetCount ret = {0:d}: {1}", ret, error_string);
			txtSReturn.Text = str;
			if( ret == (int)CdioConst.DIO_ERR_SUCCESS)
			{
				str = string.Format("{0:d}", count);
				txtSCount.Text = str;
			}
			return;
		}

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

			//-----------------------------------
			// Get transfer status
			//-----------------------------------
			ret = dio.DmGetStatus(S_Id, (short)CdioConst.DIODM_DIR_IN, out status, out err);
			dio.GetErrorString(ret, out error_string);
			str = string.Format("DioDmGetStatus ret = {0:d}: {1}", ret, error_string);
			txtSReturn.Text = str;
			if(ret != (int)CdioConst.DIO_ERR_SUCCESS)
			{
				return;
			}
			// Status
			str = "";
			if ((status & (uint)CdioConst.DIODM_STATUS_BMSTOP) != 0)	str += "BmStop,";
			if ((status & (uint)CdioConst.DIODM_STATUS_PIOSTART) != 0)	str += "PioStart,";
			if ((status & (uint)CdioConst.DIODM_STATUS_PIOSTOP) != 0)	str += "PioStop,";
			if ((status & (uint)CdioConst.DIODM_STATUS_TRGIN) != 0)		str += "TrgIn,";
			if ((status & (uint)CdioConst.DIODM_STATUS_OVERRUN) != 0)	str += "Overrun";
			txtSStatus.Text = str;
			// Error
			str = "";
			if ((err & (uint)CdioConst.DIODM_STATUS_FIFOEMPTY) != 0)	str += "FifoEmpty,";
			if ((err & (uint)CdioConst.DIODM_STATUS_FIFOFULL) != 0)		str += "FifoFull,";
			if ((err & (uint)CdioConst.DIODM_STATUS_SGOVERIN) != 0)		str += "S/GOverIn,";
			if ((err & (uint)CdioConst.DIODM_STATUS_TRGERR) != 0)		str += "TrgErr,";
			if ((err & (uint)CdioConst.DIODM_STATUS_CLKERR) != 0)		str += "ClkErr,";
			if ((err & (uint)CdioConst.DIODM_STATUS_SLAVEHALT) != 0)	str += "SlaveHalt,";
			if ((err & (uint)CdioConst.DIODM_STATUS_MASTERHALT) != 0)	str += "MasterHalt";
			txtSError.Text = str;
			return;
		}

		//===================================================
		//	Slave DioExit
		//===================================================
		private void btnSDioExit_Click(object sender, System.EventArgs e)
		{
			string	error_string = new string(' ', 256);
			string	str;
			int		ret;
	
			//-----------------------------------
			// DioExit
			//-----------------------------------
			ret = dio.Exit(S_Id);
			dio.GetErrorString(ret, out error_string);
			str = string.Format("DioExit ret = {0:d}: {1}", ret, error_string);
			txtSReturn.Text = str;
			return;
		}

		//===================================================
		//	Exit
		//===================================================
		private void btnExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		//---------------------------------------------------
		//	Message handler
		//---------------------------------------------------
		protected override void WndProc(ref Message m)
		{
			switch (m.Msg)
			{
				//===================================================
				// Transfer stop message
				//===================================================
				case (int)CdioConst.DIO_DMM_STOP:
					//-----------------------------------
					// Distingnish device object according to device name 
					//-----------------------------------
					//-----------------------------------
					// Master
					//-----------------------------------
					if(m.WParam.ToInt32() == M_Id)
					{
						txtMStatus.Text = "Stopped!";
						goto case (int)CdioConst.DIO_DMM_COUNT;
					}
					//-----------------------------------
					// Slave
					//-----------------------------------
					else
					{
						txtSStatus.Text = "Stopped!";
						goto case (int)CdioConst.DIO_DMM_COUNT;
					}				
				
					//===================================================
					// Transfer specified number of data completion message
					//===================================================
				case (int)CdioConst.DIO_DMM_COUNT:
					int		ret;
					uint	count, carry;
					string	str;
					//-----------------------------------
					// Distingnish device object according to device name 
					//-----------------------------------
					//-----------------------------------
					// Master
					//-----------------------------------
					if(m.WParam.ToInt32() == M_Id)
					{
						ret = dio.DmGetCount(M_Id, (short)CdioConst.DIODM_DIR_IN, out count, out carry);
						str = string.Format("{0:d}", count);
						txtMCount.Text = str;
					}
					//-----------------------------------
					// Slave
					//-----------------------------------
					else
					{
						ret = dio.DmGetCount(S_Id, (short)CdioConst.DIODM_DIR_IN, out count, out carry);
						str = string.Format("{0:d}", count);
						txtSCount.Text = str;
					}
					break;
				default:
					break;
			}
			base.WndProc(ref m);
		}
	}
}
