﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CSmcCs;

namespace SyncMove
{
	/// <summary>
	/// Summary description for SyncMove.
	/// </summary>
	public class SyncMove : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.TextBox txtDeviceName;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ComboBox cboTargetAxis;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.Button btnStartSync;
		private System.Windows.Forms.Button btnGetSync;
		private System.Windows.Forms.Button btnSetSync;
		private System.Windows.Forms.CheckBox chkBoardCn3;
		private System.Windows.Forms.CheckBox chkBoardCn2;
		private System.Windows.Forms.CheckBox chkChip;
		private System.Windows.Forms.CheckBox chkAxis8;
		private System.Windows.Forms.CheckBox chkAxis7;
		private System.Windows.Forms.CheckBox chkAxis6;
		private System.Windows.Forms.CheckBox chkAxis5;
		private System.Windows.Forms.CheckBox chkAxis4;
		private System.Windows.Forms.CheckBox chkAxis3;
		private System.Windows.Forms.CheckBox chkAxis2;
		private System.Windows.Forms.CheckBox chkAxis1;
		private System.Windows.Forms.Label lblComment;
		private System.Windows.Forms.Label lblSetSync;
		private System.Windows.Forms.Label lblGetSync;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		int			dwRet;
		short		SyncAxis=0;
		short		SyncChip=0;
		short		SyncBoard=0;
		short		GetSyncAxis;
		short		GetSyncChip;
		short		GetSyncBoard;
		short		AxisNo=0;
		short		Id=0;
		string		ErrorString;

		CSmc		Smc	= new CSmc();

