﻿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 header
using CcntCs;

namespace Sync
{
	/// <summary>
	/// Summary description for Sync.
	/// </summary>
	public class Sync : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox5;
		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;
		private System.Windows.Forms.GroupBox groupBox3;
		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.Label Label5;
		public System.Windows.Forms.Label Label4;
		public System.Windows.Forms.Label Label3;
		public System.Windows.Forms.Label Label2;
		public System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.GroupBox Slave;
		public System.Windows.Forms.TextBox txtSRet;
		public System.Windows.Forms.TextBox txtSError;
		public System.Windows.Forms.TextBox txtSStatus;
		public System.Windows.Forms.TextBox txtSCount;
		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 groupBox2;
		private System.Windows.Forms.TextBox txtSDeviceName;
		private System.Windows.Forms.TextBox txtMDeviceName;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.GroupBox groupBox1;
		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;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

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

		public static short M_Id;						// Device ID of mater
		public static short S_Id;						// Device ID of slave


		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()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

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

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

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.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.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.btnExit = new System.Windows.Forms.Button();
			this.Slave = 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.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.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.groupBox5.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.Slave.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// 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(488, 8);
			this.groupBox5.Name = "groupBox5";
			this.groupBox5.Size = new System.Drawing.Size(136, 354);
			this.groupBox5.TabIndex = 7;
			this.groupBox5.TabStop = false;
			this.groupBox5.Text = "Slave";
			// 
			// 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, 318);
			this.btnSExit.Name = "btnSExit";
			this.btnSExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSExit.Size = new System.Drawing.Size(104, 27);
			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, 282);
			this.btnSStatus.Name = "btnSStatus";
			this.btnSStatus.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSStatus.Size = new System.Drawing.Size(104, 27);
			this.btnSStatus.TabIndex = 42;
			this.btnSStatus.Text = "Status";
			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, 245);
			this.btnSCount.Name = "btnSCount";
			this.btnSCount.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSCount.Size = new System.Drawing.Size(104, 27);
			this.btnSCount.TabIndex = 41;
			this.btnSCount.Text = "Count";
			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, 209);
			this.btnSData.Name = "btnSData";
			this.btnSData.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSData.Size = new System.Drawing.Size(104, 27);
			this.btnSData.TabIndex = 40;
			this.btnSData.Text = "Data...";
			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, 173);
			this.btnSStop.Name = "btnSStop";
			this.btnSStop.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSStop.Size = new System.Drawing.Size(104, 27);
			this.btnSStop.TabIndex = 39;
			this.btnSStop.Text = "Stop";
			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, 137);
			this.btnSStart.Name = "btnSStart";
			this.btnSStart.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSStart.Size = new System.Drawing.Size(104, 27);
			this.btnSStart.TabIndex = 38;
			this.btnSStart.Text = "Start";
			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, 101);
			this.btnSSmpMode.Name = "btnSSmpMode";
			this.btnSSmpMode.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSSmpMode.Size = new System.Drawing.Size(104, 27);
			this.btnSSmpMode.TabIndex = 37;
			this.btnSSmpMode.Text = "Sampling Mode...";
			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, 65);
			this.btnSCntMode.Name = "btnSCntMode";
			this.btnSCntMode.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSCntMode.Size = new System.Drawing.Size(104, 27);
			this.btnSCntMode.TabIndex = 36;
			this.btnSCntMode.Text = "Mode...";
			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, 29);
			this.btnSInit.Name = "btnSInit";
			this.btnSInit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnSInit.Size = new System.Drawing.Size(104, 27);
			this.btnSInit.TabIndex = 35;
			this.btnSInit.Text = "CntInit";
			this.btnSInit.Click += new System.EventHandler(this.btnSInit_Click);
			// 
			// 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, 73);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(328, 140);
			this.groupBox3.TabIndex = 10;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Master";
			// 
			// 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(88, 108);
			this.txtMRet.MaxLength = 0;
			this.txtMRet.Name = "txtMRet";
			this.txtMRet.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtMRet.Size = new System.Drawing.Size(224, 20);
			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(88, 80);
			this.txtMError.MaxLength = 0;
			this.txtMError.Name = "txtMError";
			this.txtMError.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtMError.Size = new System.Drawing.Size(224, 20);
			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(88, 52);
			this.txtMStatus.MaxLength = 0;
			this.txtMStatus.Name = "txtMStatus";
			this.txtMStatus.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtMStatus.Size = new System.Drawing.Size(224, 20);
			this.txtMStatus.TabIndex = 46;
			this.txtMStatus.Text = "";
			// 
			// txtMCount
			// 
			this.txtMCount.AcceptsReturn = true;
			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(88, 24);
			this.txtMCount.MaxLength = 0;
			this.txtMCount.Name = "txtMCount";
			this.txtMCount.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtMCount.Size = new System.Drawing.Size(224, 20);
			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, 112);
			this.Label5.Name = "Label5";
			this.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label5.Size = new System.Drawing.Size(73, 13);
			this.Label5.TabIndex = 52;
			this.Label5.Text = "Return Value:";
			// 
			// Label4
			// 
			this.Label4.AutoSize = true;
			this.Label4.BackColor = System.Drawing.SystemColors.Control;
			this.Label4.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label4.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label4.Location = new System.Drawing.Point(8, 84);
			this.Label4.Name = "Label4";
			this.Label4.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label4.Size = new System.Drawing.Size(32, 13);
			this.Label4.TabIndex = 51;
			this.Label4.Text = "Error:";
			// 
			// Label3
			// 
			this.Label3.AutoSize = true;
			this.Label3.BackColor = System.Drawing.SystemColors.Control;
			this.Label3.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label3.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label3.Location = new System.Drawing.Point(8, 56);
			this.Label3.Name = "Label3";
			this.Label3.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label3.Size = new System.Drawing.Size(39, 13);
			this.Label3.TabIndex = 50;
			this.Label3.Text = "Status:";
			// 
			// Label2
			// 
			this.Label2.AutoSize = true;
			this.Label2.BackColor = System.Drawing.SystemColors.Control;
			this.Label2.Cursor = System.Windows.Forms.Cursors.Default;
			this.Label2.ForeColor = System.Drawing.SystemColors.ControlText;
			this.Label2.Location = new System.Drawing.Point(8, 28);
			this.Label2.Name = "Label2";
			this.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Label2.Size = new System.Drawing.Size(38, 13);
			this.Label2.TabIndex = 49;
			this.Label2.Text = "Count:";
			// 
			// 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(434, 372);
			this.btnExit.Name = "btnExit";
			this.btnExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnExit.Size = new System.Drawing.Size(104, 26);
			this.btnExit.TabIndex = 8;
			this.btnExit.Text = "Exit";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// Slave
			// 
			this.Slave.Controls.AddRange(new System.Windows.Forms.Control[] {
																				this.txtSRet,
																				this.txtSError,
																				this.txtSStatus,
																				this.txtSCount,
																				this.label1,
																				this.label8,
																				this.label9,
																				this.label10});
			this.Slave.Location = new System.Drawing.Point(8, 223);
			this.Slave.Name = "Slave";
			this.Slave.Size = new System.Drawing.Size(328, 140);
			this.Slave.TabIndex = 11;
			this.Slave.TabStop = false;
			this.Slave.Text = "Slave";
			// 
			// 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(88, 108);
			this.txtSRet.MaxLength = 0;
			this.txtSRet.Name = "txtSRet";
			this.txtSRet.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtSRet.Size = new System.Drawing.Size(224, 20);
			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(88, 80);
			this.txtSError.MaxLength = 0;
			this.txtSError.Name = "txtSError";
			this.txtSError.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtSError.Size = new System.Drawing.Size(224, 20);
			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(88, 52);
			this.txtSStatus.MaxLength = 0;
			this.txtSStatus.Name = "txtSStatus";
			this.txtSStatus.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtSStatus.Size = new System.Drawing.Size(224, 20);
			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(88, 24);
			this.txtSCount.MaxLength = 0;
			this.txtSCount.Name = "txtSCount";
			this.txtSCount.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.txtSCount.Size = new System.Drawing.Size(224, 20);
			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, 112);
			this.label1.Name = "label1";
			this.label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.label1.Size = new System.Drawing.Size(73, 13);
			this.label1.TabIndex = 52;
			this.label1.Text = "Return Value:";
			// 
			// 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, 84);
			this.label8.Name = "label8";
			this.label8.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.label8.Size = new System.Drawing.Size(32, 13);
			this.label8.TabIndex = 51;
			this.label8.Text = "Error:";
			// 
			// 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, 56);
			this.label9.Name = "label9";
			this.label9.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.label9.Size = new System.Drawing.Size(39, 13);
			this.label9.TabIndex = 50;
			this.label9.Text = "Status:";
			// 
			// 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, 28);
			this.label10.Name = "label10";
			this.label10.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.label10.Size = new System.Drawing.Size(38, 13);
			this.label10.TabIndex = 49;
			this.label10.Text = "Count:";
			// 
			// 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, 8);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(328, 55);
			this.groupBox2.TabIndex = 9;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Device Name";
			// 
			// txtSDeviceName
			// 
			this.txtSDeviceName.Location = new System.Drawing.Point(240, 24);
			this.txtSDeviceName.Name = "txtSDeviceName";
			this.txtSDeviceName.Size = new System.Drawing.Size(72, 20);
			this.txtSDeviceName.TabIndex = 3;
			this.txtSDeviceName.Text = "";
			// 
			// txtMDeviceName
			// 
			this.txtMDeviceName.Location = new System.Drawing.Point(64, 24);
			this.txtMDeviceName.Name = "txtMDeviceName";
			this.txtMDeviceName.Size = new System.Drawing.Size(72, 20);
			this.txtMDeviceName.TabIndex = 2;
			this.txtMDeviceName.Text = "";
			// 
			// label7
			// 
			this.label7.AutoSize = true;
			this.label7.Location = new System.Drawing.Point(184, 28);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(36, 13);
			this.label7.TabIndex = 1;
			this.label7.Text = "Slave:";
			// 
			// label6
			// 
			this.label6.AutoSize = true;
			this.label6.Location = new System.Drawing.Point(8, 28);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(42, 13);
			this.label6.TabIndex = 0;
			this.label6.Text = "Master:";
			// 
			// 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(344, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(136, 354);
			this.groupBox1.TabIndex = 6;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Master";
			// 
			// 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, 318);
			this.btnMExit.Name = "btnMExit";
			this.btnMExit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMExit.Size = new System.Drawing.Size(104, 27);
			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, 282);
			this.btnMStatus.Name = "btnMStatus";
			this.btnMStatus.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMStatus.Size = new System.Drawing.Size(104, 27);
			this.btnMStatus.TabIndex = 42;
			this.btnMStatus.Text = "Status";
			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, 245);
			this.btnMCount.Name = "btnMCount";
			this.btnMCount.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMCount.Size = new System.Drawing.Size(104, 27);
			this.btnMCount.TabIndex = 41;
			this.btnMCount.Text = "Count";
			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, 209);
			this.btnMData.Name = "btnMData";
			this.btnMData.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMData.Size = new System.Drawing.Size(104, 27);
			this.btnMData.TabIndex = 40;
			this.btnMData.Text = "Data...";
			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, 173);
			this.btnMStop.Name = "btnMStop";
			this.btnMStop.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMStop.Size = new System.Drawing.Size(104, 27);
			this.btnMStop.TabIndex = 39;
			this.btnMStop.Text = "Stop";
			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, 137);
			this.btnMStart.Name = "btnMStart";
			this.btnMStart.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMStart.Size = new System.Drawing.Size(104, 27);
			this.btnMStart.TabIndex = 38;
			this.btnMStart.Text = "Start";
			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, 101);
			this.btnMSmpMode.Name = "btnMSmpMode";
			this.btnMSmpMode.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMSmpMode.Size = new System.Drawing.Size(104, 27);
			this.btnMSmpMode.TabIndex = 37;
			this.btnMSmpMode.Text = "Samplilng Mode...";
			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, 65);
			this.btnMCntMode.Name = "btnMCntMode";
			this.btnMCntMode.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMCntMode.Size = new System.Drawing.Size(104, 27);
			this.btnMCntMode.TabIndex = 36;
			this.btnMCntMode.Text = "Mode...";
			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, 29);
			this.btnMInit.Name = "btnMInit";
			this.btnMInit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.btnMInit.Size = new System.Drawing.Size(104, 27);
			this.btnMInit.TabIndex = 35;
			this.btnMInit.Text = "CntInit";
			this.btnMInit.Click += new System.EventHandler(this.btnMInit_Click);
			// 
			// Sync
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(632, 406);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox5,
																		  this.groupBox3,
																		  this.btnExit,
																		  this.Slave,
																		  this.groupBox2,
																		  this.groupBox1});
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Sync";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Sync Sampling";
			this.Load += new System.EventHandler(this.Sync_Load);
			this.Closed += new System.EventHandler(this.Sync_Closed);
			this.groupBox5.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.Slave.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		//===================================================
		// Dialog initialization
		//===================================================
		private void Sync_Load(object sender, System.EventArgs e)
		{
			//-----------------------------------
			// Initialize dialog display
			//-----------------------------------
			txtMDeviceName.Text = "CNT000";
			txtSDeviceName.Text = "CNT001";

			//-----------------------------------
			// Specify Pinned handle to M_SampData and 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);
			}
		}

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

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

			//-----------------------------------
			// Termination processing
			//-----------------------------------
			ret = cnt.Exit(M_Id);
			ret = cnt.Exit(S_Id);
		}

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

			//-----------------------------------
			// Get device name
			//-----------------------------------
			buf = txtMDeviceName.Text;
	
			//-----------------------------------
			// Initialization
			//-----------------------------------
			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() for slave
		//===================================================
		private void btnSInit_Click(object sender, System.EventArgs e)
		{
			string	str;
			string	buf = new string(' ', 100);
			string	error_string = new string(' ', 256);
			int		ret;

			//-----------------------------------
			// Get device name
			//-----------------------------------
			buf = txtSDeviceName.Text;
	
			//-----------------------------------
			// Initialization
			//-----------------------------------
			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;
		}

		//===================================================
		// Condition setting for master
		//===================================================
		private void btnMCntMode_Click(object sender, System.EventArgs e)
		{
			ModeDlg dlg = new ModeDlg();

			//-----------------------------------
			// Open "Counter Mode" dialog
			//-----------------------------------
			dlg.Id = M_Id;
			dlg.ShowDialog();
			dlg.Dispose();
		}

		//===================================================
		// Condition setting for slave
		//===================================================
		private void btnSCntMode_Click(object sender, System.EventArgs e)
		{
			ModeDlg dlg = new ModeDlg();

			//-----------------------------------
			// Open "Counter Mode" dialog
			//-----------------------------------
			dlg.Id = S_Id;
			dlg.ShowDialog();
			dlg.Dispose();
		}

		//===================================================
		// Sampling mode setting for master
		//===================================================
		private void btnMSmpMode_Click(object sender, System.EventArgs e)
		{
			MSampMode	dlg = new MSampMode();
			dlg.ShowDialog();
			dlg.Dispose();
		}

		//===================================================
		// Sampling mode setting for slave
		//===================================================
		private void btnSSmpMode_Click(object sender, System.EventArgs e)
		{
			SSampMode	dlg = new SSampMode();
			dlg.ShowDialog();
			dlg.Dispose();
		}

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

			//-----------------------------------
			// Set notification
			//-----------------------------------
			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;
			}

			//-----------------------------------
			// Reset 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;
			}

			//-----------------------------------
			// Set buffer
			//-----------------------------------
			Array.Clear(M_SampData, 0, M_SampData.Length);
			//-----------------------------------
			// Get object address by Pinned handle
			//-----------------------------------
			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;
			}
			//-----------------------------------
			// Start counter
			//-----------------------------------
			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;
			}

			//-----------------------------------
			// Start sampling
			//-----------------------------------
			ret = cnt.SamplingStart(M_Id);
			csMsg = string.Format("CntStartSampling : RetCode = {0:X}H", ret);
			txtMRet.Text = csMsg;
			txtMCount.Text = "";
			txtMStatus.Text = "";
			txtMError.Text = "";
		}

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

			//-----------------------------------
			// Set notification
			//-----------------------------------
			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;
			}
			//-----------------------------------
			// Set buffer
			//-----------------------------------
			Array.Clear(S_SampData, 0, M_SampData.Length);
			//-----------------------------------
			// Get object address by Pinned handle
			//-----------------------------------
			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;
			}
			//-----------------------------------
			// Start counter
			//-----------------------------------
			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;
			}
			//-----------------------------------
			// Start sampling
			//-----------------------------------
			ret = cnt.SamplingStart(S_Id);
			csMsg = string.Format("CntStartSampling : RetCode = {0:X}H", ret);
			txtSRet.Text = csMsg;
			txtSCount.Text = "";
			txtSStatus.Text = "";
			txtSError.Text = "";
		}

		//===================================================
		// Master stop
		//===================================================
		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;
		}

		//===================================================
		// Slave stop
		//===================================================
		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;
		}

		//===================================================
		// Get master data
		//===================================================
		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);
			 
			//-----------------------------------
			// Open file
			//-----------------------------------
			try
			{
				fp = File.CreateText(szFileName);
			}
			catch (System.IO.IOException)
			{
				txtMRet.Text = "fopen : file open error.";
				return;
			}	
			//-----------------------------------
			// Write data to file
			//-----------------------------------
			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);
			}
			//-----------------------------------
			// Close file
			//-----------------------------------
			fp.Close();

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

		//===================================================
		// Get slave data
		//===================================================
		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);
			
			//-----------------------------------
			// Open file
			//-----------------------------------
			try
			{
				fp = File.CreateText(szFileName);
			}
			catch (System.IO.IOException)
			{
				txtSRet.Text = "fopen : file open error.";
				return;
			}	
			//-----------------------------------
			// Write data to file
			//-----------------------------------
			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);
			}

			//-----------------------------------
			// Close file
			//-----------------------------------
			fp.Close();

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

		//===================================================
		// Get master count value
		//===================================================
		private void btnMCount_Click(object sender, System.EventArgs e)
		{
			int		ret;
			uint	Count;
			uint	ScanCarry;
			string	csMsg;

			//-----------------------------------
			// Get count value
			//-----------------------------------
			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;
		}

		//===================================================
		// Get slave count value
		//===================================================
		private void btnSCount_Click(object sender, System.EventArgs e)
		{
			int		ret;
			uint	Count;
			uint	ScanCarry;
			string	csMsg;

			//-----------------------------------
			// Get count value
			//-----------------------------------
			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;
		}

		//===================================================
		// Get master status
		//===================================================
		private void btnMStatus_Click(object sender, System.EventArgs e)
		{
			int		ret;
			uint	Status, Err;
			string	csMsg;

			//-----------------------------------
			// Get status information
			//-----------------------------------
			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;
		}

		//===================================================
		// Get slave status
		//===================================================
		private void btnSStatus_Click(object sender, System.EventArgs e)
		{
			int		ret;
			uint	Status, Err;
			string	csMsg;

			//-----------------------------------
			// Get status information
			//-----------------------------------
			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;
		}

		//===================================================
		// Exit master
		//===================================================
		private void btnMExit_Click(object sender, System.EventArgs e)
		{
			string	szMsg = new string(' ', 100);
			int		ret;

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

		//===================================================
		// Exit slave
		//===================================================
		private void btnSExit_Click(object sender, System.EventArgs e)
		{
			string	szMsg = new string(' ', 100);
			int		ret;

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

		//===================================================
		// Exit program
		//===================================================
		private void btnExit_Click(object sender, System.EventArgs e)
		{
			int ret;	
			ret = cnt.Exit(M_Id);
			ret = cnt.Exit(S_Id);
			this.Close();
		}
	
		//===================================================
		// Processing of counter event notification
		//===================================================
		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);
		}
	}
}
