﻿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>
	/// Sync の概要の説明です。
	/// </summary>
	public class Sync : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox5;
		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.GroupBox groupBox1;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.TextBox txtSDeviceName;
		private System.Windows.Forms.TextBox txtMDeviceName;
		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.TextBox txtMReturn;
		private System.Windows.Forms.TextBox txtMError;
		private System.Windows.Forms.TextBox txtMStatus;
		private System.Windows.Forms.TextBox txtMCount;
		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.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;
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.Container components = null;

		Cdio				dio = new Cdio();
		const int			DATA_SIZE =	1000;					// バスマスタ用バッファサイズ
		public static short	M_Id;								// マスターデバイスID
		public static short	S_Id;								// スレーブデバイスID
		static uint[]		M_BmBuffer = new uint[DATA_SIZE];	// マスターバスマスタ用バッファ
		static uint[]		S_BmBuffer = new uint[DATA_SIZE];	// スレーブバスマスタ用バッファ

		GCHandle			    M_gCh, S_gCh;
		IntPtr				    pM_BmBuf, pS_BmBuf;


		public Sync()
		{
			//
			// 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.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 = 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.btnExit = 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.groupBox5.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.SuspendLayout();
			// 
			// 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(10, 5);
			this.groupBox5.Name = "groupBox5";
			this.groupBox5.Size = new System.Drawing.Size(344, 56);
			this.groupBox5.TabIndex = 1;
			this.groupBox5.TabStop = false;
			this.groupBox5.Text = "デバイス名";
			// 
			// txtSDeviceName
			// 
			this.txtSDeviceName.Location = new System.Drawing.Point(232, 24);
			this.txtSDeviceName.Name = "txtSDeviceName";
			this.txtSDeviceName.Size = new System.Drawing.Size(96, 19);
			this.txtSDeviceName.TabIndex = 2;
			this.txtSDeviceName.Text = "";
			// 
			// txtMDeviceName
			// 
			this.txtMDeviceName.Location = new System.Drawing.Point(72, 24);
			this.txtMDeviceName.Name = "txtMDeviceName";
			this.txtMDeviceName.Size = new System.Drawing.Size(95, 19);
			this.txtMDeviceName.TabIndex = 1;
			this.txtMDeviceName.Text = "";
			// 
			// label12
			// 
			this.label12.Location = new System.Drawing.Point(178, 26);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(56, 19);
			this.label12.TabIndex = 1;
			this.label12.Text = "スレーブ：";
			// 
			// label11
			// 
			this.label11.Location = new System.Drawing.Point(19, 26);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(56, 19);
			this.label11.TabIndex = 0;
			this.label11.Text = "マスター：";
			// 
			// 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(10, 197);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(344, 126);
			this.groupBox2.TabIndex = 7;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "スレーブステータス";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(8, 96);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(64, 16);
			this.label7.TabIndex = 11;
			this.label7.Text = "戻り値：";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(8, 72);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(64, 16);
			this.label8.TabIndex = 10;
			this.label8.Text = "エラー：";
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(8, 48);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(64, 16);
			this.label9.TabIndex = 9;
			this.label9.Text = "ステータス：";
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(8, 24);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(64, 16);
			this.label10.TabIndex = 8;
			this.label10.Text = "カウント：";
			// 
			// txtSReturn
			// 
			this.txtSReturn.Location = new System.Drawing.Point(72, 96);
			this.txtSReturn.Name = "txtSReturn";
			this.txtSReturn.Size = new System.Drawing.Size(256, 19);
			this.txtSReturn.TabIndex = 10;
			this.txtSReturn.Text = "";
			// 
			// txtSError
			// 
			this.txtSError.Location = new System.Drawing.Point(72, 72);
			this.txtSError.Name = "txtSError";
			this.txtSError.Size = new System.Drawing.Size(256, 19);
			this.txtSError.TabIndex = 9;
			this.txtSError.Text = "";
			// 
			// txtSStatus
			// 
			this.txtSStatus.Location = new System.Drawing.Point(72, 48);
			this.txtSStatus.Name = "txtSStatus";
			this.txtSStatus.Size = new System.Drawing.Size(256, 19);
			this.txtSStatus.TabIndex = 8;
			this.txtSStatus.Text = "";
			// 
			// txtSCount
			// 
			this.txtSCount.Location = new System.Drawing.Point(72, 24);
			this.txtSCount.Name = "txtSCount";
			this.txtSCount.Size = new System.Drawing.Size(256, 19);
			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(10, 66);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(344, 126);
			this.groupBox1.TabIndex = 3;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "マスターステータス";
			// 
			// txtMReturn
			// 
			this.txtMReturn.Location = new System.Drawing.Point(72, 96);
			this.txtMReturn.Name = "txtMReturn";
			this.txtMReturn.Size = new System.Drawing.Size(256, 19);
			this.txtMReturn.TabIndex = 6;
			this.txtMReturn.Text = "";
			// 
			// txtMError
			// 
			this.txtMError.Location = new System.Drawing.Point(72, 72);
			this.txtMError.Name = "txtMError";
			this.txtMError.Size = new System.Drawing.Size(256, 19);
			this.txtMError.TabIndex = 5;
			this.txtMError.Text = "";
			// 
			// txtMStatus
			// 
			this.txtMStatus.Location = new System.Drawing.Point(72, 48);
			this.txtMStatus.Name = "txtMStatus";
			this.txtMStatus.Size = new System.Drawing.Size(256, 19);
			this.txtMStatus.TabIndex = 4;
			this.txtMStatus.Text = "";
			// 
			// txtMCount
			// 
			this.txtMCount.Location = new System.Drawing.Point(72, 24);
			this.txtMCount.Name = "txtMCount";
			this.txtMCount.Size = new System.Drawing.Size(256, 19);
			this.txtMCount.TabIndex = 3;
			this.txtMCount.Text = "";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(8, 96);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(64, 16);
			this.label6.TabIndex = 3;
			this.label6.Text = "戻り値：";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(8, 72);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(64, 16);
			this.label5.TabIndex = 2;
			this.label5.Text = "エラー：";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 48);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(64, 16);
			this.label4.TabIndex = 1;
			this.label4.Text = "ステータス：";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 24);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 16);
			this.label3.TabIndex = 0;
			this.label3.Text = "カウント：";
			// 
			// 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(362, 5);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(126, 317);
			this.groupBox3.TabIndex = 11;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "マスター";
			// 
			// btnMDioExit
			// 
			this.btnMDioExit.Location = new System.Drawing.Point(12, 283);
			this.btnMDioExit.Name = "btnMDioExit";
			this.btnMDioExit.Size = new System.Drawing.Size(100, 22);
			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, 246);
			this.btnMStatus.Name = "btnMStatus";
			this.btnMStatus.Size = new System.Drawing.Size(100, 22);
			this.btnMStatus.TabIndex = 17;
			this.btnMStatus.Text = "転送ステータス";
			this.btnMStatus.Click += new System.EventHandler(this.btnMStatus_Click);
			// 
			// btnMCount
			// 
			this.btnMCount.Location = new System.Drawing.Point(12, 209);
			this.btnMCount.Name = "btnMCount";
			this.btnMCount.Size = new System.Drawing.Size(100, 22);
			this.btnMCount.TabIndex = 16;
			this.btnMCount.Text = "転送カウント";
			this.btnMCount.Click += new System.EventHandler(this.btnMCount_Click);
			// 
			// btnMData
			// 
			this.btnMData.Location = new System.Drawing.Point(12, 172);
			this.btnMData.Name = "btnMData";
			this.btnMData.Size = new System.Drawing.Size(100, 22);
			this.btnMData.TabIndex = 15;
			this.btnMData.Text = "取得データ...";
			this.btnMData.Click += new System.EventHandler(this.btnMData_Click);
			// 
			// btnMStop
			// 
			this.btnMStop.Location = new System.Drawing.Point(12, 135);
			this.btnMStop.Name = "btnMStop";
			this.btnMStop.Size = new System.Drawing.Size(100, 22);
			this.btnMStop.TabIndex = 14;
			this.btnMStop.Text = "転送ストップ";
			this.btnMStop.Click += new System.EventHandler(this.btnMStop_Click);
			// 
			// btnMStart
			// 
			this.btnMStart.Location = new System.Drawing.Point(12, 98);
			this.btnMStart.Name = "btnMStart";
			this.btnMStart.Size = new System.Drawing.Size(100, 22);
			this.btnMStart.TabIndex = 13;
			this.btnMStart.Text = "転送スタート";
			this.btnMStart.Click += new System.EventHandler(this.btnMStart_Click);
			// 
			// btnMCondition
			// 
			this.btnMCondition.Location = new System.Drawing.Point(12, 61);
			this.btnMCondition.Name = "btnMCondition";
			this.btnMCondition.Size = new System.Drawing.Size(100, 22);
			this.btnMCondition.TabIndex = 12;
			this.btnMCondition.Text = "条件設定...";
			this.btnMCondition.Click += new System.EventHandler(this.btnMCondition_Click);
			// 
			// btnMDioInit
			// 
			this.btnMDioInit.Location = new System.Drawing.Point(12, 24);
			this.btnMDioInit.Name = "btnMDioInit";
			this.btnMDioInit.Size = new System.Drawing.Size(100, 22);
			this.btnMDioInit.TabIndex = 11;
			this.btnMDioInit.Text = "DioInit";
			this.btnMDioInit.Click += new System.EventHandler(this.btnMDioInit_Click);
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(445, 332);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(100, 22);
			this.btnExit.TabIndex = 27;
			this.btnExit.Text = "Exit";
			this.btnExit.Click += new System.EventHandler(this.btnExit_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(496, 5);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(126, 317);
			this.groupBox4.TabIndex = 19;
			this.groupBox4.TabStop = false;
			this.groupBox4.Text = "スレーブ";
			// 
			// btnSDioExit
			// 
			this.btnSDioExit.Location = new System.Drawing.Point(12, 283);
			this.btnSDioExit.Name = "btnSDioExit";
			this.btnSDioExit.Size = new System.Drawing.Size(100, 22);
			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, 246);
			this.btnSStatus.Name = "btnSStatus";
			this.btnSStatus.Size = new System.Drawing.Size(100, 22);
			this.btnSStatus.TabIndex = 25;
			this.btnSStatus.Text = "転送ステータス";
			this.btnSStatus.Click += new System.EventHandler(this.btnSStatus_Click);
			// 
			// btnSCount
			// 
			this.btnSCount.Location = new System.Drawing.Point(12, 209);
			this.btnSCount.Name = "btnSCount";
			this.btnSCount.Size = new System.Drawing.Size(100, 22);
			this.btnSCount.TabIndex = 24;
			this.btnSCount.Text = "転送カウント";
			this.btnSCount.Click += new System.EventHandler(this.btnSCount_Click);
			// 
			// btnSData
			// 
			this.btnSData.Location = new System.Drawing.Point(12, 172);
			this.btnSData.Name = "btnSData";
			this.btnSData.Size = new System.Drawing.Size(100, 22);
			this.btnSData.TabIndex = 23;
			this.btnSData.Text = "取得データ...";
			this.btnSData.Click += new System.EventHandler(this.btnSData_Click);
			// 
			// btnSStop
			// 
			this.btnSStop.Location = new System.Drawing.Point(12, 135);
			this.btnSStop.Name = "btnSStop";
			this.btnSStop.Size = new System.Drawing.Size(100, 22);
			this.btnSStop.TabIndex = 22;
			this.btnSStop.Text = "転送ストップ";
			this.btnSStop.Click += new System.EventHandler(this.btnSStop_Click);
			// 
			// btnSStart
			// 
			this.btnSStart.Location = new System.Drawing.Point(12, 98);
			this.btnSStart.Name = "btnSStart";
			this.btnSStart.Size = new System.Drawing.Size(100, 22);
			this.btnSStart.TabIndex = 21;
			this.btnSStart.Text = "転送スタート";
			this.btnSStart.Click += new System.EventHandler(this.btnSStart_Click);
			// 
			// btnSCondition
			// 
			this.btnSCondition.Location = new System.Drawing.Point(12, 61);
			this.btnSCondition.Name = "btnSCondition";
			this.btnSCondition.Size = new System.Drawing.Size(100, 22);
			this.btnSCondition.TabIndex = 20;
			this.btnSCondition.Text = "条件設定...";
			this.btnSCondition.Click += new System.EventHandler(this.btnSCondition_Click);
			// 
			// btnSDioInit
			// 
			this.btnSDioInit.Location = new System.Drawing.Point(12, 24);
			this.btnSDioInit.Name = "btnSDioInit";
			this.btnSDioInit.Size = new System.Drawing.Size(100, 22);
			this.btnSDioInit.TabIndex = 19;
			this.btnSDioInit.Text = "DioInit";
			this.btnSDioInit.Click += new System.EventHandler(this.btnSDioInit_Click);
			// 
			// Sync
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(632, 366);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox4,
																		  this.groupBox3,
																		  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 = "同期サンプリング";
			this.Load += new System.EventHandler(this.Sync_Load);
			this.Closed += new System.EventHandler(this.Sync_Closed);
			this.groupBox5.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.groupBox4.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		//===================================================
		//	ダイアログ初期化
		//===================================================
		private void Sync_Load(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// 画面表示の初期化
			//-----------------------------------
			txtMDeviceName.Text = "DIO000";
			txtSDeviceName.Text = "DIO001";
			//-----------------------------------
			// M_BmBuffer, S_BmBufferにPinnedタイプハンドルを割り当てます
			//-----------------------------------
			if (M_gCh.IsAllocated == false)
			{
				M_gCh = GCHandle.Alloc(M_BmBuffer, GCHandleType.Pinned);
			}
			if (S_gCh.IsAllocated == false)
			{
				S_gCh = GCHandle.Alloc(S_BmBuffer, GCHandleType.Pinned);
			}

		}

		//===================================================
		//	終了時処理
		//===================================================
		private void Sync_Closed(object sender, System.EventArgs e)
		{
			int	ret;
			//-----------------------------------
			// 終了処理
			//-----------------------------------
			ret = dio.Exit(M_Id);
			ret	= dio.Exit(S_Id);
			//-----------------------------------
			// GCHandleを解放します
			//-----------------------------------
			if (M_gCh.IsAllocated == true)
			{
				M_gCh.Free();
			}	
			if (S_gCh.IsAllocated == true)
			{
				S_gCh.Free();
			}	
		}

		//===================================================
		//	マスター DioInit()
		//===================================================
		private void btnMDioInit_Click(object sender, System.EventArgs e)
		{
			string	str;
			string	error_string = new string(' ', 256);
			int		ret;
			//-----------------------------------
			// 初期化処理
			//-----------------------------------
			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;
		}

		//===================================================
		//	マスター 条件設定
		//===================================================
		private void btnMCondition_Click(object sender, System.EventArgs e)
		{
			MCondition	MConditionDlg = new MCondition();
			//-----------------------------------
			// 条件設定ダイアログを開く
			//-----------------------------------
			MConditionDlg.ShowDialog();
			MConditionDlg.Dispose();
			return;
		}

		//===================================================
		//	マスター 転送スタート
		//===================================================
		private void btnMStart_Click(object sender, System.EventArgs e)
		{
			string	error_string = new string(' ', 256);
			string	str;
			int		ret;

			//-----------------------------------
			// メッセージ通知設定
			//-----------------------------------
			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;
			}
			//-----------------------------------
			//	FIFOのリセット(入力)
			//-----------------------------------
			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;
			}
			//-----------------------------------
			// データバッファを設定
			//-----------------------------------
			Array.Clear(M_BmBuffer, 0, M_BmBuffer.Length);
			pM_BmBuf = M_gCh.AddrOfPinnedObject();
			ret = dio.DmSetBuffer(M_Id, (short)CdioConst.DIODM_DIR_IN, pM_BmBuf, 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;
			}
			//-----------------------------------
			// 転送スタート
			//-----------------------------------
			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;
		}

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

			//-----------------------------------
			// 転送ストップ
			//-----------------------------------
			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;
		}

		//===================================================
		//	マスター 取得データ
		//===================================================
		private void btnMData_Click(object sender, System.EventArgs e)
		{
			StreamWriter	fp;
			string			file_name = "MSamplingData.csv";
			string			buf = new string(' ', 100);
			uint			i;

			//-----------------------------------
			// ファイルを開く
			//-----------------------------------
			fp = File.CreateText(file_name);
			//-----------------------------------
			// ファイルに取得データを書き込みます。
			//-----------------------------------
			for (i = 0; i < DATA_SIZE; i++) 
			{
				buf = string.Format("{0:d8}: ,{1:X8}", i, M_BmBuffer[i]);
				fp.WriteLine(buf); 
			}
			//-----------------------------------
			// ファイルを閉じる
			//-----------------------------------
			fp.Close();
			//-----------------------------------
			// notepad
			//-----------------------------------
			Process Pro = Process.Start("notepad.exe", file_name);
			return;
		}

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

			//-----------------------------------
			// 転送カウント取得
			//-----------------------------------
			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;
		}

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

			//-----------------------------------
			// 転送ステータス取得
			//-----------------------------------
			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;
		}

		//===================================================
		//	マスター 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;
		}

		//===================================================
		//	スレーブ DioInit()
		//===================================================
		private void btnSDioInit_Click(object sender, System.EventArgs e)
		{
			string	str;
			string	error_string = new string(' ', 256);
			int		ret;
			//-----------------------------------
			// 初期化処理
			//-----------------------------------
			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;
		}

		//===================================================
		//	スレーブ 条件設定
		//===================================================
		private void btnSCondition_Click(object sender, System.EventArgs e)
		{
			SCondition	SConditionDlg = new SCondition();
			//-----------------------------------
			// 条件設定ダイアログを開く
			//-----------------------------------
			SConditionDlg.ShowDialog();
			SConditionDlg.Dispose();
			return;
		}

		//===================================================
		//	スレーブ 転送スタート
		//===================================================
		private void btnSStart_Click(object sender, System.EventArgs e)
		{
			string	error_string = new string(' ', 256);
			string	str;
			int		ret;

			//-----------------------------------
			// メッセージ通知設定
			//-----------------------------------
			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;
			}
			//-----------------------------------
			// FIFOのリセット(入力)
			//-----------------------------------
			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;
			}
			//-----------------------------------
			// データバッファを設定
			//-----------------------------------
			Array.Clear(S_BmBuffer, 0, S_BmBuffer.Length);
			pS_BmBuf = S_gCh.AddrOfPinnedObject();
			ret = dio.DmSetBuffer(S_Id, (short)CdioConst.DIODM_DIR_IN, pS_BmBuf, 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;
			}
			//-----------------------------------
			// 転送スタート
			//-----------------------------------
			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;
		}

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

			//-----------------------------------
			// 転送ストップ
			//-----------------------------------
			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;
		}

		//===================================================
		//	スレーブ 取得データ
		//===================================================
		private void btnSData_Click(object sender, System.EventArgs e)
		{
			StreamWriter	fp;
			string			file_name = "SSamplingData.csv";
			string			buf = new string(' ', 100);
			uint			i;

			//-----------------------------------
			// ファイルを開く
			//-----------------------------------
			fp = File.CreateText(file_name);
			//-----------------------------------
			// ファイルに取得データを書き込みます。
			//-----------------------------------
			for (i = 0; i < DATA_SIZE; i++) 
			{
				buf = string.Format("{0:d8}: ,{1:X8}", i, S_BmBuffer[i]);
				fp.WriteLine(buf); 
			}
			//-----------------------------------
			// ファイルを閉じる
			//-----------------------------------
			fp.Close();
			//-----------------------------------
			// notepad
			//-----------------------------------
			Process Pro = Process.Start("notepad.exe", file_name);
			return;
		}

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

			//-----------------------------------
			// 転送カウント取得
			//-----------------------------------
			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;
		}

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

			//-----------------------------------
			// 転送ステータス取得
			//-----------------------------------
			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;
		}

		//===================================================
		//	スレーブ 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();
		}

		//---------------------------------------------------
		//	メッセージハンドラ
		//---------------------------------------------------
		protected override void WndProc(ref Message m)
		{
			switch (m.Msg)
			{
				//===================================================
				// 転送完了メッセージ
				//===================================================
				case (int)CdioConst.DIO_DMM_STOP:
					//-----------------------------------
					// デバイスIDにより対象デバイスを判別
					//-----------------------------------
					//-----------------------------------
					// マスター
					//-----------------------------------
					if(m.WParam.ToInt32() == M_Id)
					{
						txtMStatus.Text = "Stopped!";
						goto case (int)CdioConst.DIO_DMM_COUNT;
					}
					//-----------------------------------
					// スレーブ
					//-----------------------------------
					else
					{
						txtSStatus.Text = "Stopped!";
						goto case (int)CdioConst.DIO_DMM_COUNT;
					}				
				
				//===================================================
				// 指定個数転送完了メッセージ
				//===================================================
				case (int)CdioConst.DIO_DMM_COUNT:
					int		ret;
					uint	count, carry;
					string	str;
					//-----------------------------------
					// デバイスIDにより対象デバイスを判別
					//-----------------------------------
					//-----------------------------------
					// マスター
					//-----------------------------------
					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;
					}
					//-----------------------------------
					// スレーブ
					//-----------------------------------
					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);
		}
	}
}