		public SyncMove()
		{
			//
			// 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.groupBox2 = new System.Windows.Forms.GroupBox();
			this.lblComment = new System.Windows.Forms.Label();
			this.btnExit = new System.Windows.Forms.Button();
			this.txtDeviceName = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.cboTargetAxis = new System.Windows.Forms.ComboBox();
			this.label2 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.lblGetSync = new System.Windows.Forms.Label();
			this.lblSetSync = new System.Windows.Forms.Label();
			this.btnStartSync = new System.Windows.Forms.Button();
			this.btnGetSync = new System.Windows.Forms.Button();
			this.btnSetSync = new System.Windows.Forms.Button();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.chkBoardCn3 = new System.Windows.Forms.CheckBox();
			this.chkBoardCn2 = new System.Windows.Forms.CheckBox();
			this.chkChip = new System.Windows.Forms.CheckBox();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.chkAxis8 = new System.Windows.Forms.CheckBox();
			this.chkAxis7 = new System.Windows.Forms.CheckBox();
			this.chkAxis6 = new System.Windows.Forms.CheckBox();
			this.chkAxis5 = new System.Windows.Forms.CheckBox();
			this.chkAxis4 = new System.Windows.Forms.CheckBox();
			this.chkAxis3 = new System.Windows.Forms.CheckBox();
			this.chkAxis2 = new System.Windows.Forms.CheckBox();
			this.chkAxis1 = new System.Windows.Forms.CheckBox();
			this.groupBox2.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.lblComment});
			this.groupBox2.Location = new System.Drawing.Point(8, 288);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(360, 120);
			this.groupBox2.TabIndex = 29;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "COMMENT";
			// 
			// lblComment
			// 
			this.lblComment.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblComment.Location = new System.Drawing.Point(8, 24);
			this.lblComment.Name = "lblComment";
			this.lblComment.Size = new System.Drawing.Size(344, 88);
			this.lblComment.TabIndex = 0;
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(272, 416);
			this.btnExit.Name = "btnExit";
			this.btnExit.Size = new System.Drawing.Size(96, 32);
			this.btnExit.TabIndex = 1;
			this.btnExit.Text = "EXIT";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// txtDeviceName
			// 
			this.txtDeviceName.AutoSize = false;
			this.txtDeviceName.Location = new System.Drawing.Point(104, 13);
			this.txtDeviceName.Name = "txtDeviceName";
			this.txtDeviceName.Size = new System.Drawing.Size(96, 22);
			this.txtDeviceName.TabIndex = 2;
			this.txtDeviceName.Text = "";
			this.txtDeviceName.TextChanged += new System.EventHandler(this.txtDeviceName_TextChanged);
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(79, 14);
			this.label1.TabIndex = 22;
			this.label1.Text = "Device Name";
			// 
			// cboTargetAxis
			// 
			this.cboTargetAxis.Location = new System.Drawing.Point(272, 16);
			this.cboTargetAxis.Name = "cboTargetAxis";
			this.cboTargetAxis.Size = new System.Drawing.Size(70, 23);
			this.cboTargetAxis.Sorted = true;
			this.cboTargetAxis.TabIndex = 3;
			this.cboTargetAxis.SelectedIndexChanged += new System.EventHandler(this.cboTargetAxis_SelectedIndexChanged);
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(216, 16);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(44, 14);
			this.label2.TabIndex = 24;
			this.label2.Text = "AxisNo";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.lblGetSync,
																					this.lblSetSync,
																					this.btnStartSync,
																					this.btnGetSync,
																					this.btnSetSync,
																					this.groupBox4,
																					this.chkChip,
																					this.groupBox3});
			this.groupBox1.Location = new System.Drawing.Point(8, 56);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(360, 224);
			this.groupBox1.TabIndex = 30;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "SYNCHRONIZATION";
			// 
			// lblGetSync
			// 
			this.lblGetSync.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblGetSync.Location = new System.Drawing.Point(88, 181);
			this.lblGetSync.Name = "lblGetSync";
			this.lblGetSync.Size = new System.Drawing.Size(112, 21);
			this.lblGetSync.TabIndex = 12;
			// 
			// lblSetSync
			// 
			this.lblSetSync.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblSetSync.Location = new System.Drawing.Point(88, 141);
			this.lblSetSync.Name = "lblSetSync";
			this.lblSetSync.Size = new System.Drawing.Size(112, 21);
			this.lblSetSync.TabIndex = 11;
			// 
			// btnStartSync
			// 
			this.btnStartSync.Location = new System.Drawing.Point(240, 152);
			this.btnStartSync.Name = "btnStartSync";
			this.btnStartSync.Size = new System.Drawing.Size(104, 32);
			this.btnStartSync.TabIndex = 9;
			this.btnStartSync.Text = "StartSync";
			this.btnStartSync.Click += new System.EventHandler(this.btnStartSync_Click);
			// 
			// btnGetSync
			// 
			this.btnGetSync.Location = new System.Drawing.Point(8, 176);
			this.btnGetSync.Name = "btnGetSync";
			this.btnGetSync.Size = new System.Drawing.Size(66, 30);
			this.btnGetSync.TabIndex = 8;
			this.btnGetSync.Text = "Get";
			this.btnGetSync.Click += new System.EventHandler(this.btnGetSync_Click);
			// 
			// btnSetSync
			// 
			this.btnSetSync.Location = new System.Drawing.Point(8, 136);
			this.btnSetSync.Name = "btnSetSync";
			this.btnSetSync.Size = new System.Drawing.Size(66, 30);
			this.btnSetSync.TabIndex = 7;
			this.btnSetSync.Text = "Set";
			this.btnSetSync.Click += new System.EventHandler(this.btnSetSync_Click);
			// 
			// groupBox4
			// 
			this.groupBox4.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.chkBoardCn3,
																					this.chkBoardCn2});
			this.groupBox4.Location = new System.Drawing.Point(256, 24);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(88, 96);
			this.groupBox4.TabIndex = 6;
			this.groupBox4.TabStop = false;
			this.groupBox4.Text = "Board";
			// 
			// chkBoardCn3
			// 
			this.chkBoardCn3.Location = new System.Drawing.Point(16, 64);
			this.chkBoardCn3.Name = "chkBoardCn3";
			this.chkBoardCn3.Size = new System.Drawing.Size(56, 16);
			this.chkBoardCn3.TabIndex = 6;
			this.chkBoardCn3.Text = "CN3";
			this.chkBoardCn3.CheckedChanged += new System.EventHandler(this.chkBoardCn3_CheckedChanged);
			// 
			// chkBoardCn2
			// 
			this.chkBoardCn2.Location = new System.Drawing.Point(16, 24);
			this.chkBoardCn2.Name = "chkBoardCn2";
			this.chkBoardCn2.Size = new System.Drawing.Size(56, 16);
			this.chkBoardCn2.TabIndex = 5;
			this.chkBoardCn2.Text = "CN2";
			this.chkBoardCn2.CheckedChanged += new System.EventHandler(this.chkBoardCn2_CheckedChanged);
			// 
			// chkChip
			// 
			this.chkChip.Location = new System.Drawing.Point(192, 56);
			this.chkChip.Name = "chkChip";
			this.chkChip.Size = new System.Drawing.Size(56, 16);
			this.chkChip.TabIndex = 5;
			this.chkChip.Text = "Chip";
			this.chkChip.CheckedChanged += new System.EventHandler(this.chkChip_CheckedChanged);
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.chkAxis8,
																					this.chkAxis7,
																					this.chkAxis6,
																					this.chkAxis5,
																					this.chkAxis4,
																					this.chkAxis3,
																					this.chkAxis2,
																					this.chkAxis1});
			this.groupBox3.Location = new System.Drawing.Point(8, 24);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(176, 96);
			this.groupBox3.TabIndex = 4;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Axis";
			// 
			// chkAxis8
			// 
			this.chkAxis8.Location = new System.Drawing.Point(136, 64);
			this.chkAxis8.Name = "chkAxis8";
			this.chkAxis8.Size = new System.Drawing.Size(32, 16);
			this.chkAxis8.TabIndex = 7;
			this.chkAxis8.Text = "8";
			this.chkAxis8.CheckedChanged += new System.EventHandler(this.chkAxis8_CheckedChanged);
			// 
			// chkAxis7
			// 
			this.chkAxis7.Location = new System.Drawing.Point(96, 64);
			this.chkAxis7.Name = "chkAxis7";
			this.chkAxis7.Size = new System.Drawing.Size(32, 16);
			this.chkAxis7.TabIndex = 6;
			this.chkAxis7.Text = "7";
			this.chkAxis7.CheckedChanged += new System.EventHandler(this.chkAxis7_CheckedChanged);
			// 
			// chkAxis6
			// 
			this.chkAxis6.Location = new System.Drawing.Point(56, 64);
			this.chkAxis6.Name = "chkAxis6";
			this.chkAxis6.Size = new System.Drawing.Size(32, 16);
			this.chkAxis6.TabIndex = 5;
			this.chkAxis6.Text = "6";
			this.chkAxis6.CheckedChanged += new System.EventHandler(this.chkAxis6_CheckedChanged);
			// 
			// chkAxis5
			// 
			this.chkAxis5.Location = new System.Drawing.Point(16, 64);
			this.chkAxis5.Name = "chkAxis5";
			this.chkAxis5.Size = new System.Drawing.Size(32, 16);
			this.chkAxis5.TabIndex = 4;
			this.chkAxis5.Text = "5";
			this.chkAxis5.CheckedChanged += new System.EventHandler(this.chkAxis5_CheckedChanged);
			// 
			// chkAxis4
			// 
			this.chkAxis4.Location = new System.Drawing.Point(136, 32);
			this.chkAxis4.Name = "chkAxis4";
			this.chkAxis4.Size = new System.Drawing.Size(32, 16);
			this.chkAxis4.TabIndex = 3;
			this.chkAxis4.Text = "4";
			this.chkAxis4.CheckedChanged += new System.EventHandler(this.chkAxis4_CheckedChanged);
			// 
			// chkAxis3
			// 
			this.chkAxis3.Location = new System.Drawing.Point(96, 32);
			this.chkAxis3.Name = "chkAxis3";
			this.chkAxis3.Size = new System.Drawing.Size(32, 16);
			this.chkAxis3.TabIndex = 2;
			this.chkAxis3.Text = "3";
			this.chkAxis3.CheckedChanged += new System.EventHandler(this.chkAxis3_CheckedChanged);
			// 
			// chkAxis2
			// 
			this.chkAxis2.Location = new System.Drawing.Point(56, 32);
			this.chkAxis2.Name = "chkAxis2";
			this.chkAxis2.Size = new System.Drawing.Size(32, 16);
			this.chkAxis2.TabIndex = 1;
			this.chkAxis2.Text = "2";
			this.chkAxis2.CheckedChanged += new System.EventHandler(this.chkAxis2_CheckedChanged);
			// 
			// chkAxis1
			// 
			this.chkAxis1.Location = new System.Drawing.Point(16, 32);
			this.chkAxis1.Name = "chkAxis1";
			this.chkAxis1.Size = new System.Drawing.Size(32, 16);
			this.chkAxis1.TabIndex = 0;
			this.chkAxis1.Text = "1";
			this.chkAxis1.CheckedChanged += new System.EventHandler(this.chkAxis1_CheckedChanged);
			// 
			// SyncMove
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(376, 454);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox1,
																		  this.groupBox2,
																		  this.btnExit,
																		  this.txtDeviceName,
																		  this.label1,
																		  this.label2,
																		  this.cboTargetAxis});
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "SyncMove";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "SyncMove";
			this.Load += new System.EventHandler(this.SyncMove_Load);
			this.groupBox2.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.groupBox4.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void SyncMove_Load(object sender, System.EventArgs e)
		{
			//-------------
			// Initialize
			//-------------
			AxisNo = 1;

			// AxisNo ComboBox
			string	wStr;
			for( int i = 0; i < 8; i++ )
			{
				wStr = string.Format("{0:d}", i+1 );
				cboTargetAxis.Items.Add(wStr);
			}

			// ComboBox Initialise
			cboTargetAxis.SelectedIndex = AxisNo - 1;

			string	DeviceName;
			DeviceName="SMC000";
			txtDeviceName.Text = DeviceName;

			dwRet = Smc.WInit(DeviceName, out Id);

			SyncAxis = 0;
			//------------------------
			// Get synchronous setting
			//------------------------
			dwRet = Smc.WGetSync(Id, out SyncAxis, out SyncChip, out SyncBoard);
			if( dwRet != 0 )
			{
				Smc.WGetErrorString(dwRet, out ErrorString);
				DispComment("SmcWGetSync", ErrorString);
			}

			short	Axis;
			for( int n = 0; n < 8; n++ )
			{
				Axis = (short)((0x1 << n) & 0xff);
				if( (SyncAxis & Axis) == Axis )
				{
					switch( n )
					{
						case 0:
							chkAxis1.Checked = true;
							break;
						case 1:
							chkAxis2.Checked = true;
							break;
						case 2:
							chkAxis3.Checked = true;
							break;
						case 3:
							chkAxis4.Checked = true;
							break;
						case 4:
							chkAxis5.Checked = true;
							break;
						case 5:
							chkAxis6.Checked = true;
							break;
						case 6:
							chkAxis7.Checked = true;
							break;
						case 7:
							chkAxis8.Checked = true;
							break;
					}
				}
			}
			if( SyncChip == 1 )
				chkChip.Checked = true;

			short	BoardCN;
			for( int m = 0; m < 2; m++)
			{
				BoardCN = (short)((1 << m) & 0xff);
				if( (SyncBoard & BoardCN) == BoardCN )
				{
					switch( m )
					{
						case 0:
							chkBoardCn2.Checked = true;
							break;
						case 1:
							chkBoardCn3.Checked = true;
							break;
					}
				}
			}
			DispSetSync();

		}

		private void DispComment(string func, string msg) 
		{
			lblComment.Text = string.Format("{0}: {1}", func, msg);
		}

		private void DispSetSync() 
		{
			lblSetSync.Text = string.Format("{0:x2}H {1:x2}H {2:x2}H", SyncAxis, SyncChip, SyncBoard);
		}

		private void chkAxis1_CheckedChanged(object sender, System.EventArgs e)
		{
			int	tmp = SyncAxis;
			if( chkAxis1.Checked == true )
			{
				tmp = tmp | 0x1;
				SyncAxis = (short)tmp;
			}
			else
			{
				SyncAxis = (short)(SyncAxis ^ 0x1);
			}
			DispSetSync();
		}

		private void chkAxis2_CheckedChanged(object sender, System.EventArgs e)
		{
			int	tmp = SyncAxis;
			if( chkAxis2.Checked == true )
			{
				tmp = tmp | 0x2;
				SyncAxis = (short)tmp;
			}
			else
			{
				SyncAxis = (short)(SyncAxis ^ 0x2);
			}
			DispSetSync();
		}

		private void chkAxis3_CheckedChanged(object sender, System.EventArgs e)
		{
			int	tmp = SyncAxis;
			if( chkAxis3.Checked == true )
			{
				tmp = tmp | 0x4;
				SyncAxis = (short)tmp;
			}
			else
			{
				SyncAxis = (short)(SyncAxis ^ 0x4);
			}
			DispSetSync();
		}

		private void chkAxis4_CheckedChanged(object sender, System.EventArgs e)
		{
			int	tmp = SyncAxis;
			if( chkAxis4.Checked == true )
			{
				tmp = tmp | 0x8;
				SyncAxis = (short)tmp;
			}
			else
			{
				SyncAxis = (short)(SyncAxis ^ 0x8);
			}
			DispSetSync();		
		}

		private void chkAxis5_CheckedChanged(object sender, System.EventArgs e)
		{
			int	tmp = SyncAxis;
			if( chkAxis5.Checked == true )
			{
				tmp = tmp | 0x10;
				SyncAxis = (short)tmp;
			}
			else
			{
				SyncAxis = (short)(SyncAxis ^ 0x10);
			}
			DispSetSync();
		}

		private void chkAxis6_CheckedChanged(object sender, System.EventArgs e)
		{
			int	tmp = SyncAxis;
			if( chkAxis6.Checked == true )
			{
				tmp = tmp | 0x20;
				SyncAxis = (short)tmp;
			}
			else
			{
				SyncAxis = (short)(SyncAxis ^ 0x20);
			}
			DispSetSync();
		}

		private void chkAxis7_CheckedChanged(object sender, System.EventArgs e)
		{
			int	tmp = SyncAxis;
			if( chkAxis7.Checked == true )
			{
				tmp = tmp | 0x40;
				SyncAxis = (short)tmp;
			}
			else
			{
				SyncAxis = (short)(SyncAxis ^ 0x40);
			}
			DispSetSync();
		}

		private void chkAxis8_CheckedChanged(object sender, System.EventArgs e)
		{
			int	tmp = SyncAxis;
			if( chkAxis8.Checked == true )
			{
				tmp = tmp | 0x80;
				SyncAxis = (short)tmp;
			}
			else
			{
				SyncAxis = (short)(SyncAxis ^ 0x80);
			}
			DispSetSync();
		}

		private void chkChip_CheckedChanged(object sender, System.EventArgs e)
		{
			if( chkChip.Checked == true )
				SyncChip =1;
			else
				SyncChip =0;

			DispSetSync();
		}

		private void chkBoardCn2_CheckedChanged(object sender, System.EventArgs e)
		{
			int	tmp = SyncBoard;
			if( chkBoardCn2.Checked == true )
			{
				tmp = tmp | 0x1;
				SyncBoard = (short)tmp;
			}
			else
			{
				SyncBoard = (short)(SyncBoard ^ 0x1);
			}
			DispSetSync();
		}

		private void chkBoardCn3_CheckedChanged(object sender, System.EventArgs e)
		{
			int	tmp = SyncBoard;
			if( chkBoardCn3.Checked == true )
			{
				tmp = tmp | 0x2;
				SyncBoard = (short)tmp;
			}
			else
			{
				SyncBoard = (short)(SyncBoard ^ 0x2);
			}
			DispSetSync();
		}

		private void cboTargetAxis_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			AxisNo = (short)(cboTargetAxis.SelectedIndex+1);
		}

		private void btnExit_Click(object sender, System.EventArgs e)
		{
			Smc.WExit(Id);
			this.Close();
		}

		private void btnGetSync_Click(object sender, System.EventArgs e)
		{
			dwRet = Smc.WGetSync(Id, out GetSyncAxis, out GetSyncChip, out GetSyncBoard);
			if( dwRet != 0 )
			{
				Smc.WGetErrorString(dwRet, out ErrorString);
				DispComment("SmcWGetSync", ErrorString);
			}
			lblGetSync.Text = string.Format("{0:x2}H {1:x2}H {2:x2}H", GetSyncAxis, GetSyncChip, GetSyncBoard);
		}

		private void btnSetSync_Click(object sender, System.EventArgs e)
		{
			dwRet = Smc.WSetSync(Id, SyncAxis, SyncChip, SyncBoard);
			if( dwRet != 0 )
			{
				Smc.WGetErrorString(dwRet, out ErrorString);
				DispComment("SmcWSetSync", ErrorString);
			}
		}

		private void btnStartSync_Click(object sender, System.EventArgs e)
		{
			dwRet = Smc.WSyncMotionStart(Id, AxisNo);
			if( dwRet != 0 )
			{
				Smc.WGetErrorString(dwRet, out ErrorString);
				DispComment("SmcWSyncMotionStart", ErrorString);
			}
		}

		private void txtDeviceName_TextChanged(object sender, System.EventArgs e)
		{
			string	wStr="OK";

			if( Id != -1 )
			{
				Smc.WExit(Id);
			}

            chkAxis1.Checked = false;
            chkAxis2.Checked = false;
            chkAxis3.Checked = false;
            chkAxis4.Checked = false;
            chkAxis5.Checked = false;
            chkAxis6.Checked = false;
            chkAxis7.Checked = false;
            chkAxis8.Checked = false;
            chkChip.Checked = false;
            chkBoardCn2.Checked = false;
            chkBoardCn3.Checked = false;

			wStr = txtDeviceName.Text;
			dwRet = Smc.WInit(wStr, out Id);
			if( dwRet != 0 )
			{
				Smc.WGetErrorString(dwRet, out ErrorString);
				DispComment("SmcWInit", ErrorString);
				Id = -1;
				return ;
			}

            SyncAxis = 0;
            //------------------------
            // Get synchronous setting
            //------------------------
            dwRet = Smc.WGetSync(Id, out SyncAxis, out SyncChip, out SyncBoard);
            if (dwRet != 0)
            {
                Smc.WGetErrorString(dwRet, out ErrorString);
                DispComment("SmcWGetSync", ErrorString);
            }

            short Axis;
            for (int n = 0; n < 8; n++)
            {
                Axis = (short)((0x1 << n) & 0xff);
                if ((SyncAxis & Axis) == Axis)
                {
                    switch (n)
                    {
                        case 0:
                            chkAxis1.Checked = true;
                            break;
                        case 1:
                            chkAxis2.Checked = true;
                            break;
                        case 2:
                            chkAxis3.Checked = true;
                            break;
                        case 3:
                            chkAxis4.Checked = true;
                            break;
                        case 4:
                            chkAxis5.Checked = true;
                            break;
                        case 5:
                            chkAxis6.Checked = true;
                            break;
                        case 6:
                            chkAxis7.Checked = true;
                            break;
                        case 7:
                            chkAxis8.Checked = true;
                            break;
                    }
                }
            }
            if (SyncChip == 1)
                chkChip.Checked = true;

            short BoardCN;
            for (int m = 0; m < 2; m++)
            {
                BoardCN = (short)((1 << m) & 0xff);
                if ((SyncBoard & BoardCN) == BoardCN)
                {
                    switch (m)
                    {
                        case 0:
                            chkBoardCn2.Checked = true;
                            break;
                        case 1:
                            chkBoardCn3.Checked = true;
                            break;
                    }
                }
            }
            DispSetSync();

            wStr = "OK";
            lblComment.Text = wStr;
		}
	}
}
