﻿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;
// Counter headder
using CcntCs;

namespace Sync
{
	/// <summary>
	/// Sync の概要の説明です。
	/// </summary>
	public class Sync : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.GroupBox groupBox3;
		public System.Windows.Forms.Label Label5;
		public System.Windows.Forms.Label Label4;
		public System.Windows.Forms.Label Label3;
		public System.Windows.Forms.Label Label2;
		public System.Windows.Forms.Label label1;
		public System.Windows.Forms.Label label8;
		public System.Windows.Forms.Label label9;
		public System.Windows.Forms.Label label10;
		private System.Windows.Forms.GroupBox groupBox5;
		public System.Windows.Forms.Button btnMExit;
		public System.Windows.Forms.Button btnMStatus;
		public System.Windows.Forms.Button btnMCount;
		public System.Windows.Forms.Button btnMData;
		public System.Windows.Forms.Button btnMStop;
		public System.Windows.Forms.Button btnMStart;
		public System.Windows.Forms.Button btnMSmpMode;
		public System.Windows.Forms.Button btnMCntMode;
		public System.Windows.Forms.Button btnMInit;
		public System.Windows.Forms.Button btnSExit;
		public System.Windows.Forms.Button btnSStatus;
		public System.Windows.Forms.Button btnSCount;
		public System.Windows.Forms.Button btnSData;
		public System.Windows.Forms.Button btnSStop;
		public System.Windows.Forms.Button btnSStart;
		public System.Windows.Forms.Button btnSSmpMode;
		public System.Windows.Forms.Button btnSCntMode;
		public System.Windows.Forms.Button btnSInit;
		public System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.TextBox txtMDeviceName;
		private System.Windows.Forms.TextBox txtSDeviceName;
		private System.Windows.Forms.GroupBox スレーブ;
		public System.Windows.Forms.TextBox txtMRet;
		public System.Windows.Forms.TextBox txtMError;
		public System.Windows.Forms.TextBox txtMStatus;
		public System.Windows.Forms.TextBox txtMCount;
		public System.Windows.Forms.TextBox txtSRet;
		public System.Windows.Forms.TextBox txtSError;
		public System.Windows.Forms.TextBox txtSStatus;
		public System.Windows.Forms.TextBox txtSCount;
		/// <summary>
		/// 必要なデザイナ変数です。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public const int	CHANNEL_NUM	= 8;
		const int			DATA_NUM	= 1000;

		public static short M_Id;						// マスターデバイスID
		public static short S_Id;						// スレーブデバイスID


		uint[ , ]			M_SampData = new uint[DATA_NUM, CHANNEL_NUM];
		uint[ , ]			S_SampData = new uint[DATA_NUM, CHANNEL_NUM];

		Ccnt				cnt			= new Ccnt();

