using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CdigCs;

namespace Sync
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// Creat the instance of class Cdig
		/// </summary>
		Cdig  dig = new Cdig();
		public const int	MAX_SAMPLES = 1000;				// The definition of the maximum number of acquiring samples  : 1000

		public short	m_Id1;								// Device1 ID
		public short	m_Id2;								// Device2 ID
		public float[]	m_Data1 = new float[MAX_SAMPLES];	// Array of retrieving data for Device1
		public float[]	m_Data2 = new float[MAX_SAMPLES];	// Array of retrieving data for Device2
		public int		m_Samples1;							// Number of retrieved samples for Device1
		public int		m_Samples2;							// Number of retrieved samples for Device2

		private System.Windows.Forms.ListBox listBoxData2;
		private System.Windows.Forms.TextBox textBoxInfo2;
		private System.Windows.Forms.Label labelStatus2;
		private System.Windows.Forms.Label labelCount2;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.ListBox listBoxData1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.ComboBox comboBoxRange2;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.TextBox textBoxDevice2;
		private System.Windows.Forms.TextBox textBoxInfo1;
		private System.Windows.Forms.Label labelStatus1;
		private System.Windows.Forms.Label labelCount1;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Button buttonExit;
		private System.Windows.Forms.Button buttonStop;
		private System.Windows.Forms.Button buttonInfo;
		private System.Windows.Forms.Button buttonStart;
		private System.Windows.Forms.Button buttonInit;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.ComboBox comboBoxRange1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBoxDevice1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// 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.listBoxData2 = new System.Windows.Forms.ListBox();
            this.textBoxInfo2 = new System.Windows.Forms.TextBox();
            this.labelStatus2 = new System.Windows.Forms.Label();
            this.labelCount2 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.label8 = new System.Windows.Forms.Label();
            this.listBoxData1 = new System.Windows.Forms.ListBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.comboBoxRange2 = new System.Windows.Forms.ComboBox();
            this.label9 = new System.Windows.Forms.Label();
            this.textBoxDevice2 = new System.Windows.Forms.TextBox();
            this.textBoxInfo1 = new System.Windows.Forms.TextBox();
            this.labelStatus1 = new System.Windows.Forms.Label();
            this.labelCount1 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.buttonExit = new System.Windows.Forms.Button();
            this.buttonStop = new System.Windows.Forms.Button();
            this.buttonInfo = new System.Windows.Forms.Button();
            this.buttonStart = new System.Windows.Forms.Button();
            this.buttonInit = new System.Windows.Forms.Button();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.comboBoxRange1 = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.textBoxDevice1 = new System.Windows.Forms.TextBox();
            this.groupBox2.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // listBoxData2
            // 
            this.listBoxData2.Location = new System.Drawing.Point(304, 113);
            this.listBoxData2.Name = "listBoxData2";
            this.listBoxData2.Size = new System.Drawing.Size(160, 173);
            this.listBoxData2.TabIndex = 12;
            // 
            // textBoxInfo2
            // 
            this.textBoxInfo2.Location = new System.Drawing.Point(296, 343);
            this.textBoxInfo2.Multiline = true;
            this.textBoxInfo2.Name = "textBoxInfo2";
            this.textBoxInfo2.ReadOnly = true;
            this.textBoxInfo2.Size = new System.Drawing.Size(176, 50);
            this.textBoxInfo2.TabIndex = 14;
            // 
            // labelStatus2
            // 
            this.labelStatus2.Location = new System.Drawing.Point(408, 324);
            this.labelStatus2.Name = "labelStatus2";
            this.labelStatus2.Size = new System.Drawing.Size(64, 17);
            this.labelStatus2.TabIndex = 60;
            this.labelStatus2.Text = "0H";
            // 
            // labelCount2
            // 
            this.labelCount2.Location = new System.Drawing.Point(408, 303);
            this.labelCount2.Name = "labelCount2";
            this.labelCount2.Size = new System.Drawing.Size(64, 18);
            this.labelCount2.TabIndex = 59;
            this.labelCount2.Text = "0";
            // 
            // label7
            // 
            this.label7.Location = new System.Drawing.Point(296, 324);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(64, 17);
            this.label7.TabIndex = 58;
            this.label7.Text = "Status";
            // 
            // label8
            // 
            this.label8.Location = new System.Drawing.Point(296, 303);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(104, 18);
            this.label8.TabIndex = 57;
            this.label8.Text = "Number of samples";
            // 
            // listBoxData1
            // 
            this.listBoxData1.Location = new System.Drawing.Point(128, 113);
            this.listBoxData1.Name = "listBoxData1";
            this.listBoxData1.Size = new System.Drawing.Size(160, 173);
            this.listBoxData1.TabIndex = 11;
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.comboBoxRange2);
            this.groupBox2.Controls.Add(this.label9);
            this.groupBox2.Controls.Add(this.textBoxDevice2);
            this.groupBox2.Location = new System.Drawing.Point(296, 14);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(176, 281);
            this.groupBox2.TabIndex = 2;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Device2";
            // 
            // comboBoxRange2
            // 
            this.comboBoxRange2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBoxRange2.Location = new System.Drawing.Point(8, 68);
            this.comboBoxRange2.Name = "comboBoxRange2";
            this.comboBoxRange2.Size = new System.Drawing.Size(160, 21);
            this.comboBoxRange2.TabIndex = 5;
            this.comboBoxRange2.SelectedIndexChanged += new System.EventHandler(this.comboBoxRange2_SelectedIndexChanged);
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(8, 51);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(64, 18);
            this.label9.TabIndex = 27;
            this.label9.Text = "Range";
            // 
            // textBoxDevice2
            // 
            this.textBoxDevice2.Location = new System.Drawing.Point(8, 21);
            this.textBoxDevice2.Name = "textBoxDevice2";
            this.textBoxDevice2.Size = new System.Drawing.Size(72, 20);
            this.textBoxDevice2.TabIndex = 3;
            this.textBoxDevice2.Text = "DIG001";
            // 
            // textBoxInfo1
            // 
            this.textBoxInfo1.Location = new System.Drawing.Point(120, 343);
            this.textBoxInfo1.Multiline = true;
            this.textBoxInfo1.Name = "textBoxInfo1";
            this.textBoxInfo1.ReadOnly = true;
            this.textBoxInfo1.Size = new System.Drawing.Size(176, 50);
            this.textBoxInfo1.TabIndex = 13;
            // 
            // labelStatus1
            // 
            this.labelStatus1.Location = new System.Drawing.Point(232, 324);
            this.labelStatus1.Name = "labelStatus1";
            this.labelStatus1.Size = new System.Drawing.Size(56, 17);
            this.labelStatus1.TabIndex = 53;
            this.labelStatus1.Text = "0H";
            // 
            // labelCount1
            // 
            this.labelCount1.Location = new System.Drawing.Point(232, 303);
            this.labelCount1.Name = "labelCount1";
            this.labelCount1.Size = new System.Drawing.Size(56, 18);
            this.labelCount1.TabIndex = 52;
            this.labelCount1.Text = "0";
            // 
            // label6
            // 
            this.label6.Location = new System.Drawing.Point(120, 324);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(64, 17);
            this.label6.TabIndex = 51;
            this.label6.Text = "Status";
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(120, 303);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(104, 18);
            this.label5.TabIndex = 50;
            this.label5.Text = "Number of samples";
            // 
            // buttonExit
            // 
            this.buttonExit.Location = new System.Drawing.Point(8, 210);
            this.buttonExit.Name = "buttonExit";
            this.buttonExit.Size = new System.Drawing.Size(104, 28);
            this.buttonExit.TabIndex = 10;
            this.buttonExit.Text = "Exit";
            this.buttonExit.Click += new System.EventHandler(this.buttonExit_Click);
            // 
            // buttonStop
            // 
            this.buttonStop.Location = new System.Drawing.Point(8, 171);
            this.buttonStop.Name = "buttonStop";
            this.buttonStop.Size = new System.Drawing.Size(104, 28);
            this.buttonStop.TabIndex = 9;
            this.buttonStop.Text = "Stop Acquisition";
            this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
            // 
            // buttonInfo
            // 
            this.buttonInfo.Location = new System.Drawing.Point(8, 132);
            this.buttonInfo.Name = "buttonInfo";
            this.buttonInfo.Size = new System.Drawing.Size(104, 28);
            this.buttonInfo.TabIndex = 8;
            this.buttonInfo.Text = "Get Status";
            this.buttonInfo.Click += new System.EventHandler(this.buttonInfo_Click);
            // 
            // buttonStart
            // 
            this.buttonStart.Location = new System.Drawing.Point(8, 93);
            this.buttonStart.Name = "buttonStart";
            this.buttonStart.Size = new System.Drawing.Size(104, 28);
            this.buttonStart.TabIndex = 7;
            this.buttonStart.Text = "Start Acquisition";
            this.buttonStart.Click += new System.EventHandler(this.buttonStart_Click);
            // 
            // buttonInit
            // 
            this.buttonInit.Location = new System.Drawing.Point(8, 54);
            this.buttonInit.Name = "buttonInit";
            this.buttonInit.Size = new System.Drawing.Size(104, 28);
            this.buttonInit.TabIndex = 6;
            this.buttonInit.Text = "Init";
            this.buttonInit.Click += new System.EventHandler(this.buttonInit_Click);
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.comboBoxRange1);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.textBoxDevice1);
            this.groupBox1.Location = new System.Drawing.Point(120, 14);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(176, 281);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Device1";
            // 
            // comboBoxRange1
            // 
            this.comboBoxRange1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBoxRange1.Location = new System.Drawing.Point(8, 68);
            this.comboBoxRange1.Name = "comboBoxRange1";
            this.comboBoxRange1.Size = new System.Drawing.Size(160, 21);
            this.comboBoxRange1.TabIndex = 4;
            this.comboBoxRange1.SelectedIndexChanged += new System.EventHandler(this.comboBoxRange1_SelectedIndexChanged);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(8, 51);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(64, 18);
            this.label2.TabIndex = 27;
            this.label2.Text = "Range";
            // 
            // textBoxDevice1
            // 
            this.textBoxDevice1.Location = new System.Drawing.Point(8, 21);
            this.textBoxDevice1.Name = "textBoxDevice1";
            this.textBoxDevice1.Size = new System.Drawing.Size(72, 20);
            this.textBoxDevice1.TabIndex = 1;
            this.textBoxDevice1.Text = "DIG000";
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(479, 402);
            this.Controls.Add(this.listBoxData2);
            this.Controls.Add(this.textBoxInfo2);
            this.Controls.Add(this.labelStatus2);
            this.Controls.Add(this.labelCount2);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.label8);
            this.Controls.Add(this.listBoxData1);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.textBoxInfo1);
            this.Controls.Add(this.labelStatus1);
            this.Controls.Add(this.labelCount1);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.buttonExit);
            this.Controls.Add(this.buttonStop);
            this.Controls.Add(this.buttonInfo);
            this.Controls.Add(this.buttonStart);
            this.Controls.Add(this.buttonInit);
            this.Controls.Add(this.groupBox1);
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.Text = "Sync Sampling with 2 Devices";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.Closed += new System.EventHandler(this.Form1_Closed);
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
		/// <summary>
		/// Init Dialog
		/// </summary>
		private void Form1_Load(object sender, System.EventArgs e)
		{
			// Initialize input range

			// Device1
			comboBoxRange1.ResetText();
			comboBoxRange1.Items.AddRange(new object[]
				{
					"-20 - +20V",
					"-10 - +10V",
					"-5 - +5V",
					"-2 - +2V",
					"-1 - +1V",
					"-0.5 - +0.5V",
					"-0.2 - +0.2V",
					"-0.1 - +0.1V",
					"-0.05 - +0.05V",
					"-0.02 - +0.02V"}
				);

			// Device2
			comboBoxRange2.ResetText();
			comboBoxRange2.Items.AddRange(new object[]
				{
					"-20 - +20V",
					"-10 - +10V",
					"-5 - +5V",
					"-2 - +2V",
					"-1 - +1V",
					"-0.5 - +0.5V",
					"-0.2 - +0.2V",
					"-0.1 - +0.1V",
					"-0.05 - +0.05V",
					"-0.02 - +0.02V"}
				);		
		}

		/// <summary>
		/// Init handling
		/// </summary>
		private void buttonInit_Click(object sender, System.EventArgs e)
		{
			int		Ret;
			string	ErrorString;

			//-------------------------------------
			// Device1
			//-------------------------------------
			//--------------------------------
			// Init handling
			//--------------------------------
			Ret = dig.Init(textBoxDevice1.Text, out m_Id1);
			if(Ret != 0)
			{
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.Init = " + Ret.ToString() + " : " + ErrorString;
				return;
			}

			//--------------------------------
			// Reset device
			//--------------------------------
			Ret = dig.ResetDevice(m_Id1);
			if(Ret != 0)
			{
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.ResetDevice = " + Ret.ToString() + " : " + ErrorString;
				return;
			}

			textBoxInfo1.Text = "Init : Normality completion";

			//-------------------------------------
			// Device2
			//-------------------------------------
			//--------------------------------
			// Init handling
			//--------------------------------
			Ret = dig.Init(textBoxDevice2.Text, out m_Id2);
			if(Ret != 0)
			{
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.Init = " + Ret.ToString() + " : " + ErrorString;
				return;
			}

			//--------------------------------
			// Reset device
			//--------------------------------
			Ret = dig.ResetDevice(m_Id2);
			if(Ret != 0)
			{
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.ResetDevice = " + Ret.ToString() + " : " + ErrorString;
				return;
			}

			textBoxInfo2.Text = "Init : Normality completion";		
		}

		/// <summary>
		/// Set input range for device1
		/// </summary>
		private void comboBoxRange1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			int		Index;		// The selected index value of input range combobox
			float	Range;		// Input range
			Index = comboBoxRange1.SelectedIndex;
			switch(Index)
			{
				case 0:	Range = Cdig.DIG_RANGE_40VPP;	break;
				case 1:	Range = Cdig.DIG_RANGE_20VPP;	break;
				case 2:	Range = Cdig.DIG_RANGE_10VPP;	break;
				case 3:	Range = Cdig.DIG_RANGE_4VPP;	break;
				case 4:	Range = Cdig.DIG_RANGE_2VPP;	break;
				case 5:	Range = Cdig.DIG_RANGE_1VPP;	break;
				case 6:	Range = Cdig.DIG_RANGE_400MVPP;	break;
				case 7:	Range = Cdig.DIG_RANGE_200MVPP;	break;
				case 8:	Range = Cdig.DIG_RANGE_100MVPP;	break;
				case 9:	Range = Cdig.DIG_RANGE_40MVPP;	break;
				default:Range = 0;						break;
			}

			//--------------------------
			// Set input range of channel0
			//--------------------------
			int		Ret;
			Ret = dig.SetRange(m_Id1, Cdig.DIG_CHANNEL0, Range);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.SetRange = " + Ret.ToString() + " : " + ErrorString;
				return;
			}

			textBoxInfo1.Text = "Set input range : Normality completion";		
		}

		/// <summary>
		/// Set input range for device2
		/// </summary>
		private void comboBoxRange2_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			int		Index;		// The selected index value of input range combobox
			float	Range;		// Input range
			Index = comboBoxRange2.SelectedIndex;
			switch(Index)
			{
				case 0:	Range = Cdig.DIG_RANGE_40VPP;	break;
				case 1:	Range = Cdig.DIG_RANGE_20VPP;	break;
				case 2:	Range = Cdig.DIG_RANGE_10VPP;	break;
				case 3:	Range = Cdig.DIG_RANGE_4VPP;	break;
				case 4:	Range = Cdig.DIG_RANGE_2VPP;	break;
				case 5:	Range = Cdig.DIG_RANGE_1VPP;	break;
				case 6:	Range = Cdig.DIG_RANGE_400MVPP;	break;
				case 7:	Range = Cdig.DIG_RANGE_200MVPP;	break;
				case 8:	Range = Cdig.DIG_RANGE_100MVPP;	break;
				case 9:	Range = Cdig.DIG_RANGE_40MVPP;	break;
				default:Range = 0;						break;
			}

			//--------------------------
			// Set input range of channel0
			//--------------------------
			int		Ret;
			Ret = dig.SetRange(m_Id2, Cdig.DIG_CHANNEL0, Range);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.SetRange = " + Ret.ToString() + " : " + ErrorString;
				return;
			}

			textBoxInfo2.Text = "Set input range : Normality completion";
		}

		/// <summary>
		/// Start acquisition
		/// </summary>
		private void buttonStart_Click(object sender, System.EventArgs e)
		{
			int		Ret;		// Return value
			//---------------------------------
			// Initialize variable
			//---------------------------------
			m_Samples1 = 0;
			m_Samples2 = 0;
			listBoxData1.Items.Clear();
			listBoxData2.Items.Clear();

			//-------------------------------------------
			// Set deivce1
			//-------------------------------------------
			//---------------------------------
			// Set operation mode : Data logger mode
			//---------------------------------
			Ret = dig.SetDigitizerMode(m_Id1, Cdig.DIG_MODE_LOGGER);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.SetDigitizerMode = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set Event Controller Unit : Synchronous bus signal line 1 - pre-trigger satisfaction
			//---------------------------------
			Ret = dig.SetEcuSignal(m_Id1, Cdig.DIG_ECU_DEST_SYNC1, Cdig.DIG_ECU_SRC_PRETRG);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.SetEcuSignal = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set sampling clock source : Crystal oscillator of board
			//---------------------------------
			Ret = dig.SetClockSource(m_Id1, Cdig.DIG_ECU_SRC_CLOCK);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.SetClockSource = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set sampling clock : 1000usec  (set value * 10 + 10 (nsec))
			//---------------------------------
			Ret = dig.SetSampleClock(m_Id1, 99999);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.SetSampleClock = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set the number of acquiring records : 1
			//---------------------------------
			Ret = dig.SetRecordNumber(m_Id1, 1);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.SetRecordNumber = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set the number of pre-trigger samples : 1000
			//---------------------------------
			Ret = dig.SetPreSampleTimes(m_Id1, MAX_SAMPLES);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.SetPreSampleTimes = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set the number of post-trigger samples : 0
			//---------------------------------
			Ret = dig.SetPostSampleTimes(m_Id1, 0);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.SetPostSampleTimes = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set channel : Enable channel0 only
			//---------------------------------
			// Enable channel0
			Ret = dig.SetChannel(m_Id1, Cdig.DIG_CHANNEL0, Cdig.DIG_ENABLE);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.SetChannel = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			// Disable channel1
			Ret = dig.SetChannel(m_Id1, Cdig.DIG_CHANNEL1, Cdig.DIG_DISABLE);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.SetChannel = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Sets the type of pre-trigger : Auto
			//---------------------------------
			Ret = dig.SetPreTriggerType(m_Id1, Cdig.DIG_TRIGGERTYPE_AUTO);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.SetPreTriggerType = " + Ret.ToString() + " : " + ErrorString;
				return;
			}

			//-------------------------------------------
			// Set deivce2
			//-------------------------------------------
			//---------------------------------
			// Set operation mode : Data logger mode
			//---------------------------------
			Ret = dig.SetDigitizerMode(m_Id2, Cdig.DIG_MODE_LOGGER);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.SetDigitizerMode = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set sampling clock source : Crystal oscillator of board
			//---------------------------------
			Ret = dig.SetClockSource(m_Id2, Cdig.DIG_ECU_SRC_CLOCK);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.SetClockSource = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set sampling clock : 1000usec  (set value * 10 + 10 (nsec))
			//---------------------------------
			Ret = dig.SetSampleClock(m_Id2, 99999);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.SetSampleClock = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set the number of acquiring records : 1
			//---------------------------------
			Ret = dig.SetRecordNumber(m_Id2, 1);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.SetRecordNumber = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set the number of pre-trigger samples : 1000
			//---------------------------------
			Ret = dig.SetPreSampleTimes(m_Id2, MAX_SAMPLES);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.SetPreSampleTimes = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set the number of post-trigger samples : 0
			//---------------------------------
			Ret = dig.SetPostSampleTimes(m_Id2, 0);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.SetPostSampleTimes = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set channel : Enable channel0 only
			//---------------------------------
			// Enable channel0
			Ret = dig.SetChannel(m_Id2, Cdig.DIG_CHANNEL0, Cdig.DIG_ENABLE);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.SetChannel = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			// Disable channel1
			Ret = dig.SetChannel(m_Id2, Cdig.DIG_CHANNEL1, Cdig.DIG_DISABLE);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.SetChannel = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set the type of pre-trigger : Digital edge 
			//---------------------------------
			Ret = dig.SetPreTriggerType(m_Id2, Cdig.DIG_TRIGGERTYPE_DIEDGE);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.SetPreTriggerType = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			//---------------------------------
			// Set pre-trigger : Signal source = Synchronous bus 1, Direction = Rising edge, Count = 1
			//---------------------------------
			Ret = dig.SetPreTriggerDiEdge(m_Id2, Cdig.DIG_ECU_SRC_SYNC1, 0, 1);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.SetPreTriggerDiEdge = " + Ret.ToString() + " : " + ErrorString;
				return;
			}

			//---------------------------------
			// Operation start : Start device2 first
			//---------------------------------
			Ret = dig.Start(m_Id2);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.Start = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			textBoxInfo2.Text = "Start acquisition : Normality completion";		

			Ret = dig.Start(m_Id1);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.Start = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			textBoxInfo1.Text = "Start acquisition : Normality completion";

			//---------------------------------
			// Polling until the operation status is changed to be OFF
			//---------------------------------
			int Status1, Status2;

			do
			{
				//---------------------------------
				// In order to enable the dialog operation
				//---------------------------------
				System.Windows.Forms.Application.DoEvents();
				//------------------------------------------------------------
				// Device1
				//------------------------------------------------------------
				//---------------------------------
				// Get the number of samples stored in driver memory
				//---------------------------------
				Ret = dig.GetDriverMemorySamples(m_Id1, out m_Samples1);
				if(Ret != 0)
				{
					string ErrorString;
					dig.GetErrorString(Ret, out ErrorString);
					textBoxInfo1.Text = "dig.GetDriverMemorySamples = " + Ret.ToString() + " : " + ErrorString;
					return;
				}
				labelCount1.Text = m_Samples1.ToString();
				//---------------------------------
				// Get the operation status
				//---------------------------------
				Ret = dig.GetStatus(m_Id1, out Status1);
				if(Ret != 0)
				{
					string ErrorString;
					dig.GetErrorString(Ret, out ErrorString);
					textBoxInfo1.Text = "dig.GetStatus = " + Ret.ToString() + " : " + ErrorString;
					return;
				}
				labelStatus1.Text = Status1.ToString("X") + "H";
				//------------------------------------------------------------
				// Device2
				//------------------------------------------------------------
				//---------------------------------
				// Get the number of samples stored in driver memory
				//---------------------------------
				Ret = dig.GetDriverMemorySamples(m_Id2, out m_Samples2);
				if(Ret != 0)
				{
					string ErrorString;
					dig.GetErrorString(Ret, out ErrorString);
					textBoxInfo2.Text = "dig.GetDriverMemorySamples = " + Ret.ToString() + " : " + ErrorString;
					return;
				}
				labelCount2.Text = m_Samples2.ToString();
				//---------------------------------
				// Get the operation status
				//---------------------------------
				Ret = dig.GetStatus(m_Id2, out Status2);
				if(Ret != 0)
				{
					string ErrorString;
					dig.GetErrorString(Ret, out ErrorString);
					textBoxInfo2.Text = "dig.GetStatus = " + Ret.ToString() + " : " + ErrorString;
					return;
				}
				labelStatus2.Text = Status2.ToString("X") + "H";
			}while(((Status1 | Status2) & Cdig.DIG_STATUS_BUSY) == Cdig.DIG_STATUS_BUSY);

			//------------------------------------------------------------
			// Device1
			//------------------------------------------------------------
			//---------------------------------
			// After operation stop, get the number of samples which are being stored in driver memory
			//---------------------------------
			Ret = dig.GetDriverMemorySamples(m_Id1, out m_Samples1);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.GetDriverMemorySamples = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			labelCount1.Text = m_Samples1.ToString();
			//---------------------------------
			// Retrieve the acquisition data
			//---------------------------------
			int TriggerPoint1;			// Position of the first data after the establishment of the post-trigger
			if(m_Samples1 > 0)
			{
				// Adjust to prevent the retrieved number of sampling from exceeding the size of array for storing data
				if(m_Samples1 > MAX_SAMPLES)
				{
					m_Samples1 = MAX_SAMPLES;
				}
				Ret = dig.ReadSamples(m_Id1, ref m_Samples1, ref m_Data1, out TriggerPoint1);
				if(Ret != 0)
				{
					string ErrorString;
					dig.GetErrorString(Ret, out ErrorString);
					textBoxInfo1.Text = "dig.ReadSamples = " + Ret.ToString() + " : " + ErrorString;
					return;
				}
				//---------------------------------
				// Display the retrieved data
				//---------------------------------
				for(int i = 0; i < m_Samples1; i++)
				{
					listBoxData1.Items.Add(String.Format("{0:D}\t{1:F3}", i+1, m_Data1[i])); 
				}
			}

			//------------------------------------------------------------
			// Device2
			//------------------------------------------------------------
			//---------------------------------
			// After operation stop, get the number of samples which are being stored in driver memory
			//---------------------------------
			Ret = dig.GetDriverMemorySamples(m_Id2, out m_Samples2);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.GetDriverMemorySamples = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			labelCount2.Text = m_Samples2.ToString();
			//---------------------------------
			// Retrieve the acquisition data
			//---------------------------------
			int TriggerPoint2;			// Position of the first data after the establishment of the post-trigger
			if(m_Samples2 > 0)
			{
				// Adjust to prevent the retrieved number of sampling from exceeding the size of array for storing data
				if(m_Samples2 > MAX_SAMPLES)
				{
					m_Samples2 = MAX_SAMPLES;
				}
				Ret = dig.ReadSamples(m_Id2, ref m_Samples2, ref m_Data2, out TriggerPoint2);
				if(Ret != 0)
				{
					string ErrorString;
					dig.GetErrorString(Ret, out ErrorString);
					textBoxInfo2.Text = "dig.ReadSamples = " + Ret.ToString() + " : " + ErrorString;
					return;
				}
				//---------------------------------
				// Display the retrieved data
				//---------------------------------
				for(int i = 0; i < m_Samples2; i++)
				{
					listBoxData2.Items.Add(String.Format("{0:D}\t{1:F3}", i+1, m_Data2[i])); 
				}
			}
		}

		/// <summary>
		/// Get status
		/// </summary>
		private void buttonInfo_Click(object sender, System.EventArgs e)
		{
			int		Ret;		// Return value
			int		Status;		// Status
			int		Samples;	// Number of retrieved samples

			//------------------------------------------------------------
			// Device1
			//------------------------------------------------------------
			//---------------------------------
			// Get the number of samples stored in driver memory
			//---------------------------------
			Ret = dig.GetDriverMemorySamples(m_Id1, out Samples);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.GetDriverMemorySamples = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			labelCount1.Text = Samples.ToString();
			//---------------------------------
			// Get the operation status
			//---------------------------------
			Ret = dig.GetStatus(m_Id1, out Status);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.GetStatus = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			labelStatus1.Text = Status.ToString("X") + "H";
			textBoxInfo1.Text = "Get status : Normality completion";

			//------------------------------------------------------------
			// Device2
			//------------------------------------------------------------
			//---------------------------------
			// Get the number of samples stored in driver memory
			//---------------------------------
			Ret = dig.GetDriverMemorySamples(m_Id2, out Samples);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.GetDriverMemorySamples = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			labelCount2.Text = Samples.ToString();
			//---------------------------------
			// Get the operation status
			//---------------------------------
			Ret = dig.GetStatus(m_Id2, out Status);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.GetStatus = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			labelStatus2.Text = Status.ToString("X") + "H";
			textBoxInfo2.Text = "Get status : Normality completion";		
		}

		/// <summary>
		/// Stop acquisition
		/// </summary>
		private void buttonStop_Click(object sender, System.EventArgs e)
		{
			int		Ret;		// Return value

			//------------------------------------------------------------
			// Device1
			//------------------------------------------------------------
			Ret = dig.Stop(m_Id1);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.Stop = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			textBoxInfo1.Text = "Stop acquisition : Normality completion";

			//------------------------------------------------------------
			// Device2
			//------------------------------------------------------------
			Ret = dig.Stop(m_Id2);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.Stop = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			textBoxInfo2.Text = "Stop acquisition : Normality completion";		
		}

		/// <summary>
		/// Exit handling
		/// </summary>
		private void buttonExit_Click(object sender, System.EventArgs e)
		{
			int		Ret;	// Return value

			//------------------------------------------------------------
			// Device1
			//------------------------------------------------------------
			Ret = dig.Exit(m_Id1);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo1.Text = "dig.Exit = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			textBoxInfo1.Text = "Exit : Normality completion";
			//------------------------------------------------------------
			// Device2
			//------------------------------------------------------------
			Ret = dig.Exit(m_Id2);
			if(Ret != 0)
			{
				string ErrorString;
				dig.GetErrorString(Ret, out ErrorString);
				textBoxInfo2.Text = "dig.Exit = " + Ret.ToString() + " : " + ErrorString;
				return;
			}
			textBoxInfo2.Text = "Exit : Normality completion";		
		}

		/// <summary>
		/// Close Form
		/// </summary>
		private void Form1_Closed(object sender, System.EventArgs e)
		{
			// Exit handling
			dig.Exit(m_Id1);
			dig.Exit(m_Id2);		
		}
	}
}
