﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Globalization;
using CdioCs;

namespace Sync
{
	/// <summary>
	/// MCondition の概要の説明です。
	/// </summary>
	public class MCondition : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label14;
		private System.Windows.Forms.Label label15;
		private System.Windows.Forms.Label label13;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.RadioButton rdoRing;
		private System.Windows.Forms.RadioButton rdoOnce;
		private System.Windows.Forms.TextBox txtInternalClock;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.ComboBox cboStopTrg;
		private System.Windows.Forms.TextBox txtPtnMask;
		private System.Windows.Forms.ComboBox cboClockTrg;
		private System.Windows.Forms.TextBox txtStopNum;
		private System.Windows.Forms.TextBox txtCmpPtn;
		private System.Windows.Forms.ComboBox cboStartTrg;
		private System.Windows.Forms.TextBox txtNumEvent;
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.Container components = null;

		struct StrToNum 
		{													// structure of condition and parameters
			public string	Str;
			public short	Num;
			public short	ExtSig;
			public StrToNum(string Str, short Num, short ExtSig)
			{
				this.Str = Str;
				this.Num = Num;
				this.ExtSig = ExtSig;
			}
		};
		// Start Condition
		static	StrToNum[] StartTrgTable = 
		{
			new StrToNum("Software Start",	(short)CdioConst.DIODM_START_SOFT,		(short)CdioConst.DIODM_EXT_START_SOFT_IN),
			new StrToNum("External UP",		(short)CdioConst.DIODM_START_EXT_RISE,	(short)CdioConst.DIODM_EXT_START_EXT_RISE_IN),
			new StrToNum("External Down",	(short)CdioConst.DIODM_START_EXT_FALL,	(short)CdioConst.DIODM_EXT_START_EXT_FALL_IN),
			new StrToNum("Pattern",			(short)CdioConst.DIODM_START_PATTERN,	(short)CdioConst.DIODM_EXT_START_PATTERN_IN),
			new StrToNum("End",				0,										0)
		};
		// Clock Condition
		static	StrToNum[] ClockTrgTable = 
		{
			new StrToNum("Internal Clock",	(short)CdioConst.DIODM_CLK_CLOCK,		(short)CdioConst.DIODM_EXT_CLOCK_IN),
			new StrToNum("External Clock",	(short)CdioConst.DIODM_CLK_EXT_TRG,		(short)CdioConst.DIODM_EXT_EXT_TRG_IN),
			new StrToNum("Hand Shake",		(short)CdioConst.DIODM_CLK_HANDSHAKE,	(short)CdioConst.DIODM_EXT_HANDSHAKE_IN),
			new StrToNum("End",				0,										0)
		};
		// Stop Condition
		static	StrToNum[] StopTrgTable = 
		{
			new StrToNum("Software Stop",	(short)CdioConst.DIODM_STOP_SOFT,		(short)CdioConst.DIODM_EXT_STOP_SOFT_IN),
			new StrToNum("External UP",		(short)CdioConst.DIODM_STOP_EXT_RISE,	(short)CdioConst.DIODM_EXT_STOP_EXT_RISE_IN),
			new StrToNum("External Down",	(short)CdioConst.DIODM_STOP_EXT_FALL,	(short)CdioConst.DIODM_EXT_STOP_EXT_FALL_IN),
			new StrToNum("Number",			(short)CdioConst.DIODM_STOP_NUM,		(short)CdioConst.DIODM_EXT_TRNSNUM_IN),
			new StrToNum("End",				0,										0)
		};

		Cdio				dio				= new Cdio();
		static string		szStart			= "Software Start";
		static string		szClock			= "Internal Clock";
		static string		szStop			= "Software Stop";
		static uint			Pattern			= 0x55;
		static uint			Mask			= 0xFF;
		static uint			InternalClock	= 1000;
		static uint			StopNumber		= 1000;
		public static uint	M_NumberOfData	= 500;
		public static short	M_IsRing		= (short)CdioConst.DIODM_WRITE_ONCE;

