using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using SmartX;
using INT63DC_ImageDll;
using INT63DC_2C.DialogForms;
using INT63DC_2C.SerialManger;

namespace INT63DC_2C.Forms
{
    public partial class FormCommunication : Form
    {
        #region Field
        string[] HexArray;

        private FormMain m_ParentForm;
        #endregion

        #region Constructor
        public FormCommunication(FormMain parent)
        {
            InitializeComponent();

            this.ParentForm = parent;

            this.InitializeDesign();
            this.InitializeControl();
            this.DefaultSetting();
        }
        #endregion

        #region Property
        public FormMain ParentForm
        {
            get { return this.m_ParentForm; }
            set { this.m_ParentForm = value; }
        }
        #endregion

        #region Method
        private void InitializeDesign()
        {
            ImageDll images = new ImageDll();

            switch (this.ParentForm.SystemConfig.Language)
            {
                case DataStore.E_LanguageID.Korean:
                    break;
                case DataStore.E_LanguageID.English:
                    #region English
                    this.labelTitle.Text = "Communication";
                    // Serial
                    this.groupBoxSerial.Text = "Communication";
                    this.labelTitleSerial3Baudrate.Text = "Baudrate";
                    this.labelTitleSerial3Mode.Text = "Mode";
                    // TCP
                    this.labelTitleTcpOperationMode.Text = "Operation mode";
                    this.labelTitleTcpMode.Text = "mode";

                    this.labelHelp.Text = "※ After setup, reboot is required.";

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.engBackUp)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Chinese:
                    #region Chinese
                    this.labelTitle.Text = "通讯设置";
                    // Serial
                    this.groupBoxSerial.Text = "通信";
                    this.labelTitleSerial3Baudrate.Text = "波特率";
                    this.labelTitleSerial3Mode.Text = "模式";
                    this.buttonSerial3Send.Text = "发送";
                    this.buttonSave3.Text = "保存";
                    // TCP 
                    this.groupBoxEthernet.Text = "以太网";
                    this.labelTitleSetting.Text = "设置";
                    this.labelTitleIPAddress.Text = "IP地址";
                    this.labelTitleSubnetMask.Text = "子网屏蔽码";
                    this.labelTitleGateway.Text = "网关";
                    this.labelTitleTcpOperationMode.Text = "运转模式";
                    this.labelTitleTcpMode.Text = "模式";
                    this.labelTitleServer.Text = "服务器";
                    this.labelTitleConnectedIP.Text = "连接的IP";
                    this.labelTitleServerPort.Text = "端口";
                    this.labelTitleClient.Text = "客户端";
                    this.labelTitleClientServerIP.Text = "服务器 IP 地址";
                    this.labelTitleClientServerPort.Text = "服务器端口";
                   
                    this.buttonTcpSave1.Text = this.buttonTcpSave2.Text = "保存";
                    this.buttonTcpSend.Text = "发送";
                    this.buttonStart.Text = "开启";
                    this.buttonStop.Text = "停止";

                    this.labelHelp.Text = "※ 设置完成后需要重启";

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.chnBackUp)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Czech:
                    #region Czech
                    this.labelTitle.Text = "Nastavení komunikace";
                    // Serial
                    this.groupBoxSerial.Text = "Nastavení komunikace";
                    this.labelTitleSerial3Baudrate.Text = "Přenosová rychlost";
                    this.labelTitleSerial3Mode.Text = "Režim";
                    this.buttonSave3.Text = "Uložit";
                    // TCP
                    this.labelTitleTcpOperationMode.Text = "operační mód";
                    this.labelTitleTcpMode.Text = "Režim";

                    this.labelHelp.Text = "※ Po nastavení je nutný restart.";

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.czeBackUp)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.German:
                    #region German
                    this.labelTitle.Text = "Kommunikationseinstellungen";
                    // Serial
                    this.groupBoxSerial.Text = "Kommunikationseinstellung";
                    this.labelTitleSerial3Baudrate.Text = "Baudrate";
                    this.labelTitleSerial3Mode.Text = "Modus";
                    this.buttonSerial3Send.Text = "Senden";
                    this.buttonSave3.Text = "Speichern";

                    this.labelHelp.Text = "※ Nach der Einrichtung ist ein Neustart erforderlich";