		GCHandle			M_gCh, S_gCh;
		IntPtr				pM_SampData, pS_SampData;

		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.groupBox1 = new System.Windows.Forms.GroupBox();
			this.btnMExit = 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.btnMSmpMode = new System.Windows.Forms.Button();
			this.btnMCntMode = new System.Windows.Forms.Button();
			this.btnMInit = new System.Windows.Forms.Button();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.txtSDeviceName = new System.Windows.Forms.TextBox();
			this.txtMDeviceName = new System.Windows.Forms.TextBox();
			this.label7 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.txtMRet = 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.Label5 = new System.Windows.Forms.Label();
			this.Label4 = new System.Windows.Forms.Label();
			this.Label3 = new System.Windows.Forms.Label();
			this.Label2 = new System.Windows.Forms.Label();
			this.スレーブ = new System.Windows.Forms.GroupBox();
			this.txtSRet = 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.label1 = 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.groupBox5 = new System.Windows.Forms.GroupBox();
			this.btnSExit = 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.btnSSmpMode = new System.Windows.Forms.Button();
			this.btnSCntMode = new System.Windows.Forms.Button();
			this.btnSInit = new System.Windows.Forms.Button();
			this.btnExit = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.スレーブ.SuspendLayout();
			this.groupBox5.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnMExit,
																					this.btnMStatus,
																					this.btnMCount,
																					this.btnMData,
																					this.btnMStop,
																					this.btnMStart,
																					this.btnMSmpMode,
																					this.btnMCntMode,
																					this.btnMInit});
			this.groupBox1.Location = new System.Drawing.Point(359, 16);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(136, 305);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "マスター";
			// 
			// btnMExit
			// 
			this.btnMExit.BackColor = System.Drawing.SystemColors.Control;
			this.btnMExit.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnMExit.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnMExit.Location = new System.Drawing.Point(16, 273);
			this.btnMExit.Name = "btnMExit";
			this.btnMExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMExit.Size = new System.Drawing.Size(104, 23);
			this.btnMExit.TabIndex = 43;
			this.btnMExit.Text = "CntExit";
			this.btnMExit.Click += new System.EventHandler(this.btnMExit_Click);
			// 
			// btnMStatus
			// 
			this.btnMStatus.BackColor = System.Drawing.SystemColors.Control;
			this.btnMStatus.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnMStatus.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnMStatus.Location = new System.Drawing.Point(16, 242);
			this.btnMStatus.Name = "btnMStatus";
			this.btnMStatus.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMStatus.Size = new System.Drawing.Size(104, 23);
			this.btnMStatus.TabIndex = 42;
			this.btnMStatus.Text = "転送ステータス";
			this.btnMStatus.Click += new System.EventHandler(this.btnMStatus_Click);
			// 
			// btnMCount
			// 
			this.btnMCount.BackColor = System.Drawing.SystemColors.Control;
			this.btnMCount.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnMCount.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnMCount.Location = new System.Drawing.Point(16, 211);
			this.btnMCount.Name = "btnMCount";
			this.btnMCount.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMCount.Size = new System.Drawing.Size(104, 23);
			this.btnMCount.TabIndex = 41;
			this.btnMCount.Text = "転送カウント";
			this.btnMCount.Click += new System.EventHandler(this.btnMCount_Click);
			// 
			// btnMData
			// 
			this.btnMData.BackColor = System.Drawing.SystemColors.Control;
			this.btnMData.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnMData.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnMData.Location = new System.Drawing.Point(16, 180);
			this.btnMData.Name = "btnMData";
			this.btnMData.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMData.Size = new System.Drawing.Size(104, 23);
			this.btnMData.TabIndex = 40;
			this.btnMData.Text = "取得データ...";
			this.btnMData.Click += new System.EventHandler(this.btnMData_Click);
			// 
			// btnMStop
			// 
			this.btnMStop.BackColor = System.Drawing.SystemColors.Control;
			this.btnMStop.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnMStop.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnMStop.Location = new System.Drawing.Point(16, 149);
			this.btnMStop.Name = "btnMStop";
			this.btnMStop.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMStop.Size = new System.Drawing.Size(104, 23);
			this.btnMStop.TabIndex = 39;
			this.btnMStop.Text = "転送ストップ";
			this.btnMStop.Click += new System.EventHandler(this.btnMStop_Click);
			// 
			// btnMStart
			// 
			this.btnMStart.BackColor = System.Drawing.SystemColors.Control;
			this.btnMStart.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnMStart.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnMStart.Location = new System.Drawing.Point(16, 118);
			this.btnMStart.Name = "btnMStart";
			this.btnMStart.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMStart.Size = new System.Drawing.Size(104, 23);
			this.btnMStart.TabIndex = 38;
			this.btnMStart.Text = "転送スタート";
			this.btnMStart.Click += new System.EventHandler(this.btnMStart_Click);
			// 
			// btnMSmpMode
			// 
			this.btnMSmpMode.BackColor = System.Drawing.SystemColors.Control;
			this.btnMSmpMode.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnMSmpMode.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnMSmpMode.Location = new System.Drawing.Point(16, 87);
			this.btnMSmpMode.Name = "btnMSmpMode";
			this.btnMSmpMode.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMSmpMode.Size = new System.Drawing.Size(104, 23);
			this.btnMSmpMode.TabIndex = 37;
			this.btnMSmpMode.Text = "サンプリングモード...";
			this.btnMSmpMode.Click += new System.EventHandler(this.btnMSmpMode_Click);
			// 
			// btnMCntMode
			// 
			this.btnMCntMode.BackColor = System.Drawing.SystemColors.Control;
			this.btnMCntMode.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnMCntMode.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnMCntMode.Location = new System.Drawing.Point(16, 56);
			this.btnMCntMode.Name = "btnMCntMode";
			this.btnMCntMode.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMCntMode.Size = new System.Drawing.Size(104, 23);
			this.btnMCntMode.TabIndex = 36;
			this.btnMCntMode.Text = "モード";
			this.btnMCntMode.Click += new System.EventHandler(this.btnMCntMode_Click);
			// 
			// btnMInit
			// 
			this.btnMInit.BackColor = System.Drawing.SystemColors.Control;
			this.btnMInit.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnMInit.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnMInit.Location = new System.Drawing.Point(16, 25);
			this.btnMInit.Name = "btnMInit";
			this.btnMInit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMInit.Size = new System.Drawing.Size(104, 23);
			this.btnMInit.TabIndex = 35;
			this.btnMInit.Text = "CntInit";
			this.btnMInit.Click += new System.EventHandler(this.btnMInit_Click);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtSDeviceName,
																					this.txtMDeviceName,
																					this.label7,
																					this.label6});
			this.groupBox2.Location = new System.Drawing.Point(8, 16);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(344, 48);
			this.groupBox2.TabIndex = 3;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "デバイス名";
			// 
			// txtSDeviceName
			// 
			this.txtSDeviceName.Location = new System.Drawing.Point(258, 21);
			this.txtSDeviceName.Name = "txtSDeviceName";
			this.txtSDeviceName.Size = new System.Drawing.Size(72, 19);
			this.txtSDeviceName.TabIndex = 3;
			this.txtSDeviceName.Text = "";
			// 
			// txtMDeviceName
			// 
			this.txtMDeviceName.Location = new System.Drawing.Point(64, 21);
			this.txtMDeviceName.Name = "txtMDeviceName";
			this.txtMDeviceName.Size = new System.Drawing.Size(88, 19);
			this.txtMDeviceName.TabIndex = 2;
			this.txtMDeviceName.Text = "";
			// 
			// label7
			// 
			this.label7.AutoSize = true;
			this.label7.Location = new System.Drawing.Point(202, 24);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(49, 12);
			this.label7.TabIndex = 1;
			this.label7.Text = "スレーブ：";
			// 
			// label6
			// 
			this.label6.AutoSize = true;
			this.label6.Location = new System.Drawing.Point(8, 24);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(48, 12);
			this.label6.TabIndex = 0;
			this.label6.Text = "マスター：";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.txtMRet,
																					this.txtMError,
																					this.txtMStatus,
																					this.txtMCount,
																					this.Label5,
																					this.Label4,
																					this.Label3,
																					this.Label2});
			this.groupBox3.Location = new System.Drawing.Point(8, 71);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(344, 121);
			this.groupBox3.TabIndex = 4;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "マスター";
			// 
			// txtMRet
			// 
			this.txtMRet.AcceptsReturn = true;
			this.txtMRet.AutoSize = false;
			this.txtMRet.BackColor = System.Drawing.SystemColors.Window;
			this.txtMRet.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.txtMRet.ForeColor = System.Drawing.SystemColors.WindowText;
			this.txtMRet.Location = new System.Drawing.Point(65, 93);
			this.txtMRet.MaxLength = 0;
			this.txtMRet.Name = "txtMRet";
			this.txtMRet.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtMRet.Size = new System.Drawing.Size(264, 18);
			this.txtMRet.TabIndex = 48;
			this.txtMRet.Text = "";
			// 
			// txtMError
			// 
			this.txtMError.AcceptsReturn = true;
			this.txtMError.AutoSize = false;
			this.txtMError.BackColor = System.Drawing.SystemColors.Window;
			this.txtMError.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.txtMError.ForeColor = System.Drawing.SystemColors.WindowText;
			this.txtMError.Location = new System.Drawing.Point(65, 69);
			this.txtMError.MaxLength = 0;
			this.txtMError.Name = "txtMError";
			this.txtMError.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtMError.Size = new System.Drawing.Size(264, 18);
			this.txtMError.TabIndex = 47;
			this.txtMError.Text = "";
			// 
			// txtMStatus
			// 
			this.txtMStatus.AcceptsReturn = true;
			this.txtMStatus.AutoSize = false;
			this.txtMStatus.BackColor = System.Drawing.SystemColors.Window;
			this.txtMStatus.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.txtMStatus.ForeColor = System.Drawing.SystemColors.WindowText;
			this.txtMStatus.Location = new System.Drawing.Point(65, 45);
			this.txtMStatus.MaxLength = 0;
			this.txtMStatus.Name = "txtMStatus";
			this.txtMStatus.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtMStatus.Size = new System.Drawing.Size(264, 18);
			this.txtMStatus.TabIndex = 46;
			this.txtMStatus.Text = "";
			// 
			// txtMCount
			// 
			this.txtMCount.AcceptsReturn = true;
			this.txtMCount.AutoSize = false;
			this.txtMCount.BackColor = System.Drawing.SystemColors.Window;
			this.txtMCount.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.txtMCount.ForeColor = System.Drawing.SystemColors.WindowText;
			this.txtMCount.Location = new System.Drawing.Point(65, 21);
			this.txtMCount.MaxLength = 0;
			this.txtMCount.Name = "txtMCount";
			this.txtMCount.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtMCount.Size = new System.Drawing.Size(264, 18);
			this.txtMCount.TabIndex = 45;
			this.txtMCount.Text = "";
			// 
			// Label5
			// 
			this.Label5.AutoSize = true;
			this.Label5.BackColor = System.Drawing.SystemColors.Control;
			this.Label5.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label5.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label5.Location = new System.Drawing.Point(8, 96);
			this.Label5.Name = "Label5";
			this.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label5.Size = new System.Drawing.Size(43, 12);
			this.Label5.TabIndex = 52;
			this.Label5.Text = "戻り値：";
			// 
			// Label4
			// 
			this.Label4.AutoSize = true;
			this.Label4.BackColor = System.Drawing.SystemColors.Control;
			this.Label4.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label4.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label4.Location = new System.Drawing.Point(8, 72);
			this.Label4.Name = "Label4";
			this.Label4.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label4.Size = new System.Drawing.Size(39, 12);
			this.Label4.TabIndex = 51;
			this.Label4.Text = "エラー：";
			// 
			// Label3
			// 
			this.Label3.AutoSize = true;
			this.Label3.BackColor = System.Drawing.SystemColors.Control;
			this.Label3.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label3.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label3.Location = new System.Drawing.Point(8, 48);
			this.Label3.Name = "Label3";
			this.Label3.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label3.Size = new System.Drawing.Size(58, 12);
			this.Label3.TabIndex = 50;
			this.Label3.Text = "ステータス：";
			// 
			// Label2
			// 
			this.Label2.AutoSize = true;
			this.Label2.BackColor = System.Drawing.SystemColors.Control;
			this.Label2.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label2.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label2.Location = new System.Drawing.Point(8, 24);
			this.Label2.Name = "Label2";
			this.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label2.Size = new System.Drawing.Size(47, 12);
			this.Label2.TabIndex = 49;
			this.Label2.Text = "カウント：";
			// 
			// スレーブ
			// 
			this.スレーブ.Controls.AddRange(new System.Windows.Forms.Control[] {
																			   this.txtSRet,
																			   this.txtSError,
																			   this.txtSStatus,
																			   this.txtSCount,
																			   this.label1,
																			   this.label8,
																			   this.label9,
																			   this.label10});
			this.スレーブ.Location = new System.Drawing.Point(8, 199);
			this.スレーブ.Name = "スレーブ";
			this.スレーブ.Size = new System.Drawing.Size(344, 122);
			this.スレーブ.TabIndex = 5;
			this.スレーブ.TabStop = false;
			this.スレーブ.Text = "スレーブ";
			// 
			// txtSRet
			// 
			this.txtSRet.AcceptsReturn = true;
			this.txtSRet.AutoSize = false;
			this.txtSRet.BackColor = System.Drawing.SystemColors.Window;
			this.txtSRet.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.txtSRet.ForeColor = System.Drawing.SystemColors.WindowText;
			this.txtSRet.Location = new System.Drawing.Point(65, 93);
			this.txtSRet.MaxLength = 0;
			this.txtSRet.Name = "txtSRet";
			this.txtSRet.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtSRet.Size = new System.Drawing.Size(264, 18);
			this.txtSRet.TabIndex = 48;
			this.txtSRet.Text = "";
			// 
			// txtSError
			// 
			this.txtSError.AcceptsReturn = true;
			this.txtSError.AutoSize = false;
			this.txtSError.BackColor = System.Drawing.SystemColors.Window;
			this.txtSError.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.txtSError.ForeColor = System.Drawing.SystemColors.WindowText;
			this.txtSError.Location = new System.Drawing.Point(65, 69);
			this.txtSError.MaxLength = 0;
			this.txtSError.Name = "txtSError";
			this.txtSError.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtSError.Size = new System.Drawing.Size(264, 18);
			this.txtSError.TabIndex = 47;
			this.txtSError.Text = "";
			// 
			// txtSStatus
			// 
			this.txtSStatus.AcceptsReturn = true;
			this.txtSStatus.AutoSize = false;
			this.txtSStatus.BackColor = System.Drawing.SystemColors.Window;
			this.txtSStatus.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.txtSStatus.ForeColor = System.Drawing.SystemColors.WindowText;
			this.txtSStatus.Location = new System.Drawing.Point(65, 45);
			this.txtSStatus.MaxLength = 0;
			this.txtSStatus.Name = "txtSStatus";
			this.txtSStatus.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtSStatus.Size = new System.Drawing.Size(264, 18);
			this.txtSStatus.TabIndex = 46;
			this.txtSStatus.Text = "";
			// 
			// txtSCount
			// 
			this.txtSCount.AcceptsReturn = true;
			this.txtSCount.AutoSize = false;
			this.txtSCount.BackColor = System.Drawing.SystemColors.Window;
			this.txtSCount.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.txtSCount.ForeColor = System.Drawing.SystemColors.WindowText;
			this.txtSCount.Location = new System.Drawing.Point(65, 21);
			this.txtSCount.MaxLength = 0;
			this.txtSCount.Name = "txtSCount";
			this.txtSCount.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtSCount.Size = new System.Drawing.Size(264, 18);
			this.txtSCount.TabIndex = 45;
			this.txtSCount.Text = "";
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.BackColor = System.Drawing.SystemColors.Control;
			this.label1.Cursor = System.Windows.Forms.Cursors.Default;
			this.label1.ForeColor = System.Drawing.SystemColors.ControlText;
			this.label1.Location = new System.Drawing.Point(8, 96);
			this.label1.Name = "label1";
			this.label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.label1.Size = new System.Drawing.Size(43, 12);
			this.label1.TabIndex = 52;
			this.label1.Text = "戻り値：";
			// 
			// label8
			// 
			this.label8.AutoSize = true;
			this.label8.BackColor = System.Drawing.SystemColors.Control;
			this.label8.Cursor = System.Windows.Forms.Cursors.Default;
			this.label8.ForeColor = System.Drawing.SystemColors.ControlText;
			this.label8.Location = new System.Drawing.Point(8, 72);
			this.label8.Name = "label8";
			this.label8.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.label8.Size = new System.Drawing.Size(39, 12);
			this.label8.TabIndex = 51;
			this.label8.Text = "エラー：";
			// 
			// label9
			// 
			this.label9.AutoSize = true;
			this.label9.BackColor = System.Drawing.SystemColors.Control;
			this.label9.Cursor = System.Windows.Forms.Cursors.Default;
			this.label9.ForeColor = System.Drawing.SystemColors.ControlText;
			this.label9.Location = new System.Drawing.Point(8, 48);
			this.label9.Name = "label9";
			this.label9.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.label9.Size = new System.Drawing.Size(58, 12);
			this.label9.TabIndex = 50;
			this.label9.Text = "ステータス：";
			// 
			// label10
			// 
			this.label10.AutoSize = true;
			this.label10.BackColor = System.Drawing.SystemColors.Control;
			this.label10.Cursor = System.Windows.Forms.Cursors.Default;
			this.label10.ForeColor = System.Drawing.SystemColors.ControlText;
			this.label10.Location = new System.Drawing.Point(8, 24);
			this.label10.Name = "label10";
			this.label10.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.label10.Size = new System.Drawing.Size(47, 12);
			this.label10.TabIndex = 49;
			this.label10.Text = "カウント：";
			// 
			// groupBox5
			// 
			this.groupBox5.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.btnSExit,
																					this.btnSStatus,
																					this.btnSCount,
																					this.btnSData,
																					this.btnSStop,
																					this.btnSStart,
																					this.btnSSmpMode,
																					this.btnSCntMode,
																					this.btnSInit});
			this.groupBox5.Location = new System.Drawing.Point(502, 16);
			this.groupBox5.Name = "groupBox5";
			this.groupBox5.Size = new System.Drawing.Size(136, 305);
			this.groupBox5.TabIndex = 1;
			this.groupBox5.TabStop = false;
			this.groupBox5.Text = "スレーブ";
			// 
			// btnSExit
			// 
			this.btnSExit.BackColor = System.Drawing.SystemColors.Control;
			this.btnSExit.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnSExit.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnSExit.Location = new System.Drawing.Point(16, 273);
			this.btnSExit.Name = "btnSExit";
			this.btnSExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSExit.Size = new System.Drawing.Size(104, 23);
			this.btnSExit.TabIndex = 43;
			this.btnSExit.Text = "CntExit";
			this.btnSExit.Click += new System.EventHandler(this.btnSExit_Click);
			// 
			// btnSStatus
			// 
			this.btnSStatus.BackColor = System.Drawing.SystemColors.Control;
			this.btnSStatus.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnSStatus.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnSStatus.Location = new System.Drawing.Point(16, 242);
			this.btnSStatus.Name = "btnSStatus";
			this.btnSStatus.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSStatus.Size = new System.Drawing.Size(104, 23);
			this.btnSStatus.TabIndex = 42;
			this.btnSStatus.Text = "転送ステータス";
			this.btnSStatus.Click += new System.EventHandler(this.btnSStatus_Click);
			// 
			// btnSCount
			// 
			this.btnSCount.BackColor = System.Drawing.SystemColors.Control;
			this.btnSCount.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnSCount.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnSCount.Location = new System.Drawing.Point(16, 211);
			this.btnSCount.Name = "btnSCount";
			this.btnSCount.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSCount.Size = new System.Drawing.Size(104, 23);
			this.btnSCount.TabIndex = 41;
			this.btnSCount.Text = "転送カウント";
			this.btnSCount.Click += new System.EventHandler(this.btnSCount_Click);
			// 
			// btnSData
			// 
			this.btnSData.BackColor = System.Drawing.SystemColors.Control;
			this.btnSData.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnSData.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnSData.Location = new System.Drawing.Point(16, 180);
			this.btnSData.Name = "btnSData";
			this.btnSData.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSData.Size = new System.Drawing.Size(104, 23);
			this.btnSData.TabIndex = 40;
			this.btnSData.Text = "取得データ...";
			this.btnSData.Click += new System.EventHandler(this.btnSData_Click);
			// 
			// btnSStop
			// 
			this.btnSStop.BackColor = System.Drawing.SystemColors.Control;
			this.btnSStop.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnSStop.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnSStop.Location = new System.Drawing.Point(16, 149);
			this.btnSStop.Name = "btnSStop";
			this.btnSStop.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSStop.Size = new System.Drawing.Size(104, 23);
			this.btnSStop.TabIndex = 39;
			this.btnSStop.Text = "転送ストップ";
			this.btnSStop.Click += new System.EventHandler(this.btnSStop_Click);
			// 
			// btnSStart
			// 
			this.btnSStart.BackColor = System.Drawing.SystemColors.Control;
			this.btnSStart.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnSStart.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnSStart.Location = new System.Drawing.Point(16, 118);
			this.btnSStart.Name = "btnSStart";
			this.btnSStart.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSStart.Size = new System.Drawing.Size(104, 23);
			this.btnSStart.TabIndex = 38;
			this.btnSStart.Text = "転送スタート";
			this.btnSStart.Click += new System.EventHandler(this.btnSStart_Click);
			// 
			// btnSSmpMode
			// 
			this.btnSSmpMode.BackColor = System.Drawing.SystemColors.Control;
			this.btnSSmpMode.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnSSmpMode.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnSSmpMode.Location = new System.Drawing.Point(16, 87);
			this.btnSSmpMode.Name = "btnSSmpMode";
			this.btnSSmpMode.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSSmpMode.Size = new System.Drawing.Size(104, 23);
			this.btnSSmpMode.TabIndex = 37;
			this.btnSSmpMode.Text = "サンプリングモード...";
			this.btnSSmpMode.Click += new System.EventHandler(this.btnSSmpMode_Click);
			// 
			// btnSCntMode
			// 
			this.btnSCntMode.BackColor = System.Drawing.SystemColors.Control;
			this.btnSCntMode.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnSCntMode.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnSCntMode.Location = new System.Drawing.Point(16, 56);
			this.btnSCntMode.Name = "btnSCntMode";
			this.btnSCntMode.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSCntMode.Size = new System.Drawing.Size(104, 23);
			this.btnSCntMode.TabIndex = 36;
			this.btnSCntMode.Text = "モード";
			this.btnSCntMode.Click += new System.EventHandler(this.btnSCntMode_Click);
			// 
			// btnSInit
			// 
			this.btnSInit.BackColor = System.Drawing.SystemColors.Control;
			this.btnSInit.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnSInit.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnSInit.Location = new System.Drawing.Point(16, 25);
			this.btnSInit.Name = "btnSInit";
			this.btnSInit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSInit.Size = new System.Drawing.Size(104, 23);
			this.btnSInit.TabIndex = 35;
			this.btnSInit.Text = "CntInit";
			this.btnSInit.Click += new System.EventHandler(this.btnSInit_Click);
			// 
			// btnExit
			// 
			this.btnExit.BackColor = System.Drawing.SystemColors.Control;
			this.btnExit.Cursor = System.Windows.Forms.Cursors.Default;
			this.btnExit.ForeColor = System.Drawing.SystemColors.ControlText;
			this.btnExit.Location = new System.Drawing.Point(448, 328);
			this.btnExit.Name = "btnExit";
			this.btnExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnExit.Size = new System.Drawing.Size(104, 23);
			this.btnExit.TabIndex = 2;
			this.btnExit.Text = "Exit";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// Sync
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
			this.ClientSize = new System.Drawing.Size(648, 358);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnExit,
																		  this.groupBox5,
																		  this.スレーブ,
																		  this.groupBox3,
																		  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.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.スレーブ.ResumeLayout(false);
			this.groupBox5.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 = "CNT000";
			txtSDeviceName.Text = "CNT001";

			//-----------------------------------
			// PinnedハンドルをM_SampDataとS_SampDataに分配する
			//-----------------------------------
			if (M_gCh.IsAllocated == false)
			{
				M_gCh = GCHandle.Alloc(M_SampData, GCHandleType.Pinned);
			}
			if (S_gCh.IsAllocated == false)
			{
				S_gCh = GCHandle.Alloc(S_SampData, GCHandleType.Pinned);
			}
		}

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

			//-----------------------------------
			// GCHandleの釈放
			//-----------------------------------
			if (M_gCh.IsAllocated == true)
			{
				M_gCh.Free();
			}
			if (S_gCh.IsAllocated == true)
			{
				S_gCh.Free();
			}

			//-----------------------------------
			//	終了処理
			//-----------------------------------
			ret = cnt.Exit(M_Id);
			ret = cnt.Exit(S_Id);
		}

		//===================================================
		//	マスター CntInit()
		//===================================================
		private void btnMInit_Click(object sender, System.EventArgs e)
		{
			string	str;
			string	buf = new string(' ', 100);
			string	error_string = new string(' ', 256);
			int		ret;

			//-----------------------------------
			//	デバイス名取得
			//-----------------------------------
			buf = txtMDeviceName.Text;
	
			//-----------------------------------
			//	初期化処理
			//-----------------------------------
			ret = cnt.Init(buf, out M_Id);
			cnt.GetErrorString(ret, out error_string);
			str = string.Format("CntInit ret = {0:d}: {1}", ret, error_string);
			txtMRet.Text = str;
		}

		//===================================================
		//	スレーブ CntInit()
		//===================================================
		private void btnSInit_Click(object sender, System.EventArgs e)
		{
			string	str;
			string	buf = new string(' ', 100);
			string	error_string = new string(' ', 256);
			int		ret;

			//-----------------------------------
			//	デバイス名取得
			//-----------------------------------
			buf = txtSDeviceName.Text;
	
			//-----------------------------------
			//	初期化処理
			//-----------------------------------
			ret = cnt.Init(buf, out S_Id);
			cnt.GetErrorString(ret, out error_string);
			str = string.Format("CntInit ret = {0:d}: {1}", ret, error_string);
			txtSRet.Text = str;
		}

		//===================================================
		//	マスター 条件設定
		//===================================================
		private void btnMCntMode_Click(object sender, System.EventArgs e)
		{
			ModeDlg dlg = new ModeDlg();

			//-----------------------------------
			//	条件設定ダイアログを開く
			//-----------------------------------
			dlg.Id = M_Id;
			dlg.ShowDialog();
			dlg.Dispose();
		}

		//===================================================
		//	スレーブ 条件設定
		//===================================================
		private void btnSCntMode_Click(object sender, System.EventArgs e)
		{
			ModeDlg dlg = new ModeDlg();

			//-----------------------------------
			//	条件設定ダイアログを開く
			//-----------------------------------
			dlg.Id = S_Id;
			dlg.ShowDialog();
			dlg.Dispose();
		}

		//===================================================
		//	マスター サンプリングモード設定
		//===================================================
		private void btnMSmpMode_Click(object sender, System.EventArgs e)
		{
			MSampMode	dlg = new MSampMode();
			dlg.ShowDialog();
			dlg.Dispose();
		}

		//===================================================
		//	スレーブ サンプリングモード設定
		//===================================================
		private void btnSSmpMode_Click(object sender, System.EventArgs e)
		{
			SSampMode	dlg = new SSampMode();
			dlg.ShowDialog();
			dlg.Dispose();
		}

		//===================================================
		//	マスター 開始
		//===================================================
		private void btnMStart_Click(object sender, System.EventArgs e)
		{
			int		ret;
			string	csMsg;
			short[]	ChNo = new short[CHANNEL_NUM];
			short	i;
			string	error_string = new string(' ', 256);
			string	str;

			//-----------------------------------
			// 通知設定
			//-----------------------------------
			ret = cnt.NotifySamplingStop(M_Id, this.Handle);
			if (ret != 0) 
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntNotifySamplingStop ret = {0:d}: {1}", ret, error_string);
				txtMRet.Text =str;
				return;
			}

			ret = cnt.NotifySamplingCount(M_Id, (int)MSampMode.M_NotifyCount, this.Handle);
			if (ret != 0) 
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntNotifySamplingCount ret = {0:d}: {1}", ret, error_string);
				txtMRet.Text = str;
				return;
			}

			//-----------------------------------
			// FIFOリセット
			//-----------------------------------
			ret = cnt.SamplingFifoReset(M_Id);
			if (ret != 0) 
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntSamplingFifoReset ret = {0:d}: {1}", ret, error_string);
				txtMRet.Text = str;
				return;
			}

			//-----------------------------------
			// バッファセット
			//-----------------------------------
			Array.Clear(M_SampData, 0, M_SampData.Length);
			//-----------------------------------
			// Pinnedハンドルでオブジェクトのアドレスを取得
			//-----------------------------------
			pM_SampData = M_gCh.AddrOfPinnedObject();
			ret = cnt.SamplingSetBuffer(M_Id, pM_SampData, CHANNEL_NUM, DATA_NUM, MSampMode.M_IsRing);
			if (ret != 0) 
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntSamplingSetBuffer ret = {0:d}: {1}", ret, error_string);
				txtMRet.Text = str;
				return;
			}
			//-----------------------------------
			// カウンタ開始
			//-----------------------------------
			for (i=0; i<CHANNEL_NUM; i++) 
			{
				ChNo[i] = i;
			}
			ret = cnt.StartCount(M_Id, ChNo, CHANNEL_NUM);
			if (ret != 0) 
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntStartCount ret = {0:d}: {1}", ret, error_string);
				txtMRet.Text = str;
				return;
			}

			//-----------------------------------
			// サンプリング開始
			//-----------------------------------
			ret = cnt.SamplingStart(M_Id);
			csMsg = string.Format("CntStartSampling : RetCode = {0:X}H", ret);
			txtMRet.Text = csMsg;
			txtMCount.Text = "";
			txtMStatus.Text = "";
			txtMError.Text = "";
		}

		//===================================================
		//	スレーブ 開始
		//===================================================
		private void btnSStart_Click(object sender, System.EventArgs e)
		{
			int		ret;
			string	csMsg;
			short[]	ChNo = new short[CHANNEL_NUM];
			short	i;
			string	error_string = new string(' ', 256);
			string	str;

			//-----------------------------------
			// 通知設定
			//-----------------------------------
			ret = cnt.NotifySamplingStop(S_Id, this.Handle);
			if (ret != 0) 
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntNotifySamplingStop ret = {0:d}: {1}", ret, error_string);
				txtSRet.Text = str;
				return;
			}
			ret = cnt.NotifySamplingCount(S_Id, (int)SSampMode.S_NotifyCount, this.Handle);
			if (ret != 0) 
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntNotifySamplingCount ret = {0:d}: {1}", ret, error_string);
				txtSRet.Text = str;
				return;
			}

			//-----------------------------------
			// Reset FIFO
			//-----------------------------------
			ret = cnt.SamplingFifoReset(S_Id);
			if (ret != 0) 
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntSamplingFifoReset ret = {0:d}: {1}", ret, error_string);
				txtMRet.Text = str;
				return;
			}

			//-----------------------------------
			// バッファセット
			//-----------------------------------
			Array.Clear(S_SampData, 0, M_SampData.Length);
			//-----------------------------------
			// Pinnedハンドルでオブジェクトのアドレスを取得
			//-----------------------------------
			pS_SampData = S_gCh.AddrOfPinnedObject();
			ret = cnt.SamplingSetBuffer(S_Id, pS_SampData, CHANNEL_NUM, DATA_NUM, SSampMode.S_IsRing);
			if (ret != 0) 
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntSamplingSetBuffer ret = {0:d}: {1}", ret, error_string);
				txtSRet.Text = str;
				return;
			}
			//-----------------------------------
			// カウンタ開始
			//-----------------------------------
			for (i=0; i<CHANNEL_NUM; i++) 
			{
				ChNo[i] = i;
			}
			ret =cnt.StartCount(S_Id, ChNo, CHANNEL_NUM);
			if (ret != 0) 
			{
				cnt.GetErrorString(ret, out error_string);
				str = string.Format("CntStartCount ret = {0:d}: {1}", ret, error_string);
				txtSRet.Text = str;
				return;
			}
			//-----------------------------------
			// サンプリング開始
			//-----------------------------------
			ret = cnt.SamplingStart(S_Id);
			csMsg = string.Format("CntStartSampling : RetCode = {0:X}H", ret);
			txtSRet.Text = csMsg;
			txtSCount.Text = "";
			txtSStatus.Text = "";
			txtSError.Text = "";
		}

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

			ret = cnt.SamplingStop(M_Id);
			cnt.GetErrorString(ret, out error_string);
			str = string.Format("CntSamplingStop ret = {0:d}: {1}", ret, error_string);
			txtMRet.Text = str;
		}

		//===================================================
		//	スレーブ 停止
		//===================================================
		private void btnSStop_Click(object sender, System.EventArgs e)
		{
			int		ret;
			string	csMsg;

			ret = cnt.SamplingStop(S_Id);
			csMsg = string.Format("CntStopSampling : Ret = {0:X}H", ret);
			txtSRet.Text = csMsg;
		}

		//===================================================
		//	マスター データ取得
		//===================================================
		private void btnMData_Click(object sender, System.EventArgs e)
		{
			uint		i, j;
			TextWriter 	fp;
			string		szFileName = "Sampling1.txt";
			string		szLine = new string(' ', 500);
			string		szTmp = new string(' ', 100);
			 
			//-----------------------------------
			// ファイルオープン
			//-----------------------------------
			try
			{
				fp = File.CreateText(szFileName);
			}
			catch (System.IO.IOException)
			{
				txtMRet.Text = "fopen : file open error.";
				return;
			}	
			//-----------------------------------
			// ファイルへデータの書き込み
			//-----------------------------------
			for (i=0; i<DATA_NUM; i++) {
				szLine = string.Format("{0:d8} : ", i);
				for (j=0; j<CHANNEL_NUM; j++) {
					szTmp = string.Format("{0:X8},", M_SampData[i, j]);
					szLine += szTmp;
				}
				fp.WriteLine(szLine);
			}
			//-----------------------------------
			// ファイルクローズ
			//-----------------------------------
			fp.Close();

			//-----------------------------------
			// メモ帳
			//-----------------------------------
			Process Pro = Process.Start("notepad.exe", szFileName);
		}

		//===================================================
		//	スレーブ データ取得
		//===================================================
		private void btnSData_Click(object sender, System.EventArgs e)
		{
			uint		i, j;
			TextWriter 	fp;
			string		szFileName = "Sampling2.txt";
			string		szLine = new string(' ', 500);
			string		szTmp = new string(' ', 100);
			
			//-----------------------------------
			// ファイルオープン
			//-----------------------------------
			try
			{
				fp = File.CreateText(szFileName);
			}
			catch (System.IO.IOException)
			{
				txtSRet.Text = "fopen : file open error.";
				return;
			}	
			//-----------------------------------
			// ファイルへデータの書き込み
			//-----------------------------------
			for (i=0; i<DATA_NUM; i++) {
				szLine = string.Format("{0:d8} : ", i);
				for (j=0; j<CHANNEL_NUM; j++) {
					szTmp = string.Format("{0:X8},", S_SampData[i, j]);
					szLine += szTmp;
				}
				fp.WriteLine(szLine);
			}

			//-----------------------------------
			// ファイルクローズ
			//-----------------------------------
			fp.Close();

			//-----------------------------------
			// メモ帳
			//-----------------------------------
			Process Pro = Process.Start("notepad.exe", szFileName);
		}

		//===================================================
		//	マスター カウント値取得
		//===================================================
		private void btnMCount_Click(object sender, System.EventArgs e)
		{
			int		ret;
			uint	Count;
			uint	ScanCarry;
			string	csMsg;

			//-----------------------------------
			// カウント値取得
			//-----------------------------------
			ret = cnt.GetSamplingCount(M_Id, out Count, out ScanCarry);
			csMsg = string.Format("CntGetSamplingCount : Ret = {0:X}H", ret);
			txtMRet.Text = csMsg;
			csMsg = Count.ToString();
			txtMCount.Text = csMsg;
		}

		//===================================================
		//	スレーブ カウント値取得
		//===================================================
		private void btnSCount_Click(object sender, System.EventArgs e)
		{
			int		ret;
			uint	Count;
			uint	ScanCarry;
			string	csMsg;

			//-----------------------------------
			// カウント値取得
			//-----------------------------------
			ret = cnt.GetSamplingCount(S_Id, out Count, out ScanCarry);
			csMsg = string.Format("CntGetSamplingCount : Ret = {0:X}H", ret);
			txtSRet.Text = csMsg;
			csMsg = Count.ToString();
			txtSCount.Text = csMsg;
		}

		//===================================================
		//	マスター ステータス情報取得
		//===================================================
		private void btnMStatus_Click(object sender, System.EventArgs e)
		{
			int		ret;
			uint	Status, Err;
			string	csMsg;

			//-----------------------------------
			// ステータス情報取得
			//-----------------------------------
			ret = cnt.GetSamplingStatus(M_Id, out Status, out Err);
			csMsg = string.Format("CntGetSamplingStatus : Ret = {0:X}H", ret);
			txtMRet.Text = csMsg;
			// Status
			csMsg = "";
			if ((Status & (uint)CcntConst.CNTS_STATUS_BMSTOP) != 0)		csMsg += "BmStop,";
			if ((Status & (uint)CcntConst.CNTS_STATUS_CNTSTART) != 0)	csMsg += "CntStart,";
			if ((Status & (uint)CcntConst.CNTS_STATUS_CNTSTOP) != 0)	csMsg += "CntStop,";
			if ((Status & (uint)CcntConst.CNTS_STATUS_TRGIN) != 0)		csMsg += "TrgIn,";
			if ((Status & (uint)CcntConst.CNTS_STATUS_OVERRUN) != 0)	csMsg += "Overrun";
			txtMStatus.Text = csMsg;
			// Error
			csMsg = "";
			if ((Err & (uint)CcntConst.CNTS_STATUS_FIFOFULL) != 0)		csMsg += "FifoFull,";
			if ((Err & (uint)CcntConst.CNTS_STATUS_SGOVERIN) != 0)		csMsg += "S/GOverIn,";
			if ((Err & (uint)CcntConst.CNTS_STATUS_TRGERR) != 0)		csMsg += "TrgErr,";
			if ((Err & (uint)CcntConst.CNTS_STATUS_CLKERR) != 0)		csMsg += "ClkErr,";
			if ((Err & (uint)CcntConst.CNTS_STATUS_SLAVEHALT) != 0)		csMsg += "SlaveHalt,";
			if ((Err & (uint)CcntConst.CNTS_STATUS_MASTERHALT) != 0)	csMsg += "MasterHalt";
			txtMError.Text = csMsg;
		}

		//===================================================
		//	スレーブ ステータス情報取得
		//===================================================
		private void btnSStatus_Click(object sender, System.EventArgs e)
		{
			int		ret;
			uint	Status, Err;
			string	csMsg;

			//-----------------------------------
			// ステータス情報取得
			//-----------------------------------
			ret = cnt.GetSamplingStatus(S_Id, out Status, out Err);
			csMsg = string.Format("CntGetSamplingStatus : Ret = {0:X}H", ret);
			txtSRet.Text = csMsg;
			// Status
			csMsg = "";
			if ((Status & (uint)CcntConst.CNTS_STATUS_BMSTOP) != 0)		csMsg += "BmStop,";
			if ((Status & (uint)CcntConst.CNTS_STATUS_CNTSTART) != 0)	csMsg += "CntStart,";
			if ((Status & (uint)CcntConst.CNTS_STATUS_CNTSTOP) != 0)	csMsg += "CntStop,";
			if ((Status & (uint)CcntConst.CNTS_STATUS_TRGIN) != 0)		csMsg += "TrgIn,";
			if ((Status & (uint)CcntConst.CNTS_STATUS_OVERRUN) != 0)	csMsg += "Overrun";
			txtSStatus.Text = csMsg;
			// Error
			csMsg = "";
			if ((Err & (uint)CcntConst.CNTS_STATUS_FIFOFULL) != 0)		csMsg += "FifoFull,";
			if ((Err & (uint)CcntConst.CNTS_STATUS_SGOVERIN) != 0)		csMsg += "S/GOverIn,";
			if ((Err & (uint)CcntConst.CNTS_STATUS_TRGERR) != 0)		csMsg += "TrgErr,";
			if ((Err & (uint)CcntConst.CNTS_STATUS_CLKERR) != 0)		csMsg += "ClkErr,";
			if ((Err & (uint)CcntConst.CNTS_STATUS_SLAVEHALT) != 0)		csMsg += "SlaveHalt,";
			if ((Err & (uint)CcntConst.CNTS_STATUS_MASTERHALT) != 0)	csMsg += "MasterHalt";
			txtSError.Text = csMsg;
		}

		//===================================================
		//	マスター 終了処理
		//===================================================
		private void btnMExit_Click(object sender, System.EventArgs e)
		{
			string	szMsg = new string(' ', 100);
			int		ret;

			//------------------------------
			// 終了処理
			//------------------------------
			ret = cnt.Exit(M_Id);
			szMsg = string.Format("CntClose : Ret = {0:X} h", ret);
			txtMRet.Text = szMsg;
		}

		//===================================================
		//	スレーブ 終了処理
		//===================================================
		private void btnSExit_Click(object sender, System.EventArgs e)
		{
			string	szMsg = new string(' ', 100);
			int		ret;

			//------------------------------
			// 終了処理
			//------------------------------
			ret = cnt.Exit(S_Id);
			szMsg = string.Format("CntClose : Ret = {0:X} h", ret);
			txtSRet.Text = szMsg;
		}

		//===================================================
		// プログラム終了
		//===================================================
		private void btnExit_Click(object sender, System.EventArgs e)
		{
			int ret;	
			ret = cnt.Exit(M_Id);
			ret = cnt.Exit(S_Id);
			this.Close();
		}
	
		//===================================================
		// カウンタ イベント通知処理
		//===================================================
		protected override void WndProc(ref Message m)
		{
			switch (m.Msg)
			{
				case (int)CcntConst.CNTM_SAMPLING_COUNT:
					if ( (m.WParam.ToInt32() & 0x0000ffff) == M_Id )
					{
						btnMCount_Click(btnMCount, new System.EventArgs());
					}
					else
					{
						btnSCount_Click(btnSCount, new System.EventArgs());
					}
					break;
				case (int)CcntConst.CNTM_SAMPLING_STOP:
					if ( (m.WParam.ToInt32() & 0x0000ffff) == M_Id )
					{
						btnMCount_Click(btnMCount, new System.EventArgs());
						txtMStatus.Text = "Stopped!";
					}
					else
					{
						btnSCount_Click(btnSCount, new System.EventArgs());
						txtSStatus.Text = "Stopped!";
					}
					break;
			}

			base.WndProc(ref m);
		}
	}
}
