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

using SmartX;
using INT69DB_2A_ImageDll;
using INT69DB_2A.Controls;
using INT69DB_2A.DialogForms;

namespace INT69DB_2A.Forms
{
    public partial class FormCommunication : Form
    {
        #region Field
        private FormMain m_ParentForm;

        private delegate void ConnetedIPStatusCallback(string value);
        #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; }
            private set { this.m_ParentForm = value; }
        } 
        #endregion

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

            if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Korean)
            {

            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.English)
            {
                this.labelTitle.Text = "Communication";

                this.labelStaticTcpOperationMode.Text = "Operation";
                this.labelStaticTcpMode.Text = "Mode";

                this.labelStaticBaudrate.Text = "Baudrate";
                this.smartLabel4.Text = "Mode";

                this.labelHelp.Text = "※ When IP configuration is changed, it is normally connected after rebooting.";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Chinese)
            {
                this.labelTitle.Text = "选项";

                this.labelStaticTcpOperationMode.Text = "运转模式";
                this.labelStaticTcpMode.Text = "通信模式";

                this.smartLabel4.Text = "模式";

                this.labelProtocolDescriptionCOM3_1.Text = "COM3协议说明";
                this.labelProtocolDescriptionCOM3_2.Text = "COM3协议说明";

                this.buttonSave.Text = "保存";
                this.buttonSerialSend.Text = "发送";

                this.labelHelp.Text = "※ 更改IP配置后，通常在重新启动后即可连接。";

                this.comboBoxSerial3Mode.Items.Clear();
                this.comboBoxSerial3Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);
                this.comboBoxSerial3Mode.Items.Add("-");
                this.comboBoxSerial3Mode.Items.Add("OPT1");
                this.comboBoxSerial3Mode.Items.Add("OPT2");
                this.comboBoxSerial3Mode.Items.Add("OPT3");
                this.comboBoxSerial3Mode.Items.Add("工业打印机");
                this.comboBoxSerial3Mode.Items.Add("OPT4");
                this.comboBoxSerial3Mode.SelectedIndex = 0;
                this.comboBoxSerial3Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Japanese)
            {
                this.labelTitle.Text = "通信";

                this.labelStaticTcpOperationMode.Text = "動作";
                this.labelStaticTcpMode.Text = "モㅡド";

                this.smartLabel4.Text = "モㅡド";

                this.buttonTcpSave1.Text = "保存";
                this.buttonTcpSave2.Text = "保存";
                this.buttonSave.Text = "保存";
                this.buttonSerialSend.Text = "送信";

                this.labelHelp.Text = "※ IP Configurationの変更時には、再起動後に正常に接続されます。";
            }
            else if (this.ParentForm.SystemConfig.Language == DataStore.LanguageID.Czech)
            {

            }
            else
            {

            }
        }
        private void InitializeControl()
        {
            this.comboBoxOperationMode.Items.Clear();
            this.comboBoxOperationMode.SelectedIndexChanged -= new EventHandler(this.comboBoxOperationMode_SelectedIndexChanged);
            this.comboBoxOperationMode.Items.Add("-");
            this.comboBoxOperationMode.Items.Add("Server");
            this.comboBoxOperationMode.Items.Add("Client");
            this.comboBoxOperationMode.SelectedIndex = 0;
            this.comboBoxOperationMode.SelectedIndexChanged += new EventHandler(this.comboBoxOperationMode_SelectedIndexChanged);

            this.comboBoxMode.Items.Clear();
            this.comboBoxMode.SelectedIndexChanged -= new EventHandler(this.comboBoxMode_SelectedIndexChanged);
            this.comboBoxMode.Items.Add("-");
            this.comboBoxMode.Items.Add("OPT1");
            this.comboBoxMode.Items.Add("OPT2");
            this.comboBoxMode.Items.Add("OPT3");
            this.comboBoxMode.Items.Add("Modbus");
            this.comboBoxMode.Items.Add("OPT4");
            this.comboBoxMode.Items.Add("OPT5");
            this.comboBoxMode.SelectedIndex = 0;
            this.comboBoxMode.SelectedIndexChanged += new EventHandler(this.comboBoxMode_SelectedIndexChanged);

            this.comboBoxSerial3BaudRate.Items.Clear();
            this.comboBoxSerial3BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3BaudRate_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.comboBoxSerial3BaudRate_SelectedIndexChanged);

            this.comboBoxSerial3Mode.Items.Clear();
            this.comboBoxSerial3Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);
            this.comboBoxSerial3Mode.Items.Add("-");
            this.comboBoxSerial3Mode.Items.Add("OPT1");
            this.comboBoxSerial3Mode.Items.Add("OPT2");
            this.comboBoxSerial3Mode.Items.Add("OPT3");
            this.comboBoxSerial3Mode.Items.Add("Printer");
            this.comboBoxSerial3Mode.Items.Add("OPT4");
            this.comboBoxSerial3Mode.Items.Add("OPT5");
            this.comboBoxSerial3Mode.SelectedIndex = 0;
            this.comboBoxSerial3Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);
        }
        private void DefaultSetting()
        {
            string[] sValue;

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

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

            // Host IP
            if (this.ParentForm.SystemConfig.EthernetServerAddress == "")
            {
                this.ParentForm.SystemConfig.EthernetServerAddress = "0.0.0.0";
                this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);
            }
            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

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

            this.comboBoxSerial3Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);
            this.comboBoxSerial3Mode.SelectedIndex = this.ParentForm.SystemConfig.Serial3Mode;
            if (this.comboBoxSerial3Mode.SelectedIndex == 5)
                this.comboBoxSerial3BaudRate.Enabled = false;
            this.comboBoxSerial3Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);

            if (this.ParentForm.SystemConfig.Serial3Mode != 0)
                this.buttonSerialSend.Enabled = true;
            else
                this.buttonSerialSend.Enabled = false;
            #endregion
        }
        
        private void EthernetStart()
        {
            string detail = "";

            this.buttonStart.Enabled = true;
            this.buttonStop.Enabled = true;
            this.buttonTestSend.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;

                    this.ParentForm.ChildFormMainDisplay.TimerServer(true);
                }
            }
            else if (this.ParentForm.SystemConfig.EthernetOperationMode == 2)
            {
                detail = "Client";
                this.ParentForm.EthernetClientConnect();
            }

            if (this.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.SetTrackingHistoryData(DataStore.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();
            }

            if (this.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.TryEthernetStop, detail);
        }
        private void EthernetDataTestSend()
        {
            string data = "";

            if (this.ParentForm.SystemConfig.EthernetMode != (int)DataStore.EthernetMode.f4_Modbus)
            {
                if (this.ParentForm.SystemConfig.EthernetMode == (int)DataStore.EthernetMode.f1_OPT1)
                {
                    if (this.ParentForm.SystemConfig.EquipmentMode == 1)
                        data = this.ParentForm.Protocol_OPT1(this.ParentForm.CollectionWeightData);
                    else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                        data = this.ParentForm.Protocol_OPT1(1, this.ParentForm.CollectionWeightData[0]);
                }
                else if (this.ParentForm.SystemConfig.EthernetMode == (int)DataStore.EthernetMode.f2_OPT2)
                {
                    if (this.ParentForm.SystemConfig.EquipmentMode == 1)
                        data = this.ParentForm.Protocol_OPT2(this.ParentForm.CollectionWeightData);
                    else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                        data = this.ParentForm.Protocol_OPT2(1, this.ParentForm.CollectionWeightData[0]);
                }
                else if (this.ParentForm.SystemConfig.EthernetMode == (int)DataStore.EthernetMode.f3_OPT3)
                {
                    if (this.ParentForm.SystemConfig.EquipmentMode == 1)
                        data = this.ParentForm.Protocol_OPT3(this.ParentForm.CollectionWeightData);
                    else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                        data = this.ParentForm.Protocol_OPT3(1, this.ParentForm.CollectionWeightData[0]);
                }
                else if (this.ParentForm.SystemConfig.EthernetMode == (int)DataStore.EthernetMode.f5_OPT4)
                {
                    if (this.ParentForm.SystemConfig.EquipmentMode == 1)
                        data = this.ParentForm.Protocol_OPT4(this.ParentForm.CollectionWeightData);
                    else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                        data = this.ParentForm.Protocol_OPT4(1, this.ParentForm.CollectionWeightData[0]);
                }

                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;
                }
            }
            else
            {
                this.ParentForm.ModbusCommonDataSend();
            }
        }
        private void EthernetLaneTestSend(int lane)
        {
            string data = "";

            if (this.ParentForm.SystemConfig.EthernetMode != (int)DataStore.EthernetMode.f4_Modbus)
            {
                if (this.ParentForm.SystemConfig.EthernetMode == (int)DataStore.EthernetMode.f1_OPT1)
                {
                    if (this.ParentForm.SystemConfig.EquipmentMode == 1)
                        data = this.ParentForm.Protocol_OPT1(this.ParentForm.CollectionWeightData);
                    else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                        data = this.ParentForm.Protocol_OPT1(1, this.ParentForm.CollectionWeightData[0]);
                }
                else if (this.ParentForm.SystemConfig.EthernetMode == (int)DataStore.EthernetMode.f2_OPT2)
                {
                    if (this.ParentForm.SystemConfig.EquipmentMode == 1)
                        data = this.ParentForm.Protocol_OPT2(this.ParentForm.CollectionWeightData);
                    else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                        data = this.ParentForm.Protocol_OPT2(1, this.ParentForm.CollectionWeightData[0]);
                }
                else if (this.ParentForm.SystemConfig.EthernetMode == (int)DataStore.EthernetMode.f3_OPT3)
                {
                    if (this.ParentForm.SystemConfig.EquipmentMode == 1)
                        data = this.ParentForm.Protocol_OPT3(this.ParentForm.CollectionWeightData);
                    else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                        data = this.ParentForm.Protocol_OPT3(1, this.ParentForm.CollectionWeightData[0]);
                }
                else if (this.ParentForm.SystemConfig.EthernetMode == (int)DataStore.EthernetMode.f5_OPT4)
                {
                    if (this.ParentForm.SystemConfig.EquipmentMode == 1)
                        data = this.ParentForm.Protocol_OPT4(this.ParentForm.CollectionWeightData);
                    else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                        data = this.ParentForm.Protocol_OPT4(1, this.ParentForm.CollectionWeightData[0]);
                }

                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;
                }
            }
            else
            {
                this.ParentForm.ModbusLaneTestDataSend(lane);
            }
        }

        private void UpdateDisplayCom3()
        {
            this.comboBoxSerial3BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3BaudRate_SelectedIndexChanged);
            try
            {
                this.comboBoxSerial3BaudRate.SelectedIndex = this.ParentForm.SystemConfig.Serial3BaudRate;
            }
            catch
            {
                this.comboBoxSerial3BaudRate.SelectedIndex = 0;
            }
            this.comboBoxSerial3BaudRate.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3BaudRate_SelectedIndexChanged);

            this.comboBoxSerial3Mode.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);
            this.comboBoxSerial3Mode.SelectedIndex = this.ParentForm.SystemConfig.Serial3Mode;
            if (this.comboBoxSerial3Mode.SelectedIndex == (int)DataStore.SerialMode.f4_Printer)
                this.comboBoxSerial3BaudRate.Enabled = false;
            this.comboBoxSerial3Mode.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3Mode_SelectedIndexChanged);

            this.buttonSave.Enabled = false;

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

            this.UpdateProtocolDescriptionCOM3();
        }

        private void UpdateProtocolDescriptionCOM3()
        {
            if (this.comboBoxSerial3Mode.SelectedIndex == 1)
            {
                if (this.ParentForm.SystemConfig.EquipmentMode == 1 || this.ParentForm.SystemConfig.EquipmentMode == 3)
                {
                    this.labelProtocolDescriptionCOM3_1.Text = "STX[1],ID[5],NO[4],";
                    this.labelProtocolDescriptionCOM3_2.Text = "{GRD[1],WEIGHT[6]} * n,ETX[1]";
                }
                else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                {
                    this.labelProtocolDescriptionCOM3_1.Text = "STX[1],ID[5],NO[4],LANE[2],GRD[1],";
                    this.labelProtocolDescriptionCOM3_2.Text = "WEIGHT[6],ETX[1]";
                }
            }
            else if (this.comboBoxSerial3Mode.SelectedIndex == 2)
            {
                if (this.ParentForm.SystemConfig.EquipmentMode == 1 || this.ParentForm.SystemConfig.EquipmentMode == 3)
                {
                    this.labelProtocolDescriptionCOM3_1.Text = "STX[1],ID[5],NO[4],";
                    this.labelProtocolDescriptionCOM3_2.Text = "{SIGN[1],GAP WEIGHT[6]} * n,ETX[1]";
                }
                else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                {
                    this.labelProtocolDescriptionCOM3_1.Text = "STX[1],ID[5],NO[4],LANE[2],SIGN[1],";
                    this.labelProtocolDescriptionCOM3_2.Text = "GAP WEIGHT[6],ETX[1]";
                }
            }
            else if (this.comboBoxSerial3Mode.SelectedIndex == 3)
            {
                if (this.ParentForm.SystemConfig.EquipmentMode == 1 || this.ParentForm.SystemConfig.EquipmentMode == 3)
                {
                    this.labelProtocolDescriptionCOM3_1.Text = "STX[1],ID[5],NO[4],PassRange[6],";
                    this.labelProtocolDescriptionCOM3_2.Text = "{GRD[1],WEIGHT[6]} * n,ETX[1]";
                }
                else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                {
                    this.labelProtocolDescriptionCOM3_1.Text = "STX[1],ID[5],NO[4],PassRange[6],LANE[2],";
                    this.labelProtocolDescriptionCOM3_2.Text = "GRD[1],WEIGHT[6],ETX[1]]";
                }
            }
            else if (this.comboBoxSerial3Mode.SelectedIndex == 4)
            {
                this.labelProtocolDescriptionCOM3_1.Text = "NO[4], LANE[2], GRADE[1], WEIGHT[6],";
                this.labelProtocolDescriptionCOM3_2.Text = "COUNT[9]";
            }
            else if (this.comboBoxSerial3Mode.SelectedIndex == 5)
            {
                if (this.ParentForm.SystemConfig.EquipmentMode == 1 || this.ParentForm.SystemConfig.EquipmentMode == 3)
                {
                    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,ETX[1]";
                }
                else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                {
                    this.labelProtocolDescriptionCOM3_1.Text = "STX[1],ID[5],NO[4],Under[6],Pass[6],Over[6],";
                    this.labelProtocolDescriptionCOM3_2.Text = "LANE[2],GRD[1],WEIGHT[6],ETX[1]]";
                }
            }
            else if (this.comboBoxSerial3Mode.SelectedIndex == 6)
            {
                if (this.ParentForm.SystemConfig.EquipmentMode == 1 || this.ParentForm.SystemConfig.EquipmentMode == 3)
                {
                    this.labelProtocolDescriptionCOM3_1.Text = "STX[1],ID[5],NO[4],Pass Count[7],NG Count[7],";
                    this.labelProtocolDescriptionCOM3_2.Text = "Total Count[9],{GRD[1],WEIGHT[6]} * n,ETX[1]";
                }
                else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                {
                    this.labelProtocolDescriptionCOM3_1.Text = "STX[1],ID[5],NO[4],Pass Count[7],NG Count[7],";
                    this.labelProtocolDescriptionCOM3_2.Text = "Total Count[9],LANE[2],GRD[1],WEIGHT[6],ETX[1]]";
                }
            }
            else
            {
                this.labelProtocolDescriptionCOM3_1.Text = "-";
                this.labelProtocolDescriptionCOM3_2.Text = "-";
            }
        }
        private void UpdateProtocolDescriptionEthernet()
        {
            if (this.comboBoxMode.SelectedIndex == (int)DataStore.EthernetMode.f1_OPT1)
            {
                if (this.ParentForm.SystemConfig.EquipmentMode == 1 || this.ParentForm.SystemConfig.EquipmentMode == 3)
                {
                    this.labelProtocolDescriptionEthernet_1.Text = "STX[1],ID[5],NO[4],";
                    this.labelProtocolDescriptionEthernet_2.Text = "{GRD[1],WEIGHT[6]} * n,ETX[1]";
                }
                else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                {
                    this.labelProtocolDescriptionEthernet_1.Text = "STX[1],ID[5],NO[4],LANE[2],GRD[1],";
                    this.labelProtocolDescriptionEthernet_2.Text = "WEIGHT[6],ETX[1]";
                }
            }
            else if (this.comboBoxMode.SelectedIndex == (int)DataStore.EthernetMode.f2_OPT2)
            {
                if (this.ParentForm.SystemConfig.EquipmentMode == 1 || this.ParentForm.SystemConfig.EquipmentMode == 3)
                {
                    this.labelProtocolDescriptionEthernet_1.Text = "STX[1],ID[5],NO[4],";
                    this.labelProtocolDescriptionEthernet_2.Text = "{SIGN[1],GAP WEIGHT[6]} * n,ETX[1]";
                }
                else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                {
                    this.labelProtocolDescriptionEthernet_1.Text = "STX[1],ID[5],NO[4],LANE[2],SIGN[1],";
                    this.labelProtocolDescriptionEthernet_2.Text = "GAP WEIGHT[6],ETX[1]";
                }
            }
            else if (this.comboBoxMode.SelectedIndex == (int)DataStore.EthernetMode.f3_OPT3)
            {
                if (this.ParentForm.SystemConfig.EquipmentMode == 1 || this.ParentForm.SystemConfig.EquipmentMode == 3)
                {
                    this.labelProtocolDescriptionEthernet_1.Text = "STX[1],ID[5],NO[4],PassRange[6],";
                    this.labelProtocolDescriptionEthernet_2.Text = "{GRD[1],WEIGHT[6]} * n,ETX[1]";
                }
                else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                {
                    this.labelProtocolDescriptionEthernet_1.Text = "STX[1],ID[5],NO[4],PassRange[6],LANE[2],";
                    this.labelProtocolDescriptionEthernet_2.Text = "GRD[1],WEIGHT[6],ETX[1]]";
                }
            }
            else if (this.comboBoxMode.SelectedIndex == (int)DataStore.EthernetMode.f4_Modbus)
            {
                this.labelProtocolDescriptionEthernet_1.Text = "-";
                this.labelProtocolDescriptionEthernet_2.Text = "-";
            }
            else if (this.comboBoxMode.SelectedIndex == (int)DataStore.EthernetMode.f5_OPT4)
            {
                if (this.ParentForm.SystemConfig.EquipmentMode == 1 || this.ParentForm.SystemConfig.EquipmentMode == 3)
                {
                    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,ETX[1]";
                }
                else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                {
                    this.labelProtocolDescriptionEthernet_1.Text = "STX[1],ID[5],NO[4],Under[6],Pass[6],Over[6],";
                    this.labelProtocolDescriptionEthernet_2.Text = "LANE[2],GRD[1],WEIGHT[6],ETX[1]]";
                }
            }
            else
            {
                this.labelProtocolDescriptionEthernet_1.Text = "-";
                this.labelProtocolDescriptionEthernet_2.Text = "-";
            }
        }

        private void ConnectedIPStatusOut(string value)
        {
            this.labelConnectedIP.Text = value;
        }
        public void DelegateConnectedIPStatusOut(string value)
        {
            this.Invoke(new ConnetedIPStatusCallback(this.ConnectedIPStatusOut), value);
        }

        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];
            }
        }
        private void UpdateLocalIPConfigurationDisplay()
        {
            this.radioButtonLocalIpDHCP.Click -= new EventHandler(this.radioButtonLocalIpConfig_Click);
            // Local IP DHCP
            if (this.ParentForm.smartConfigs.IPSettings.DHCPEnable == 1)
            {
                this.radioButtonLocalIpDHCP.Checked = true;
                this.LocalIPEnable(false);
            }
            else
            {
                this.radioButtonLocalIpStatic.Checked = true;
                this.LocalIPEnable(true);
            }
            this.radioButtonLocalIpDHCP.Click += new EventHandler(this.radioButtonLocalIpConfig_Click);
        }
        private void UpdateDisplaySettingData(int index)
        {
            switch (index)
            {
                case (int)DataStore.EthernetMode.f4_Modbus: // Modbus
                    this.labelTitleStartAddress.Visible = true;
                    this.labelStartAddress.Visible = true;
                    this.labelStartAddress.Text = this.ParentForm.SystemConfig.ModbusTcpStartAddress.ToString();
                    break;
                default:
                    this.labelTitleStartAddress.Visible = false;
                    this.labelStartAddress.Visible = false;
                    break;
            }
        }        
        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;
        }
        private string ReturnCommunicationModeName(ComboBox comboBox, int num)
        {
            switch (num)
            {
                case 0:
                    return "None";
                case 1:
                    return "OPT1";
                case 2:
                    return "OPT2";
                case 3:
                    return "OPT3";
                case 4:
                    if(comboBox == this.comboBoxMode)
                        return "Modbus";
                    else
                        return "Printer";
                case 5:
                    return "OPT4";
                default:
                    return "None";
            }
        }

        public void DisplayRefresh()
        {
            this.ParentForm.SystemConfig.CurrentForm = DataStore.FormStore.FormCommunication;

            #region TCP
            // Local IP DHCP
            this.UpdateLocalIPConfigurationDisplay();
            // Local IP
            this.UpdateLocalIPAddressDisplay();
            // Setting Data visible
            this.UpdateDisplaySettingData(this.ParentForm.SystemConfig.EthernetMode);

            this.comboBoxMode.SelectedIndexChanged -= new EventHandler(this.comboBoxMode_SelectedIndexChanged);
            this.comboBoxOperationMode.SelectedIndexChanged -= new EventHandler(this.comboBoxOperationMode_SelectedIndexChanged);

            this.comboBoxMode.SelectedIndex = this.ParentForm.SystemConfig.EthernetMode;
            this.comboBoxOperationMode.SelectedIndex = this.ParentForm.SystemConfig.EthernetOperationMode;

            this.comboBoxMode.SelectedIndexChanged += new EventHandler(this.comboBoxMode_SelectedIndexChanged);
            this.comboBoxOperationMode.SelectedIndexChanged += new EventHandler(this.comboBoxOperationMode_SelectedIndexChanged);

            // Format
            this.UpdateProtocolDescriptionEthernet();

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

            if (this.comboBoxMode.SelectedIndex == (int)DataStore.EthernetMode.f4_Modbus) // Modbus
            {
                this.button1LaneSend.Visible = true;
                this.button10LaneSend.Visible = true;
            }
            else
            {
                this.button1LaneSend.Visible = false;
                this.button10LaneSend.Visible = false;
            }

            if (this.comboBoxOperationMode.SelectedIndex == 1)
            {
                this.buttonStart.Enabled = true;
                this.buttonStop.Enabled = true;
                this.buttonTestSend.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.comboBoxOperationMode.SelectedIndex == 2)
            {
                this.buttonStart.Enabled = true;
                this.buttonStop.Enabled = true;
                this.buttonTestSend.Enabled = true;

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

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

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

            this.UpdateDisplayCom3();
        }
        #endregion

        #region Event Handler

        #region Ethernet
        private void buttonStart_Click(object sender, EventArgs e)
        {
            this.EthernetStart();
        }
        private void buttonStop_Click(object sender, EventArgs e)
        {
            this.EthernetStop();
        }
        private void buttonSend_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";
                if (this.ParentForm.SystemConfig.IsPart11 == true)
                    this.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.EthernetConnect, "Server");

                this.labelConnectedIP.Text = "-";

                if (this.ParentForm.smartTCPMultiServer.ClientInfoList != null)
                    this.labelConnectedIP.Text = this.ParentForm.smartTCPMultiServer.ClientInfoList[0].strIPAddress;
            }
            else
            {
                if (this.labelServerStatus.Text != ">Stop")
                    this.labelServerStatus.Text = ">Stop";
                if (this.ParentForm.SystemConfig.IsPart11 == true)
                    this.ParentForm.SetTrackingHistoryData(DataStore.TrackingOperation.EthernetDisconnect, "Server");
            }
        }

        private void comboBoxOperationMode_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.comboBoxOperationMode.SelectedIndex == 1)
            {
                #region Server
                this.ParentForm.EthernetClientDisconnect();

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

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

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

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

            this.ParentForm.SystemConfig.EthernetOperationMode = this.comboBoxOperationMode.SelectedIndex;

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

            this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);

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

            before = this.ReturnCommunicationModeName(comboBox, this.ParentForm.SystemConfig.EthernetMode);
            if (this.comboBoxMode.SelectedIndex == (int)DataStore.EthernetMode.f4_Modbus) // Modbus 사용 시
            {
                this.button1LaneSend.Visible = true;
                this.button10LaneSend.Visible = true;
            }
            else
            {
                this.button1LaneSend.Visible = false;
                this.button10LaneSend.Visible = false;
            }

            this.ParentForm.SystemConfig.EthernetMode = this.comboBoxMode.SelectedIndex;
            this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);
            after = this.comboBoxMode.SelectedItem.ToString();

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

            this.UpdateDisplaySettingData(this.ParentForm.SystemConfig.EthernetMode);
            this.UpdateProtocolDescriptionEthernet();
        }

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

            before = this.ParentForm.SystemConfig.ModbusTcpStartAddress.ToString();
            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
                {
                    after = this.labelStartAddress.Text = myKeyPad.StringValue;
                    this.ParentForm.SystemConfig.ModbusTcpStartAddress = myKeyPad.IntValue;

                    this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);

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

            if (this.ParentForm.smartConfigs.IPSettings.DHCPEnable == 1)
                before = "DHCP";
            else
                before = "Static";
            if (this.radioButtonLocalIpStatic.Checked == true)
                myDlg = new DialogFormYesNo(this.ParentForm.SystemConfig.Language, 15); // 로컬IP주소 수동으로 설정하시겠습니까?
            else
                myDlg = new DialogFormYesNo(this.ParentForm.SystemConfig.Language, 16); // 로컬IP주소 자동으로 설정하시겠습니까?

            if (myDlg.ShowDialog() == DialogResult.Yes)
            {
                if (this.radioButtonLocalIpDHCP.Checked == true)
                {
                    this.ParentForm.smartConfigs.IPSettings.DHCPEnable = 1;
                    after = "DHCP";

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

                    this.LocalIPEnable(false);
                    this.UpdateLocalIPAddressDisplay();
                }
                else
                {
                    this.ParentForm.smartConfigs.IPSettings.DHCPEnable = 0;
                    after = "Static";

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

                    this.LocalIPEnable(true);

                    try
                    {
                        this.UpdateLocalIPAddressDisplay();
                    }
                    catch
                    {
                        this.ParentForm.smartConfigs.IPSettings.DeviceIP = "192.168.3.10";
                        this.ParentForm.smartConfigs.IPSettings.SubNetMask = "255.255.255.0";
                        this.ParentForm.smartConfigs.IPSettings.GateWay = "192.168.3.1";

                        this.UpdateLocalIPAddressDisplay();
                    }
                }

                if (this.ParentForm.SystemConfig.IsPart11 == true)
                    this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.TCPLocalIPConfig, "", before, after);

                DialogFormMessage msg = new DialogFormMessage(18, this.ParentForm.SystemConfig.Language);
                msg.ShowDialog();
            }
            else
            {
                this.DisplayRefresh();
            }
        }
        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 before = "", after = "";

            this.buttonTcpSave1.Enabled = false;

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

            before = this.ParentForm.smartConfigs.IPSettings.DeviceIP;
            this.ParentForm.smartConfigs.IPSettings.DeviceIP = string.Format("{0}.{1}.{2}.{3}",
                this.labelLocalIP1.Text, this.labelLocalIP2.Text, this.labelLocalIP3.Text, this.labelLocalIP4.Text);
            after = this.ParentForm.smartConfigs.IPSettings.DeviceIP;
            if (this.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.TCPLocalIPAddress, "", before, after);

            before = this.ParentForm.smartConfigs.IPSettings.SubNetMask;
            this.ParentForm.smartConfigs.IPSettings.SubNetMask = string.Format("{0}.{1}.{2}.{3}",
                this.labelLocalSubnetMask1.Text, this.labelLocalSubnetMask2.Text, this.labelLocalSubnetMask3.Text, this.labelLocalSubnetMask4.Text);
            after = this.ParentForm.smartConfigs.IPSettings.SubNetMask;
            if (this.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.TCPLocalSubnetMask, "", before, after);

            before = this.ParentForm.smartConfigs.IPSettings.GateWay;
            this.ParentForm.smartConfigs.IPSettings.GateWay = string.Format("{0}.{1}.{2}.{3}",
                this.labelLocalGateway1.Text, this.labelLocalGateway2.Text, this.labelLocalGateway3.Text, this.labelLocalGateway4.Text);
            after = this.ParentForm.smartConfigs.IPSettings.GateWay;
            if (this.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.TCPLocalGateway, "", before, after);

            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();
        }

        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)
            {
                after = this.labelLocalPort.Text = myKeyPad.StringValue;

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

                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 before = "", after = "";

            before = this.ParentForm.SystemConfig.EthernetServerPort.ToString();
            this.ParentForm.SystemConfig.EthernetServerPort = int.Parse(this.labelHostPort.Text);
            after = this.ParentForm.SystemConfig.EthernetServerPort.ToString();
            if (this.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.TCPServerPort, "", before, after);

            before = this.ParentForm.SystemConfig.EthernetServerAddress;
            this.ParentForm.SystemConfig.EthernetServerAddress = string.Format("{0}.{1}.{2}.{3}",
                this.labelHostIP1.Text, this.labelHostIP2.Text, this.labelHostIP3.Text, this.labelHostIP4.Text);
            after = this.ParentForm.SystemConfig.EthernetServerAddress;
            if (this.ParentForm.SystemConfig.IsPart11 == true)
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.TCPServerIPAddress, "", before, after);

            this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);
            this.ParentForm.EthernetClientDisconnect();
            this.buttonTcpSave2.Enabled = false;
        }
        
        private void buttonTestSend_Click(object sender, EventArgs e)
        {
            this.EthernetDataTestSend();
        }
        private void button1LaneSend_Click(object sender, EventArgs e)
        {
            this.EthernetLaneTestSend(1);
        }
        private void button10LaneSend_Click(object sender, EventArgs e)
        {
            this.EthernetLaneTestSend(10);
        }
        #endregion

        #region Serial
        private void buttonSave2_Click(object sender, EventArgs e)
        {
            string beforeMode = "", afterMode = "";
            string beforeBaudrate = "", afterBaudrate = "";
            ComboBox comboBox = sender as ComboBox;

            beforeMode = this.ReturnCommunicationModeName(comboBox, this.ParentForm.SystemConfig.Serial3Mode);
            beforeBaudrate = this.ParentForm.SystemConfig.Serial3BaudRate.ToString();

            this.ParentForm.SystemConfig.Serial3BaudRate = this.comboBoxSerial3BaudRate.SelectedIndex;
            this.ParentForm.SystemConfig.Serial3Mode = this.comboBoxSerial3Mode.SelectedIndex;

            afterMode = this.ReturnCommunicationModeName(comboBox, this.ParentForm.SystemConfig.Serial3Mode);
            afterBaudrate = this.ParentForm.SystemConfig.Serial3BaudRate.ToString();

            if (this.ParentForm.SystemConfig.IsPart11 == true)
            {
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.Com3Mode, "", beforeMode, afterMode);
                this.ParentForm.SetTrackingHistoryData(DataStore.TrackingParameter.Com3Baudrate, "", beforeBaudrate, afterBaudrate);
            }

            this.ParentForm.SaveSystemConfigurationFile(this.ParentForm.SystemConfig);

            if (this.ParentForm.smartSerialPortCom3.IsOpen == true)
                this.ParentForm.smartSerialPortCom3.Close();

            if (this.ParentForm.SystemConfig.Serial3Mode != 0)
            {
                switch (this.ParentForm.SystemConfig.Serial3Mode)
                {
                    case 4:
                        this.ParentForm.smartSerialPortCom3.FrameSeparationType = SmartSerialPort.FRAMESEPARATIONTYPES.NONEFRAME_READTIMEOUT;
                        break;
                    default:
                        this.ParentForm.smartSerialPortCom3.FrameSeparationType = SmartSerialPort.FRAMESEPARATIONTYPES.STXANDETX;
                        break;
                }

                switch (this.ParentForm.SystemConfig.Serial3BaudRate)
                {
                    case 0:
                        this.ParentForm.smartSerialPortCom3.Baud_Rate = SmartSerialPort.BAUDRATE._9600bps;
                        break;
                    case 1:
                        this.ParentForm.smartSerialPortCom3.Baud_Rate = SmartSerialPort.BAUDRATE._19200bps;
                        break;
                    case 2:
                        this.ParentForm.smartSerialPortCom3.Baud_Rate = SmartSerialPort.BAUDRATE._38400bps;
                        break;
                    case 3:
                        this.ParentForm.smartSerialPortCom3.Baud_Rate = SmartSerialPort.BAUDRATE._115200bps;
                        break;
                    default:
                        this.ParentForm.smartSerialPortCom3.Baud_Rate = SmartSerialPort.BAUDRATE._9600bps;
                        break;
                }
                this.ParentForm.smartSerialPortCom3.Open();

                this.buttonSerialSend.Enabled = true;
            }
            else
            {
                this.buttonSerialSend.Enabled = false;
            }
            
            this.buttonSave.Enabled = false;
        }
        private void buttonSerialSend_Click(object sender, EventArgs e)
        {
            if (this.ParentForm.SystemConfig.Serial3Mode == (int)DataStore.SerialMode.f1_OPT1)
            {
                if (this.ParentForm.SystemConfig.EquipmentMode == 1 || this.ParentForm.SystemConfig.EquipmentMode == 3)
                    this.ParentForm.UartCom3Write(this.ParentForm.Protocol_OPT1_NotStxEtx(this.ParentForm.CollectionWeightData));
                else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                    this.ParentForm.UartCom3Write(this.ParentForm.Protocol_OPT1_NotStxEtx(1, this.ParentForm.CollectionWeightData[0]));
            }
            else if (this.ParentForm.SystemConfig.Serial3Mode == (int)DataStore.SerialMode.f2_OPT2)
            {
                if (this.ParentForm.SystemConfig.EquipmentMode == 1 || this.ParentForm.SystemConfig.EquipmentMode == 3)
                    this.ParentForm.UartCom3Write(this.ParentForm.Protocol_OPT2_NotStxEtx(this.ParentForm.CollectionWeightData));
                else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                    this.ParentForm.UartCom3Write(this.ParentForm.Protocol_OPT2_NotStxEtx(1, this.ParentForm.CollectionWeightData[0]));
            }
            else if (this.ParentForm.SystemConfig.Serial3Mode == (int)DataStore.SerialMode.f3_OPT3)
            {
                if (this.ParentForm.SystemConfig.EquipmentMode == 1 || this.ParentForm.SystemConfig.EquipmentMode == 3)
                    this.ParentForm.UartCom3Write(this.ParentForm.Protocol_OPT3_NotStxEtx(this.ParentForm.CollectionWeightData));
                else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                    this.ParentForm.UartCom3Write(this.ParentForm.Protocol_OPT3_NotStxEtx(1, this.ParentForm.CollectionWeightData[0]));
            }
            else if (this.ParentForm.SystemConfig.Serial3Mode == (int)DataStore.SerialMode.f4_Printer)
            {
                if (this.ParentForm.SystemConfig.EquipmentMode == 1 || this.ParentForm.SystemConfig.EquipmentMode == 3)
                    this.ParentForm.UartCom3Write(this.ParentForm.Protocol_Printer(this.ParentForm.CollectionWeightData));
                else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                    this.ParentForm.UartCom3Write(this.ParentForm.Protocol_Printer(1, this.ParentForm.CollectionWeightData[0]));
            }
            else if (this.ParentForm.SystemConfig.Serial3Mode == (int)DataStore.SerialMode.f5_OPT4)
            {
                if (this.ParentForm.SystemConfig.EquipmentMode == 1 || this.ParentForm.SystemConfig.EquipmentMode == 3)
                    this.ParentForm.UartCom3Write(this.ParentForm.Protocol_OPT4_NotStxEtx(this.ParentForm.CollectionWeightData));
                else if (this.ParentForm.SystemConfig.EquipmentMode == 2)
                    this.ParentForm.UartCom3Write(this.ParentForm.Protocol_OPT4_NotStxEtx(1, this.ParentForm.CollectionWeightData[0]));
            }
        }

        private void comboBoxSerial3BaudRate_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.buttonSave.Enabled = true;
        }
        private void comboBoxSerial3Mode_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.UpdateProtocolDescriptionCOM3();

            this.comboBoxSerial3BaudRate.SelectedIndexChanged -= new EventHandler(this.comboBoxSerial3BaudRate_SelectedIndexChanged);
            if (this.comboBoxSerial3Mode.SelectedIndex == 4)
            {
                this.comboBoxSerial3BaudRate.SelectedIndex = 0;
                this.comboBoxSerial3BaudRate.Enabled = false;
            }
            else
                this.comboBoxSerial3BaudRate.Enabled = true;
            this.comboBoxSerial3BaudRate.SelectedIndexChanged += new EventHandler(this.comboBoxSerial3BaudRate_SelectedIndexChanged);

            this.buttonSave.Enabled = true;
        } 
        #endregion

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

        #endregion
    }
}