                    this.buttonBack.DisableImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDisable));
                    this.buttonBack.DownImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackDown));
                    this.buttonBack.UpImage = new Bitmap(images.GetImage(ImageDll.ButtonImages.gerBackUp)); 
                    #endregion
                    break;
                case DataStore.E_LanguageID.Japanese:
                    break;
                default:
                    break;
            }
        }
        private void InitializeControl()
        {
            #region Serial
            this.comboBoxSerial3BaudRate.Items.Clear();
            this.comboBoxSerial3BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);
            this.comboBoxSerial3BaudRate.Items.Add(9600);
            this.comboBoxSerial3BaudRate.Items.Add(19200);
            this.comboBoxSerial3BaudRate.Items.Add(38400);
            this.comboBoxSerial3BaudRate.Items.Add(115200);
            this.comboBoxSerial3BaudRate.SelectedIndex = 0;
            this.comboBoxSerial3BaudRate.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);

            this.comboBoxSerial3Mode.Items.Clear();
            this.comboBoxSerial3Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);
            this.comboBoxSerial3Mode.Items.Add("None");
            this.comboBoxSerial3Mode.Items.Add("STD1");
            this.comboBoxSerial3Mode.Items.Add("STD2");
            this.comboBoxSerial3Mode.Items.Add("STD3");
            this.comboBoxSerial3Mode.SelectedIndex = 0;
            this.comboBoxSerial3Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);
            #endregion

            #region TCP
            this.comboBoxTcpOperationMode.Items.Clear();
            this.comboBoxTcpOperationMode.SelectedIndexChanged -= new EventHandler(this.comboBoxTcpOperationMode_SelectedIndexChanged);
            this.comboBoxTcpOperationMode.Items.Add("None");
            this.comboBoxTcpOperationMode.Items.Add("Server");
            this.comboBoxTcpOperationMode.Items.Add("Client");
            this.comboBoxTcpOperationMode.SelectedIndex = 0;
            this.comboBoxTcpOperationMode.SelectedIndexChanged += new EventHandler(this.comboBoxTcpOperationMode_SelectedIndexChanged);

            this.comboBoxTcpMode.Items.Clear();
            this.comboBoxTcpMode.SelectedIndexChanged -= new EventHandler(this.comboBoxTcpMode_SelectedIndexChanged);
            this.comboBoxTcpMode.Items.Add("None");
            this.comboBoxTcpMode.Items.Add("STD1");
            this.comboBoxTcpMode.Items.Add("STD2");
            this.comboBoxTcpMode.Items.Add("STD3");
            this.comboBoxTcpMode.SelectedIndex = 0;
            this.comboBoxTcpMode.SelectedIndexChanged += new EventHandler(this.comboBoxTcpMode_SelectedIndexChanged);
            #endregion
        }
        private void DefaultSetting()
        {
            string[] sValue;

            if (this.ParentForm.smartConfigs.IPSettings.DHCPEnable != 0)
            {
                this.ParentForm.smartConfigs.IPSettings.DHCPEnable = 0;
                this.ParentForm.smartConfigs.IPSettings.Save();
                this.ParentForm.smartConfigs.IPSettings.SetApply();
            }

            #region Serial
            this.comboBoxSerial3BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);
            this.comboBoxSerial3BaudRate.SelectedIndex = this.ParentForm.SystemConfig.Serial3BaudRate;
            this.comboBoxSerial3BaudRate.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);

            this.comboBoxSerial3Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);
            this.comboBoxSerial3Mode.SelectedIndex = this.ParentForm.SystemConfig.Serial3Mode;
            this.comboBoxSerial3Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);

            if (this.ParentForm.SystemConfig.Serial3Mode != 0)
                this.buttonSerial3Send.Enabled = true;
            else
                this.buttonSerial3Send.Enabled = false;
            #endregion

            #region TCP
            if (this.ParentForm.SystemConfig.IsEthernetEnable == true)
            {
                this.buttonStart.Enabled = true;
                this.buttonStop.Enabled = true;
            }
            else
            {
                this.buttonStart.Enabled = false;
                this.buttonStop.Enabled = false;
            }

            this.labelServerStatus.Text = "-";
            this.labelClientStatus.Text = "-";
            this.labelConnectedIP.Text = "-";

            sValue = this.ParentForm.SystemConfig.EthernetServerAddress.Split('.');
            this.labelHostIP1.Text = sValue[0];
            this.labelHostIP2.Text = sValue[1];
            this.labelHostIP3.Text = sValue[2];
            this.labelHostIP4.Text = sValue[3];

            this.labelHostPort.Text = this.ParentForm.SystemConfig.EthernetServerPort.ToString();
            this.labelLocalPort.Text = this.ParentForm.SystemConfig.EthernetLocalPort.ToString();
            //this.labelLocalPort.Text = this.ParentForm.smartTCPMultiServer.Port.ToString();

            this.buttonTcpSave1.Enabled = false;
            this.buttonTcpSave2.Enabled = false;
            #endregion
        }

        private void EthernetStart()
        {
            string detail = "";

            this.buttonStart.Enabled = true;
            this.buttonStop.Enabled = true;

            if (this.ParentForm.SystemConfig.EthernetOperationMode == 1)
            {
                detail = "Server";

                if (string.Compare(this.ParentForm.smartConfigs.IPSettings.DeviceIP, "0.0.0.0") == 0)
                {
                    if (this.labelServerStatus.Text != ">Addr Err")
                        this.labelServerStatus.Text = ">Addr Err";

                    this.timerServer.Enabled = false;
                }
                else
                {
                    this.ParentForm.EthernetServerStart();

                    if (this.timerServer.Enabled == false)
                        this.timerServer.Enabled = true;
                }
            }
            else if (this.ParentForm.SystemConfig.EthernetOperationMode == 2)
            {
                detail = "Client";
                this.ParentForm.EthernetClientConnect();
            }

            this.ParentForm.SetTrackingHistoryData(DataStore.E_TrackingOperation.TryEthernetStart, detail);
        }
        private void EthernetStop()
        {
            string detail = "";

            if (this.ParentForm.SystemConfig.EthernetOperationMode == 1)
            {
                detail = "Server";

                this.timerServer.Enabled = false;
                this.ParentForm.EthernetServerStop();

                if (this.labelServerStatus.Text != ">Stop")
                    this.labelServerStatus.Text = ">Stop";
            }
            else if (this.ParentForm.SystemConfig.EthernetOperationMode == 2)
            {
                detail = "Client";
                this.ParentForm.EthernetClientDisconnect();
            }

            this.ParentForm.SetTrackingHistoryData(DataStore.E_TrackingOperation.TryEthernetStop, detail);
        }
        private void EthernetDataTestSend()
        {
            string data = "";

            if (this.ParentForm.SystemConfig.EthernetMode == 1)
                data = this.ParentForm.Protocol_STD1(this.ParentForm.SystemConfig, this.ParentForm.CollectionWeightData);
            else if (this.ParentForm.SystemConfig.EthernetMode == 2)
                data = this.ParentForm.Protocol_STD2(this.ParentForm.SystemConfig, this.ParentForm.CollectionWeightData, this.ParentForm.CurrentProductItem);
            else if (this.ParentForm.SystemConfig.EthernetMode == 3)
                data = this.ParentForm.Protocol_STD3(this.ParentForm.SystemConfig, this.ParentForm.CollectionWeightData, this.ParentForm.CurrentProductItem);

            switch (this.ParentForm.SystemConfig.EthernetOperationMode)
            {
                case 1:
                    if (this.ParentForm.smartTCPMultiServer.ClientInfoList != null)
                    {
                        foreach (SmartTCPMultiServer.CLIENTSINFOS info in this.ParentForm.smartTCPMultiServer.ClientInfoList)
                        {
                            this.ParentForm.EthernetServerSendMessage(info, data);
                        }
                    }
                    break;
                case 2:
                    this.ParentForm.EthernetSendMessage(data);
                    break;
                default:
                    break;
            }
        }
        private void UpdateProtocolDescriptionCom3Display()
        {
            if (this.comboBoxSerial3Mode.SelectedIndex == 1)
            {
                this.labelProtocolDescriptionCOM3_1.Text = "STX[1],ID[5],NO[4],";
                this.labelProtocolDescriptionCOM3_2.Text = "{GRD[1],WEIGHT[6]} * n,CHK8[2],ETX[1]";
            }
            else if (this.comboBoxSerial3Mode.SelectedIndex == 2)
            {
                this.labelProtocolDescriptionCOM3_1.Text = "STX[1],ID[5],NO[4],UNDER[6],PASS[6],OVER[6],";
                this.labelProtocolDescriptionCOM3_2.Text = "TARE[6],{GRD[1],WEIGHT[6]} * n,CHK8[2],ETX[1]";
            }
            else if (this.comboBoxSerial3Mode.SelectedIndex == 3)
            {
                this.labelProtocolDescriptionCOM3_1.Text = "STX[1],ID[5],NO[4],UNDER[6],PASS[6],OVER[6],TARE[6]";
                this.labelProtocolDescriptionCOM3_2.Text = "PASS CNT[7],NG CNT[7],{GRD[1],WEIGHT[6]} * n,ETX[1]";
            }
            else
            {
                this.labelProtocolDescriptionCOM3_1.Text = "";
                this.labelProtocolDescriptionCOM3_2.Text = "";
            }
        }
        private void UpdateProtocolDescriptionTcpDisplay()
        {
            if (this.comboBoxTcpMode.SelectedIndex == 1)
            {
                this.labelProtocolDescriptionEthernet_1.Text = "STX[1],ID[5],NO[4],";
                this.labelProtocolDescriptionEthernet_2.Text = "{GRD[1],WEIGHT[6]} * n,CHK8[2],ETX[1]";
            }
            else if (this.comboBoxTcpMode.SelectedIndex == 2)
            {
                this.labelProtocolDescriptionEthernet_1.Text = "STX[1],ID[5],NO[4],UNDER[6],PASS[6],OVER[6],";
                this.labelProtocolDescriptionEthernet_2.Text = "TARE[6],{GRD[1],WEIGHT[6]} * n,CHK8[2],ETX[1]";
            }
            else if (this.comboBoxSerial3Mode.SelectedIndex == 3)
            {
                this.labelProtocolDescriptionCOM3_1.Text = "STX[1],ID[5],NO[4],UNDER[6],PASS[6],OVER[6],TARE[6]";
                this.labelProtocolDescriptionCOM3_2.Text = "PASS CNT[7],NG CNT[7],{GRD[1],WEIGHT[6]} * n,ETX[1]";
            }
            else
            {
                this.labelProtocolDescriptionEthernet_1.Text = "";
                this.labelProtocolDescriptionEthernet_2.Text = "";
            }
        }
        private void UpdateMACAddressDisplay()
        {
            this.buttonSave4.Enabled = false;

            byte[] currentMAC = this.ParentForm.smartConfigs.IPSettings.GetMACAddress();
            string hex = BitConverter.ToString(currentMAC);
            this.HexArray = hex.Split('-');

            if (this.HexArray.Length >= 6)
            {
                this.labelMACAddress1.Text = this.HexArray[0] + ":" + this.HexArray[1] + ":" + this.HexArray[2] + ":";
                this.labelMACAddress4.Text = this.HexArray[3];
                this.labelMACAddress5.Text = this.HexArray[4];
                this.labelMACAddress6.Text = this.HexArray[5];
            }
        }
        private void LocalIPEnable(bool enable)
        {
            Color tempColor;

            if (enable == true)
                tempColor = Color.Black;
            else
                tempColor = Color.Gray;

            #region Color
            this.labelLocalIP1.ForeColor = tempColor;
            this.labelLocalIP2.ForeColor = tempColor;
            this.labelLocalIP3.ForeColor = tempColor;
            this.labelLocalIP4.ForeColor = tempColor;

            this.labelLocalSubnetMask1.ForeColor = tempColor;
            this.labelLocalSubnetMask2.ForeColor = tempColor;
            this.labelLocalSubnetMask3.ForeColor = tempColor;
            this.labelLocalSubnetMask4.ForeColor = tempColor;

            this.labelLocalGateway1.ForeColor = tempColor;
            this.labelLocalGateway2.ForeColor = tempColor;
            this.labelLocalGateway3.ForeColor = tempColor;
            this.labelLocalGateway4.ForeColor = tempColor;
            #endregion

            #region Enable
            this.labelLocalIP1.Enabled = enable;
            this.labelLocalIP2.Enabled = enable;
            this.labelLocalIP3.Enabled = enable;
            this.labelLocalIP4.Enabled = enable;

            this.labelLocalSubnetMask1.Enabled = enable;
            this.labelLocalSubnetMask2.Enabled = enable;
            this.labelLocalSubnetMask3.Enabled = enable;
            this.labelLocalSubnetMask4.Enabled = enable;

            this.labelLocalGateway1.Enabled = enable;
            this.labelLocalGateway2.Enabled = enable;
            this.labelLocalGateway3.Enabled = enable;
            this.labelLocalGateway4.Enabled = enable;
            #endregion
        }
        private void UpdateLocalIPAddressDisplay()
        {
            string[] sValue;


            // Local IP
            sValue = this.ParentForm.smartConfigs.IPSettings.DeviceIP.Split('.');
            if (sValue.Length == 4)
            {
                this.labelLocalIP1.Text = sValue[0];
                this.labelLocalIP2.Text = sValue[1];
                this.labelLocalIP3.Text = sValue[2];
                this.labelLocalIP4.Text = sValue[3];
            }

            // Local Subnet Mask
            sValue = this.ParentForm.smartConfigs.IPSettings.SubNetMask.Split('.');
            if (sValue.Length == 4)
            {
                this.labelLocalSubnetMask1.Text = sValue[0];
                this.labelLocalSubnetMask2.Text = sValue[1];
                this.labelLocalSubnetMask3.Text = sValue[2];
                this.labelLocalSubnetMask4.Text = sValue[3];
            }

            // Local GateWay
            sValue = this.ParentForm.smartConfigs.IPSettings.GateWay.Split('.');
            if (sValue.Length == 4)
            {
                this.labelLocalGateway1.Text = sValue[0];
                this.labelLocalGateway2.Text = sValue[1];
                this.labelLocalGateway3.Text = sValue[2];
                this.labelLocalGateway4.Text = sValue[3];
            }
        }
        public void DisplayLabelLocalPort(bool bValue)
        {
            if (bValue == false)
            {
                this.labelLocalPort.Enabled = false;
                this.labelLocalPort.BackColor = Color.Silver;
            }
            else
            {
                this.labelLocalPort.Enabled = true;
                this.labelLocalPort.BackColor = Color.White;
            }
        }
        public void UpdateEthernetStatusDisplay(string value)
        {
            if (this.labelClientStatus.Text != value)
                this.labelClientStatus.Text = value;
        }

        public void DisplayRefresh()
        {
            // MAC Address
            this.UpdateMACAddressDisplay();

            #region Serial
            this.buttonSave3.Enabled = false;

            this.comboBoxSerial3BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);
            this.comboBoxSerial3BaudRate.SelectedItem = this.ParentForm.SystemConfig.Serial3BaudRate;
            this.comboBoxSerial3BaudRate.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);

            this.comboBoxSerial3Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);
            this.comboBoxSerial3Mode.SelectedIndex = this.ParentForm.SystemConfig.Serial3Mode;
            this.comboBoxSerial3Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3_SelectedIndexChanged);

            this.UpdateProtocolDescriptionCom3Display();

            if (this.ParentForm.SystemConfig.Serial3Mode != 0)
                this.buttonSerial3Send.Enabled = true;
            else
                this.buttonSerial3Send.Enabled = false;
            #endregion

            #region TCP
            // Mode
            this.comboBoxTcpMode.SelectedIndexChanged -= new EventHandler(this.comboBoxTcpMode_SelectedIndexChanged);
            this.comboBoxTcpMode.SelectedIndex = this.ParentForm.SystemConfig.EthernetMode;
            this.comboBoxTcpMode.SelectedIndexChanged += new EventHandler(this.comboBoxTcpMode_SelectedIndexChanged);

            // Operation Mode
            this.comboBoxTcpOperationMode.SelectedIndexChanged -= new EventHandler(this.comboBoxTcpOperationMode_SelectedIndexChanged);
            this.comboBoxTcpOperationMode.SelectedIndex = this.ParentForm.SystemConfig.EthernetOperationMode;
            this.comboBoxTcpOperationMode.SelectedIndexChanged += new EventHandler(this.comboBoxTcpOperationMode_SelectedIndexChanged);

            this.labelHostPort.Text = this.ParentForm.SystemConfig.EthernetServerPort.ToString();
            this.labelLocalPort.Text = this.ParentForm.SystemConfig.EthernetLocalPort.ToString();

            // Local IP
            try
            {
                this.UpdateLocalIPAddressDisplay();
            }
            catch
            {
                this.ParentForm.smartConfigs.IPSettings.DeviceIP = "192.168.11.205";
                this.ParentForm.smartConfigs.IPSettings.SubNetMask = "255.255.255.0";
                this.ParentForm.smartConfigs.IPSettings.GateWay = "192.168.11.1";
            }
            // Format
            this.UpdateProtocolDescriptionTcpDisplay();

            if (this.comboBoxTcpOperationMode.SelectedIndex == 1)
            {
                this.buttonStart.Enabled = true;
                this.buttonStop.Enabled = true;

                if (string.Compare(this.ParentForm.smartConfigs.IPSettings.DeviceIP, "0.0.0.0") == 0)
                {
                    if (this.labelServerStatus.Text != ">Addr Err")
                        this.labelServerStatus.Text = ">Addr Err";

                    this.timerServer.Enabled = false;
                }
                else
                {
                    this.ParentForm.EthernetServerStart();

                    if (this.timerServer.Enabled != true)
                        this.timerServer.Enabled = true;
                }
            }
            else if (this.comboBoxTcpOperationMode.SelectedIndex == 2)
            {
                this.buttonStart.Enabled = true;
                this.buttonStop.Enabled = true;

                this.ParentForm.EthernetClientConnect();
            }
            else
            {
                this.buttonStart.Enabled = false;
                this.buttonStop.Enabled = false;

                this.labelServerStatus.Text = "-";
                this.labelClientStatus.Text = "-";
                this.labelConnectedIP.Text = "-";
            }

            this.buttonTcpSave1.Enabled = false;
            this.buttonTcpSave2.Enabled = false;

            if (this.ParentForm.SystemConfig.EthernetMode != 0)
                this.buttonTcpSend.Enabled = true;
            else
                this.buttonTcpSend.Enabled = false;
            #endregion
        }
        #endregion

        #region Event Handler
        private void buttonBack_Click(object sender, EventArgs e)
        {
            this.ParentForm.ChildFormMenu.DisplayRefresh();
            ((FormMain)(Owner)).smartForm.Show((int)DataStore.FormStore.FormMenu);
        }

        // Serial
        private void buttonSend_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.SystemConfig.Serial3Mode == 1)
                this.ParentForm.SerialCH3STD1(this.ParentForm.CollectionWeightData);
            else if (this.ParentForm.SystemConfig.Serial3Mode == 2)
                this.ParentForm.SerialCH3STD2(this.ParentForm.CollectionWeightData);
            else if (this.ParentForm.SystemConfig.Serial3Mode == 3)
                this.ParentForm.SerialCH3STD3(this.ParentForm.CollectionWeightData);
        }
        private void buttonSave3_Click(object sender, EventArgs e)
        {
            string beforeBr = "", beforeMode = "", afterBr = "", afterMode = "";

            // before : BaudRate
            if (this.ParentForm.SystemConfig.Serial3BaudRate == 0)
                beforeBr = "9600";
            else if (this.ParentForm.SystemConfig.Serial3BaudRate == 1)
                beforeBr = "19200";
            else if (this.ParentForm.SystemConfig.Serial3BaudRate == 2)
                beforeBr = "38400";
            else if (this.ParentForm.SystemConfig.Serial3BaudRate == 3)
                beforeBr = "115200";
            else
                beforeBr = "9600";

            // before : SerialMode
            if (this.ParentForm.SystemConfig.Serial3Mode == 0)
                beforeMode = "None";
            else if (this.ParentForm.SystemConfig.Serial3Mode == 1)
                beforeMode = "STD1";
            else if (this.ParentForm.SystemConfig.Serial3Mode == 2)
                beforeMode = "STD2";
            else if (this.ParentForm.SystemConfig.Serial3Mode == 3)
                beforeMode = "STD3";
            else
                beforeMode = "None";

            int baudrate = 9600;
            this.ParentForm.SystemConfig.Serial3BaudRate = this.comboBoxSerial3BaudRate.SelectedIndex;
            this.ParentForm.SystemConfig.Serial3Mode = this.comboBoxSerial3Mode.SelectedIndex;
            this.ParentForm.SaveSystemConfigurationFile1(this.ParentForm.SystemConfig);

            SerialMgrComm.serialPortClose("COM3:");

            // after : BaudRate
            if (this.ParentForm.SystemConfig.Serial3BaudRate == 0)
            {
                baudrate = 9600;
                afterBr = "9600";
            }
            else if (this.ParentForm.SystemConfig.Serial3BaudRate == 1)
            {
                baudrate = 19200;
                afterBr = "19200";
            }
            else if (this.ParentForm.SystemConfig.Serial3BaudRate == 2)
            {
                baudrate = 38400;
                afterBr = "38400";
            }
            else if (this.ParentForm.SystemConfig.Serial3BaudRate == 3)
            {
                baudrate = 115200;
                afterBr = "115200";
            }
            else
            {
                baudrate = 9600;
                afterBr = "9600";
            }

            // after : SerialMode
            if (this.ParentForm.SystemConfig.Serial3Mode == 0)
                afterMode = "None";
            else if (this.ParentForm.SystemConfig.Serial3Mode == 1)
                afterMode = "STD1";
            else if (this.ParentForm.SystemConfig.Serial3Mode == 2)
                afterMode = "STD2";
            else if (this.ParentForm.SystemConfig.Serial3Mode == 3)
                afterMode = "STD3";
            else
                afterMode = "None";

            if (this.ParentForm.SystemConfig.Serial3Mode != 0)
            {
                SerialMgrComm.serialPortOpen("COM3:", baudrate, 0, 0, 8);
                this.buttonSerial3Send.Enabled = true;
            }
            else
                this.buttonSerial3Send.Enabled = false;

            this.buttonSave3.Enabled = false;

            // Part11
            if (this.ParentForm.SystemConfig.IsPart11 == true)
            {
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.Com3Baudrate, "", beforeBr, afterBr);
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.Com3Mode, "", beforeMode, afterMode);
            }
        }
        private void comboBoxSerial3_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.UpdateProtocolDescriptionCom3Display();

            this.buttonSave3.Enabled = true;
        }

        // TCP
        private void buttonStart_Click(object sender, EventArgs e)
        {
            this.EthernetStart();
        }
        private void buttonStop_Click(object sender, EventArgs e)
        {
            this.EthernetStop();
        }
        private void buttonTcpSend_Click(object sender, EventArgs e)
        {
            this.EthernetDataTestSend();
        }

        private void timerServer_Tick(object sender, EventArgs e)
        {
            if (this.ParentForm.smartTCPMultiServer.IsStart == true)
            {
                if (this.labelServerStatus.Text != ">Start")
                    this.labelServerStatus.Text = ">Start";

                this.smartListBoxClientList.ClearAll();
                this.labelClientCount.Text = "-";
                this.labelConnectedIP.Text = "-";

                if (this.ParentForm.smartTCPMultiServer.ClientInfoList != null)
                {
                    for (int i = 0; i < this.ParentForm.smartTCPMultiServer.ClientInfoList.Length; i++)
                    {
                        this.smartListBoxClientList.AddItem(
                            this.ParentForm.smartTCPMultiServer.ClientInfoList[i].strID + ", " +
                            this.ParentForm.smartTCPMultiServer.ClientInfoList[i].strIPAddress);
                    }

                    this.labelClientCount.Text = this.ParentForm.smartTCPMultiServer.ClientInfoList.Length.ToString();
                    this.labelConnectedIP.Text = this.ParentForm.smartTCPMultiServer.ClientInfoList[0].strIPAddress;
                }
            }
            else
            {
                if (this.labelServerStatus.Text != ">Stop")
                    this.labelServerStatus.Text = ">Stop";

                this.smartListBoxClientList.ClearAll();
                this.labelClientCount.Text = "-";
            }
        }

        private void comboBoxTcpOperationMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            string before = "", after = "";

            if (this.ParentForm.SystemConfig.EthernetOperationMode == 1)
                before = "Server";
            else if (this.ParentForm.SystemConfig.EthernetOperationMode == 2)
                before = "Client";
            else
                before = "None";

            if (this.comboBoxTcpOperationMode.SelectedIndex == 1)
            {
                #region Server
                this.ParentForm.EthernetClientDisconnect();
                this.ParentForm.ChildFormMainDisplay.TimerServer(true);

                this.buttonStart.Enabled = true;
                this.buttonStop.Enabled = true;
                #endregion
            }
            else if (this.comboBoxTcpOperationMode.SelectedIndex == 2)
            {
                #region Client
                this.labelServerStatus.Text = "-";
                this.timerServer.Enabled = false;
                this.ParentForm.EthernetServerStop();
                this.ParentForm.EthernetClientDisconnect();
                this.ParentForm.ChildFormMainDisplay.TimerServer(false);

                this.buttonStart.Enabled = true;
                this.buttonStop.Enabled = true;
                #endregion
            }
            else
            {
                #region None
                this.timerServer.Enabled = false;
                this.ParentForm.EthernetServerStop();
                this.ParentForm.EthernetClientDisconnect();
                this.ParentForm.ChildFormMainDisplay.TimerServer(false);

                this.buttonStart.Enabled = false;
                this.buttonStop.Enabled = false;

                this.labelServerStatus.Text = "-";
                this.labelClientStatus.Text = "-";
                this.labelConnectedIP.Text = "-";
                #endregion
            }

            this.ParentForm.SystemConfig.EthernetOperationMode = this.comboBoxTcpOperationMode.SelectedIndex;
            this.ParentForm.SaveSystemConfigurationFile3(this.ParentForm.SystemConfig);
            after = this.comboBoxTcpOperationMode.SelectedItem.ToString();

            // Part11
            if (this.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.TCPOpMode, "", before, after);
        }
        private void comboBoxTcpMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            string before = "", after = "";

            if (this.ParentForm.SystemConfig.EthernetMode == 1)
                before = "STD1";
            else if (this.ParentForm.SystemConfig.EthernetMode == 2)
                before = "STD2";
            else if (this.ParentForm.SystemConfig.EthernetMode == 3)
                before = "STD3";
            else
                before = "None";

            this.ParentForm.SystemConfig.EthernetMode = this.comboBoxTcpMode.SelectedIndex;
            this.ParentForm.SaveSystemConfigurationFile3(this.ParentForm.SystemConfig);

            this.UpdateProtocolDescriptionTcpDisplay();

            if (this.ParentForm.SystemConfig.EthernetMode != 0)
                this.buttonTcpSend.Enabled = true;
            else
                this.buttonTcpSend.Enabled = false;

            after = this.comboBoxTcpMode.SelectedItem.ToString();

            // Part11
            if (this.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.TCPComMode, "", before, after);
        }

        private void labelStartAddress_Click(object sender, EventArgs e)
        {
            string before = "", after = "";

            before = this.labelStartAddress.Text;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelStartAddress.Text, 5, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.doubleValue < 0 || myKeyPad.doubleValue > 65535)
                {
                    // 입력범위를 확인하세요
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelStartAddress.Text = myKeyPad.StringValue;
                    this.ParentForm.SystemConfig.ModbusTcpStartAddress = myKeyPad.IntValue;
                    after = myKeyPad.StringValue;

                    this.ParentForm.SaveSystemConfigurationFile3(this.ParentForm.SystemConfig);

                    // Part11
                    if (this.ParentForm.SystemConfig.IsPart11 == true)
                        this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.Mod_StartAddr, "", before, after);
                }
            }
        }
        private void labelLocalIP1_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelLocalIP1.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalIP1.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalIP2_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelLocalIP2.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalIP2.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalIP3_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelLocalIP3.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalIP3.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalIP4_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelLocalIP4.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalIP4.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalSubnetMask1_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelLocalSubnetMask1.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalSubnetMask1.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalSubnetMask2_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelLocalSubnetMask2.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalSubnetMask2.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalSubnetMask3_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelLocalSubnetMask3.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalSubnetMask3.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalSubnetMask4_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelLocalSubnetMask4.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalSubnetMask4.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalGateway1_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelLocalGateway1.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalGateway1.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalGateway2_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelLocalGateway2.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalGateway2.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalGateway3_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelLocalGateway3.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalGateway3.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void labelLocalGateway4_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelLocalGateway4.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelLocalGateway4.Text = myKeyPad.StringValue;
                    this.buttonTcpSave1.Enabled = true;
                }
            }
        }
        private void buttonTcpSave1_Click(object sender, EventArgs e)
        {
            string deviceIP = "", subNetMask = "", gateWay = "";
            string beforeIP = "", beforeSub = "", beforeGate = "", afterIP = "", afterSub = "", afterGate = "";

            beforeIP = this.ParentForm.smartConfigs.IPSettings.DeviceIP;
            beforeSub = this.ParentForm.smartConfigs.IPSettings.SubNetMask;
            beforeGate = this.ParentForm.smartConfigs.IPSettings.GateWay;

            this.buttonTcpSave1.Enabled = false;

            if (this.ParentForm.SystemConfig.EthernetOperationMode == 1)
                this.ParentForm.EthernetServerStop();
            else if (this.ParentForm.SystemConfig.EthernetOperationMode == 2)
                this.ParentForm.EthernetClientDisconnect();

            deviceIP = string.Format("{0}.{1}.{2}.{3}",
                this.labelLocalIP1.Text, this.labelLocalIP2.Text, this.labelLocalIP3.Text, this.labelLocalIP4.Text);
            subNetMask = string.Format("{0}.{1}.{2}.{3}",
                this.labelLocalSubnetMask1.Text, this.labelLocalSubnetMask2.Text, this.labelLocalSubnetMask3.Text, this.labelLocalSubnetMask4.Text);
            gateWay = string.Format("{0}.{1}.{2}.{3}",
                this.labelLocalGateway1.Text, this.labelLocalGateway2.Text, this.labelLocalGateway3.Text, this.labelLocalGateway4.Text);

            this.ParentForm.smartConfigs.IPSettings.DeviceIP = deviceIP;
            this.ParentForm.smartConfigs.IPSettings.SubNetMask = subNetMask;
            this.ParentForm.smartConfigs.IPSettings.GateWay = gateWay;

            this.ParentForm.smartConfigs.IPSettings.Save();
            this.ParentForm.smartConfigs.IPSettings.SetApply();

            if (this.ParentForm.SystemConfig.EthernetOperationMode == 1)
                this.ParentForm.EthernetServerStart();
            else if (this.ParentForm.SystemConfig.EthernetOperationMode == 2)
                this.ParentForm.EthernetClientConnect();

            afterIP = this.ParentForm.smartConfigs.IPSettings.DeviceIP;
            afterSub = this.ParentForm.smartConfigs.IPSettings.SubNetMask;
            afterGate = this.ParentForm.smartConfigs.IPSettings.GateWay;

            // Part 11
            if (this.ParentForm.SystemConfig.IsPart11 == true)
            {
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.TCPLocalIPAddress, "", beforeIP, afterIP);
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.TCPLocalSubnetMask, "", beforeSub, afterSub);
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.TCPLocalGateway, "", beforeGate, afterGate);
            }
        }

        private void labelLocalPort_Click(object sender, EventArgs e)
        {
            string before = "", after = "";

            before = this.labelLocalPort.Text;

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelLocalPort.Text, 4, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                this.labelLocalPort.Text = myKeyPad.StringValue;
                after = myKeyPad.StringValue;

                this.ParentForm.EthernetServerStop();
                this.ParentForm.smartTCPMultiServer.Port = myKeyPad.IntValue;
                this.ParentForm.SystemConfig.EthernetLocalPort = myKeyPad.IntValue;
                this.ParentForm.SaveSystemConfigurationFile3(this.ParentForm.SystemConfig);

                // Part 11
                if (this.ParentForm.SystemConfig.IsPart11 == true)
                    this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.TCPLocalPort, "", before, after);
            }
        }
        private void labelHostIP1_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelHostIP1.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelHostIP1.Text = myKeyPad.StringValue;
                    this.buttonTcpSave2.Enabled = true;
                }
            }
        }
        private void labelHostIP2_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelHostIP2.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelHostIP2.Text = myKeyPad.StringValue;
                    this.buttonTcpSave2.Enabled = true;
                }
            }
        }
        private void labelHostIP3_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelHostIP3.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelHostIP3.Text = myKeyPad.StringValue;
                    this.buttonTcpSave2.Enabled = true;
                }
            }
        }
        private void labelHostIP4_Click(object sender, EventArgs e)
        {
            string temp = "";

            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelHostIP4.Text, 3, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                if (myKeyPad.IntValue > 255)
                {
                    temp = "0 ~ 255";
                    DialogFormMessage myMsg = new DialogFormMessage(1, this.ParentForm.SystemConfig.Language, temp);
                    myMsg.ShowDialog();
                }
                else
                {
                    this.labelHostIP4.Text = myKeyPad.StringValue;
                    this.buttonTcpSave2.Enabled = true;
                }
            }
        }
        private void labelHostPort_Click(object sender, EventArgs e)
        {
            DialogFormNumKeyPad myKeyPad = new DialogFormNumKeyPad(this.labelHostPort.Text, 4, 0, false, this.ParentForm.SystemConfig.Language);

            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                this.labelHostPort.Text = myKeyPad.StringValue;
                this.buttonTcpSave2.Enabled = true;
            }
        }
        private void buttonTcpSave2_Click(object sender, EventArgs e)
        {
            string beforeIP = "", beforePort = "", afterIP = "", afterPort = "";

            beforeIP = this.ParentForm.SystemConfig.EthernetServerAddress;
            beforePort = this.ParentForm.SystemConfig.EthernetServerPort.ToString();

            this.ParentForm.SystemConfig.EthernetServerPort = int.Parse(this.labelHostPort.Text);

            this.ParentForm.SystemConfig.EthernetServerAddress = string.Format("{0}.{1}.{2}.{3}",
                this.labelHostIP1.Text, this.labelHostIP2.Text, this.labelHostIP3.Text, this.labelHostIP4.Text);

            this.ParentForm.SaveSystemConfigurationFile3(this.ParentForm.SystemConfig);
            this.ParentForm.EthernetClientDisconnect();
            this.buttonTcpSave2.Enabled = false;

            afterIP = this.ParentForm.SystemConfig.EthernetServerAddress;
            afterPort = this.ParentForm.SystemConfig.EthernetServerPort.ToString();

            // Part11
            if (this.ParentForm.SystemConfig.IsPart11 == true)
            {
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.TCPServerIPAddress, "", beforeIP, afterIP);
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.TCPServerPort, "", beforePort, afterPort);
            }
        }

        private void buttonDataSend_Click(object sender, EventArgs e)
        {
            this.ParentForm.ModbusCommonDataSend();
        }
        private void button1LaneSend_Click(object sender, EventArgs e)
        {
            this.ParentForm.ModbusLaneTestDataSend(1);
        }
        private void button10LaneSend_Click(object sender, EventArgs e)
        {
            this.ParentForm.ModbusLaneTestDataSend(10);
        }

        private void labelMACAddress_Click(object sender, EventArgs e)
        {
            SmartX.SmartLabel label = sender as SmartX.SmartLabel;

            DialogFormHexKeyPad myKeyPad = new DialogFormHexKeyPad(label.Text, 2, this.ParentForm.SystemConfig.Language);
            if (myKeyPad.ShowDialog() == DialogResult.OK)
            {
                label.Text = myKeyPad.StringValue;
                this.buttonSave4.Enabled = true;
            }
        }
        private void buttonSave4_Click(object sender, EventArgs e)
        {
            byte[] changeMAC = new byte[6];

            changeMAC[0] = Convert.ToByte(this.HexArray[0], 16);
            changeMAC[1] = Convert.ToByte(this.HexArray[1], 16);
            changeMAC[2] = Convert.ToByte(this.HexArray[2], 16);
            changeMAC[3] = Convert.ToByte(this.labelMACAddress4.Text, 16);
            changeMAC[4] = Convert.ToByte(this.labelMACAddress5.Text, 16);
            changeMAC[5] = Convert.ToByte(this.labelMACAddress6.Text, 16);

            this.ParentForm.smartConfigs.IPSettings.SetMACAddress(changeMAC);

            this.buttonSave4.Enabled = false;
        }
        #endregion

    }
}