		public MCondition()
		{
			//
			// 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.label14 = new System.Windows.Forms.Label();
			this.label15 = new System.Windows.Forms.Label();
			this.label13 = new System.Windows.Forms.Label();
			this.cboStopTrg = new System.Windows.Forms.ComboBox();
			this.label10 = new System.Windows.Forms.Label();
			this.label9 = new System.Windows.Forms.Label();
			this.label12 = new System.Windows.Forms.Label();
			this.rdoRing = new System.Windows.Forms.RadioButton();
			this.rdoOnce = new System.Windows.Forms.RadioButton();
			this.txtInternalClock = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.txtPtnMask = new System.Windows.Forms.TextBox();
			this.cboClockTrg = new System.Windows.Forms.ComboBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.label5 = new System.Windows.Forms.Label();
			this.txtStopNum = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label11 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.txtCmpPtn = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.cboStartTrg = new System.Windows.Forms.ComboBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnOK = new System.Windows.Forms.Button();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.txtNumEvent = new System.Windows.Forms.TextBox();
			this.groupBox2.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// label14
			// 
			this.label14.Location = new System.Drawing.Point(8, 22);
			this.label14.Name = "label14";
			this.label14.Size = new System.Drawing.Size(89, 16);
			this.label14.TabIndex = 0;
			this.label14.Text = "転送条件：";
			// 
			// label15
			// 
			this.label15.Location = new System.Drawing.Point(104, 28);
			this.label15.Name = "label15";
			this.label15.Size = new System.Drawing.Size(96, 16);
			this.label15.TabIndex = 0;
			this.label15.Text = "データ毎に通知";
			// 
			// label13
			// 
			this.label13.Location = new System.Drawing.Point(232, 100);
			this.label13.Name = "label13";
			this.label13.Size = new System.Drawing.Size(56, 16);
			this.label13.TabIndex = 19;
			this.label13.Text = "->ExtSig3";
			// 
			// cboStopTrg
			// 
			this.cboStopTrg.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStopTrg.ItemHeight = 12;
			this.cboStopTrg.Location = new System.Drawing.Point(88, 96);
			this.cboStopTrg.Name = "cboStopTrg";
			this.cboStopTrg.Size = new System.Drawing.Size(136, 20);
			this.cboStopTrg.Sorted = true;
			this.cboStopTrg.TabIndex = 4;
			this.cboStopTrg.SelectedIndexChanged += new System.EventHandler(this.cboStopTrg_SelectedIndexChanged);
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(475, 73);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(16, 16);
			this.label10.TabIndex = 16;
			this.label10.Text = "ns";
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(472, 46);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(30, 16);
			this.label9.TabIndex = 15;
			this.label9.Text = "Hex";
			// 
			// label12
			// 
			this.label12.Location = new System.Drawing.Point(232, 73);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(56, 16);
			this.label12.TabIndex = 18;
			this.label12.Text = "->ExtSig1";
			// 
			// rdoRing
			// 
			this.rdoRing.Location = new System.Drawing.Point(168, 22);
			this.rdoRing.Name = "rdoRing";
			this.rdoRing.Size = new System.Drawing.Size(88, 16);
			this.rdoRing.TabIndex = 10;
			this.rdoRing.Text = "無限回転送";
			// 
			// rdoOnce
			// 
			this.rdoOnce.Location = new System.Drawing.Point(88, 22);
			this.rdoOnce.Name = "rdoOnce";
			this.rdoOnce.Size = new System.Drawing.Size(72, 16);
			this.rdoOnce.TabIndex = 9;
			this.rdoOnce.Text = "一回転送";
			// 
			// txtInternalClock
			// 
			this.txtInternalClock.Location = new System.Drawing.Point(376, 70);
			this.txtInternalClock.Name = "txtInternalClock";
			this.txtInternalClock.Size = new System.Drawing.Size(88, 19);
			this.txtInternalClock.TabIndex = 7;
			this.txtInternalClock.Text = "";
			this.txtInternalClock.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 100);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(73, 16);
			this.label3.TabIndex = 2;
			this.label3.Text = "ストップ条件：";
			// 
			// txtPtnMask
			// 
			this.txtPtnMask.Location = new System.Drawing.Point(376, 43);
			this.txtPtnMask.Name = "txtPtnMask";
			this.txtPtnMask.Size = new System.Drawing.Size(88, 19);
			this.txtPtnMask.TabIndex = 6;
			this.txtPtnMask.Text = "";
			this.txtPtnMask.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// cboClockTrg
			// 
			this.cboClockTrg.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboClockTrg.ItemHeight = 12;
			this.cboClockTrg.Location = new System.Drawing.Point(88, 69);
			this.cboClockTrg.Name = "cboClockTrg";
			this.cboClockTrg.Size = new System.Drawing.Size(136, 20);
			this.cboClockTrg.Sorted = true;
			this.cboClockTrg.TabIndex = 3;
			this.cboClockTrg.SelectedIndexChanged += new System.EventHandler(this.cboClockTrg_SelectedIndexChanged);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.rdoRing,
																					this.rdoOnce,
																					this.label14});
			this.groupBox2.Location = new System.Drawing.Point(8, 144);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(504, 48);
			this.groupBox2.TabIndex = 3;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "バッファ条件設定";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(472, 19);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(30, 16);
			this.label5.TabIndex = 6;
			this.label5.Text = "Hex";
			// 
			// txtStopNum
			// 
			this.txtStopNum.Location = new System.Drawing.Point(376, 97);
			this.txtStopNum.Name = "txtStopNum";
			this.txtStopNum.Size = new System.Drawing.Size(88, 19);
			this.txtStopNum.TabIndex = 8;
			this.txtStopNum.Text = "";
			this.txtStopNum.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.label13,
																					this.label12,
																					this.label11,
																					this.label10,
																					this.label9,
																					this.txtStopNum,
																					this.txtInternalClock,
																					this.txtPtnMask,
																					this.cboStopTrg,
																					this.cboClockTrg,
																					this.label8,
																					this.label7,
																					this.label6,
																					this.label5,
																					this.txtCmpPtn,
																					this.label4,
																					this.cboStartTrg,
																					this.label3,
																					this.label2,
																					this.label1});
			this.groupBox1.Location = new System.Drawing.Point(8, 7);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(504, 129);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "サンプリング条件";
			// 
			// label11
			// 
			this.label11.Location = new System.Drawing.Point(232, 19);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(56, 16);
			this.label11.TabIndex = 17;
			this.label11.Text = "->ExtSig2";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(296, 100);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(77, 16);
			this.label8.TabIndex = 9;
			this.label8.Text = "停止個数：";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(296, 73);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(77, 16);
			this.label7.TabIndex = 8;
			this.label7.Text = "内部クロック：";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(296, 46);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(77, 16);
			this.label6.TabIndex = 7;
			this.label6.Text = "パターンマスク：";
			// 
			// txtCmpPtn
			// 
			this.txtCmpPtn.Location = new System.Drawing.Point(376, 16);
			this.txtCmpPtn.Name = "txtCmpPtn";
			this.txtCmpPtn.Size = new System.Drawing.Size(88, 19);
			this.txtCmpPtn.TabIndex = 5;
			this.txtCmpPtn.Text = "";
			this.txtCmpPtn.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(296, 19);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(77, 16);
			this.label4.TabIndex = 4;
			this.label4.Text = "比較パターン：";
			// 
			// cboStartTrg
			// 
			this.cboStartTrg.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboStartTrg.ItemHeight = 12;
			this.cboStartTrg.Location = new System.Drawing.Point(88, 15);
			this.cboStartTrg.Name = "cboStartTrg";
			this.cboStartTrg.Size = new System.Drawing.Size(136, 20);
			this.cboStartTrg.Sorted = true;
			this.cboStartTrg.TabIndex = 2;
			this.cboStartTrg.SelectedIndexChanged += new System.EventHandler(this.cboStartTrg_SelectedIndexChanged);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 73);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(73, 16);
			this.label2.TabIndex = 1;
			this.label2.Text = "クロック条件：";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 19);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(73, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "スタート条件：";
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(432, 269);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(80, 24);
			this.btnCancel.TabIndex = 1;
			this.btnCancel.Text = "ｷｬﾝｾﾙ";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(338, 269);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(80, 24);
			this.btnOK.TabIndex = 0;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtNumEvent,
																					this.label15});
			this.groupBox3.Location = new System.Drawing.Point(8, 200);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(504, 56);
			this.groupBox3.TabIndex = 4;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "転送個数イベント通知";
			// 
			// txtNumEvent
			// 
			this.txtNumEvent.Location = new System.Drawing.Point(8, 24);
			this.txtNumEvent.Name = "txtNumEvent";
			this.txtNumEvent.Size = new System.Drawing.Size(88, 19);
			this.txtNumEvent.TabIndex = 11;
			this.txtNumEvent.Text = "";
			this.txtNumEvent.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			// 
			// MCondition
			// 
			this.AcceptButton = this.btnOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(520, 302);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox1,
																		  this.btnCancel,
																		  this.btnOK,
																		  this.groupBox3,
																		  this.groupBox2});
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "MCondition";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "マスターサンプリング条件設定";
			this.Load += new System.EventHandler(this.MCondition_Load);
			this.groupBox2.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		//===================================================
		//	ダイアログ初期化
		//===================================================
		private void MCondition_Load(object sender, System.EventArgs e)
		{
			//--------------------------------
			// 表示初期化
			//--------------------------------
			uint	i;
			string	strTmp;

			for (i = 0; string.Compare(StartTrgTable[i].Str, "End") != 0; i++) 
			{
				cboStartTrg.Items.Add(StartTrgTable[i].Str);
			}
			for (i = 0; string.Compare(ClockTrgTable[i].Str, "End") != 0; i++) 
			{
				cboClockTrg.Items.Add(ClockTrgTable[i].Str);
			}
			for (i = 0; string.Compare(StopTrgTable[i].Str, "End") != 0; i++) 
			{
				cboStopTrg.Items.Add(StopTrgTable[i].Str);
			}

			cboStartTrg.Text = szStart;
			cboClockTrg.Text = szClock;
			cboStopTrg.Text = szStop;

			strTmp = string.Format("{0:X}", Pattern);
			txtCmpPtn.Text = strTmp;
			strTmp = string.Format("{0:X}", Mask);
			txtPtnMask.Text = strTmp;
			strTmp = string.Format("{0:d}", InternalClock);
			txtInternalClock.Text = strTmp;
			strTmp = string.Format("{0:d}", StopNumber);
			txtStopNum.Text = strTmp;
			strTmp = string.Format("{0:d}", M_NumberOfData);
			txtNumEvent.Text = strTmp;
			if (M_IsRing == (short)CdioConst.DIODM_WRITE_ONCE) 
			{
				rdoOnce.Checked = true;
				rdoRing.Checked = false;
			} 
			else 
			{
				rdoOnce.Checked = false;
				rdoRing.Checked = true;
			}

			cboStartTrg_SelectedIndexChanged(cboStartTrg, new System.EventArgs());
			cboClockTrg_SelectedIndexChanged(cboClockTrg, new System.EventArgs());
			cboStopTrg_SelectedIndexChanged(cboStopTrg, new System.EventArgs());
		}

		//===================================================
		//	スタート条件設定変更
		//===================================================
		private void cboStartTrg_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string	szBuff = new string(' ', 100);
			//--------------------------------
			// Pattern と Maskの有効、無効を切り替え
			//--------------------------------
			szBuff = cboStartTrg.Text;
			if (string.Compare(szBuff, "Pattern") == 0) 
			{
				txtCmpPtn.Enabled = true;
				txtPtnMask.Enabled = true;
			} 
			else 
			{
				txtCmpPtn.Enabled = false;
				txtPtnMask.Enabled = false;
			}	
		}

		
		//===================================================
		//	クロック条件設定変更
		//===================================================
		private void cboClockTrg_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string	szBuff = new string(' ', 100);
			//--------------------------------
			// Internal Clockの有効、無効を切り替え
			//--------------------------------
			szBuff = cboClockTrg.Text;
			if (string.Compare(szBuff, "Internal Clock") == 0) 
			{
				txtInternalClock.Enabled = true;
			} 
			else 
			{
				txtInternalClock.Enabled = false;
			}
		}

		//===================================================
		//	ストップ条件設定変更
		//===================================================
		private void cboStopTrg_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string	szBuff = new string(' ', 100);
			//--------------------------------
			// Stop Numberの有効、無効を切り替え
			//--------------------------------
			szBuff = cboStopTrg.Text;
			if (string.Compare(szBuff, "Number") == 0) 
			{
				txtStopNum.Enabled = true;
			} 
			else 
			{
				txtStopNum.Enabled = false;
			}
		}

		//===================================================
		//	OKボタン処理
		//===================================================
		private void btnOK_Click(object sender, System.EventArgs e)
		{
			string	error_string = new string(' ', 256);
			string	szTmp = new string(' ', 100);
			short	StartTrg;
			short	ClockTrg;
			short	StopTrg;
			short	ExtSig1, ExtSig2, ExtSig3;
			short	i;
			short	Direction = (short)CdioConst.DIODM_DIR_IN;
			int		ret;
			string	strMsg;

			//--------------------------------
			// 表示データを取得
			//--------------------------------
			szStart = cboStartTrg.Text;
			szClock = cboClockTrg.Text;
			szStop = cboStopTrg.Text;
			szTmp = txtCmpPtn.Text;
			try
			{
				Pattern = uint.Parse(szTmp, NumberStyles.HexNumber);
			}
			catch (System.FormatException) 
			{
				Pattern = 0x55;
			}
			szTmp = txtPtnMask.Text;
			try
			{
				Mask = uint.Parse(szTmp, NumberStyles.HexNumber);
			}
			catch (System.FormatException) 
			{
				Mask = 0xFF;
			}
			szTmp = txtInternalClock.Text;
			try
			{
				InternalClock = uint.Parse(szTmp);
			}
			catch (System.FormatException)
			{
				InternalClock = 1000;
			}
			szTmp = txtStopNum.Text;
			try
			{
				StopNumber = uint.Parse(szTmp);
			}
			catch (System.FormatException)
			{
				StopNumber = 1000;
			}
			szTmp = txtNumEvent.Text;
			try
			{
				M_NumberOfData = uint.Parse(szTmp);
			}
			catch (System.FormatException)
			{
				M_NumberOfData = 500;
			}
			if (rdoOnce.Checked == true) 
			{
				M_IsRing = (short)CdioConst.DIODM_WRITE_ONCE;
			} 
			else 
			{
				M_IsRing = (short)CdioConst.DIODM_WRITE_RING;
			}
			//--------------------------------
			// 設定値を数値に変換
			//--------------------------------
			StartTrg	= 0;
			ExtSig2		= 0;
			for (i = 0; string.Compare(StartTrgTable[i].Str, "End") != 0; i++) 
			{
				if (string.Compare(szStart, StartTrgTable[i].Str) == 0) 
				{
					StartTrg	= StartTrgTable[i].Num;
					ExtSig2		= StartTrgTable[i].ExtSig;
					break;
				}
			}
			ClockTrg	= 0;
			ExtSig1		= 0;
			for (i = 0; string.Compare(ClockTrgTable[i].Str, "End") != 0; i++) 
			{
				if (string.Compare(szClock, ClockTrgTable[i].Str) == 0) 
				{
					ClockTrg	= ClockTrgTable[i].Num;
					ExtSig1		= ClockTrgTable[i].ExtSig;
					break;
				}
			}
			StopTrg	= 0;
			ExtSig3	= 0;
			for (i = 0; string.Compare(StopTrgTable[i].Str, "End") != 0; i++) 
			{
				if (string.Compare(szStop, StopTrgTable[i].Str) == 0) 
				{
					StopTrg	= StopTrgTable[i].Num;
					ExtSig3	= StopTrgTable[i].ExtSig;
					break;
				}
			}
			//--------------------------------
			// サンプリング条件を設定
			//--------------------------------
			// 入出力方向設定(全点入力)
			ret = dio.DmSetDirection(Sync.M_Id, (short)CdioConst.PI_32);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetDirection ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// マスター設定
			ret = dio.DmSetMaster(Sync.M_Id, ExtSig1, ExtSig2, ExtSig3, 1, 1);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetMaster ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// スタート条件設定
			ret = dio.DmSetStartTrigger(Sync.M_Id, Direction, StartTrg);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetStartTrigger ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// クロック条件設定
			ret = dio.DmSetClockTrigger(Sync.M_Id, Direction, ClockTrg);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetClockTrigger ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// ストップ条件設定
			ret = dio.DmSetStopTrigger(Sync.M_Id, Direction, StopTrg);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetStopTrigger ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// パターン/マスク
			ret = dio.DmSetStartPattern(Sync.M_Id, Pattern, Mask);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetStartPattern ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// 内部クロック値設定
			ret = dio.DmSetInternalClock(Sync.M_Id, Direction, InternalClock, (short)CdioConst.DIODM_TIM_UNIT_NS);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetInternalClock ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
			// 停止個数設定
			ret = dio.DmSetStopNumber(Sync.M_Id, Direction, StopNumber);
			if (ret != (int)CdioConst.DIO_ERR_SUCCESS) 
			{
				dio.GetErrorString(ret, out error_string);
				strMsg = string.Format("DioDmSetStopNumber ret = {0:d}: {1}", ret, error_string);
				MessageBox.Show(strMsg, this.ProductName, MessageBoxButtons.OK);
				return;
			}
	
			this.Close();
		}

		//===================================================
		//	キャンセルボタン処理
		//===================================================
		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}
