예제 #1
0
        private void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                if (th != null && th.IsAlive)
                {
                    th.Abort();
                    th = null;
                }
                if (modObj != null)
                {
                    if (modObj.IsSerialPortOpen())
                    {
                        modObj.CloseSerialPort();
                        modObj = null;
                    }
                }
            }
            catch (Exception ex)
            {
                this.Invoke((Action)(() =>
                {
                    MessageBoxEx.Show(ex.Message);
                }));
            }
            finally
            {
                th     = null;
                modObj = null;
            }


            this.Close();
        }
예제 #2
0
        private void Form11_Load(object sender, EventArgs e)
        {
            if (modbusRTUobj == null)
            {
                modbusRTUobj = new clsModbus();
                modbusRTUobj._GetLRCResultResult += new clsModbus.GetLRCResult(singlecmdtxt);
            }

            if (obj == null)
            {
                obj = new cls_1();
            }

            cmbNodeAddress.DataSource = address;
            cmbBaudRate.DataSource    = baudRates;
            cmbBitslength.DataSource  = dataLengths;
            cmbParity.DataSource      = parity;
            cmbStopBits.DataSource    = stopBits;
            cmbProtocol.DataSource    = protocol;

            obj.Comport       = SetValues.Set_PortName;
            obj.Baudrate      = SetValues.Set_Baudrate;
            obj.Parity        = SetValues.Set_parity;
            obj.Stopbits      = SetValues.Set_StopBits;
            obj.Bitslength    = Convert.ToString(SetValues.Set_BitsLength);
            obj.Protocol      = SetValues.Set_CommunicationProtocol;
            obj.ProtocalIndex = SetValues.Set_CommunicationProtocolindex;

            btnSet.Enabled    = false;
            btnSearch.Visible = true;
            btnCancel.Visible = false;

            lblStatus1.Text = "";
            lblFrame.Text   = "";
        }
예제 #3
0
        private void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                if (_thread != null)
                {
                    if (_thread.IsAlive)
                    {
                        _thread.Abort();
                    }
                    _thread = null;
                }

                btnStart.Enabled = true;
                btnSet.Enabled   = false;

                if (modbusRTUobj != null)
                {
                    if (modbusRTUobj.IsSerialPortOpen())
                    {
                        //modbusRTUobj.Port_Close();
                        modbusRTUobj.CloseSerialPort();
                    }

                    modbusRTUobj = null;
                }
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("6" + ex.Message);
            }
        }
예제 #4
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (modbusobj != null)
            {
                if (modbusobj.IsSerialPortOpen())
                {
                    modbusobj.CloseSerialPort();
                }
            }
            else
            {
                modbusobj = new clsModbus();
                modbusobj._GetLRCResultResult += new clsModbus.GetLRCResult(singlecmdtxt);
            }
            lblMessage.Text      = "";
            txtBxRecievecmd.Text = "";
            txtBxRecievecmd.Refresh();
            if (string.IsNullOrEmpty(txtBxSendCommnd.Text))
            {
                lblMessage.Text = "Please enter all fields.";
            }
            else
            {
                btnSend.Enabled = false;

                //Task<bool> task = new Task<bool>(SendFrameToDevice1);
                //task.Start();

                //bool status = await task;

                bool sent = SendFrameToDevice1();
            }
            btnSend.Enabled = true;
            StoreDefaultValues();
        }
예제 #5
0
        private void TcSettings_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                if (th != null)
                {
                    th.Abort();
                    th = null;
                }

                if (modObj != null)
                {
                    if (modObj.IsSerialPortOpen())
                    {
                        modObj.CloseSerialPort();
                        modObj = null;
                    }
                }

                this.Close();
            }
            catch (Exception ex)
            {
                this.Invoke((Action)(() =>
                {
                    MessageBoxEx.Show(ex.Message);
                }));
            }
            finally
            {
                th     = null;
                modObj = null;
            }
        }
예제 #6
0
        public GraphRecorder()
        {
            InitializeComponent();
            chart = chart1.ChartAreas[0];

            modbusobj = new clsModbus();

            //modbusobj._GetLRCResultResult += new ModbusRTU.GetLRCResult(singlecmdtxt);
            modbusobj._GetLRCResultResult += new clsModbus.GetLRCResult(singlecmdtxt);
        }
예제 #7
0
        private void CancelSearch()
        {
            try
            {
                progressBar1.Invoke((Action)(() => progressBar1.Value = 0));
                if (_thread != null)
                {
                    if (_thread.IsAlive)
                    {
                        _thread.Abort();
                        // btnClose.Invoke((Action)(() => btnClose.Text = "Close"));
                        // lblStatus1.Invoke((Action)(() => lblStatus1.Text = "Search Abort"));
                        _thread = null;

                        if (lblStatus1.InvokeRequired)
                        {
                            lblStatus1.Invoke((Action)(() => lblStatus1.Text = ""));
                        }
                        else
                        {
                            lblStatus1.Text = "Search Abort";
                        }

                        if (lblFrame.InvokeRequired)
                        {
                            lblFrame.Invoke((Action)(() => lblFrame.Text = ""));
                        }
                        else
                        {
                            lblFrame.Text = "";
                        }
                    }
                }

                if (modbusRTUobj != null)
                {
                    if (modbusRTUobj.IsSerialPortOpen())
                    {
                        modbusRTUobj.CloseSerialPort();
                        modbusRTUobj = null;
                    }
                }
            }

            catch (Exception ex)
            {
                LogWriter.WriteToFile("AutoDetectsettings: ", ex.StackTrace, "DTC_ErrorLog");
            }
            finally
            {
                _thread      = null;
                modbusRTUobj = null;
            }
        }
예제 #8
0
        private void GraphRecorder_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (modbusobj != null)
            {
                if (modbusobj.IsSerialPortOpen())
                {
                    modbusobj.CloseSerialPort();
                }

                modbusobj = null;
            }
        }
예제 #9
0
        private void btnSearch_Click(object sender, EventArgs e)
        {
            btnSearch.Visible = false;
            btnCancel.Visible = true;

            try
            {
                _thread = null;

                if (modbusRTUobj != null)
                {
                    if (modbusRTUobj.IsSerialPortOpen())
                    {
                        modbusRTUobj.CloseSerialPort();
                    }
                    modbusRTUobj = null;
                }

                modbusRTUobj = new clsModbus();
                // modbusRTUobj._GetLRCResultResult += new clsModbus.GetLRCResult(singlecmdtxt);


                table = null;

                //GetResultsTable();

                dataGridView1.Refresh();
                dataGridView1.DataSource = null;
                dataGridView1.Rows.Clear();
                dataGridView1.Refresh();
                ControlBox = false;
                if (_thread == null)
                {
                    _thread = new Thread(new ThreadStart(MyThread));
                    _thread.IsBackground = true;

                    _thread.Start();
                    // btnClose.Text = "Cancel";
                }
                else
                {
                    btnSearch.Enabled = true;
                    btnSet.Enabled    = false;

                    //_thread.Abort();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("1" + ex.Message);
            }
        }
예제 #10
0
        private void AutoSetForm_Load_1(object sender, EventArgs e)
        {
            GetResultsTable();

            cmbBxAddress.DataSource    = address;
            cmbBxBaudrate.DataSource   = baudRates;
            cmbBxBitslength.DataSource = dataLengths;
            cmbBxParity.DataSource     = parity;
            cmbBxStopbits.DataSource   = stopBits;
            cmbBxProtocol.DataSource   = protocol;

            modbusRTUobj = new clsModbus();
        }
예제 #11
0
 private void btnClose_Click(object sender, EventArgs e)
 {
     try
     {
         //ControlBox = true;
         clsModbus mo = new clsModbus();
         //mo.ACloseSerialPort();
         CloseForm();
     }
     catch (Exception ae)
     {
     }
 }
예제 #12
0
 private void btnCancel_Click(object sender, EventArgs e)
 {
     btnCancel.Visible = false;
     btnSearch.Visible = true;
     try
     {
         //ControlBox = true;
         clsModbus mo = new clsModbus();
         //mo.ACloseSerialPort();
         CancelSearch();
     }
     catch (Exception ae)
     {
     }
 }
예제 #13
0
 private void btnSet_Click(object sender, EventArgs e)
 {
     this.DialogResult = DialogResult.OK;
     this.Close();
     //Setting not set issue add close port function
     try
     {
         ControlBox = true;
         clsModbus mo = new clsModbus();
         //mo.ACloseSerialPort();
     }
     catch (Exception ae)
     {
     }
 }
예제 #14
0
        //  private ClassList.Serial serialObj = new ClassList.Serial();
        public SinglecmdText()
        {
            //modbusobj = new ModbusRTU();
            modbusobj = new clsModbus();

            SetValues.Set_Form = "SinglecmdText";
            InitializeComponent();
            //modbusobj._GetLRCResultResult += new ModbusRTU.GetLRCResult(singlecmdtxt);

            modbusobj._GetLRCResultResult += new clsModbus.GetLRCResult(singlecmdtxt);
            worker = new BackgroundWorker();
            worker.WorkerReportsProgress      = true;
            worker.WorkerSupportsCancellation = true;
            worker.DoWork             += worker_DoWork;
            worker.ProgressChanged    += worker_ProgressChanged;
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        }
예제 #15
0
 private void SinglecmdText_FormClosed(object sender, FormClosedEventArgs e)
 {
     try
     {
         if (modbusobj != null)
         {
             if (modbusobj.IsSerialPortOpen())
             {
                 modbusobj.CloseSerialPort();
                 modbusobj = null;
             }
         }
         this.Close();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
예제 #16
0
        public TcSettings()
        {
            InitializeComponent();

            SetValues.Set_Form = "SetDTC";

            modObj = new clsModbus();
            modObj._GetLRCResultResult += new clsModbus.GetLRCResult(singlecmdtxt);
            fileSettings = new FileSettings();
            dsFile       = new DataSet();

            Array.Copy(BasicProperties.baudRates, baudRates, BasicProperties.baudRates.Length);
            Array.Copy(BasicProperties.dataLengths, dataLengths, BasicProperties.dataLengths.Length);
            Array.Copy(BasicProperties.parity, parity, BasicProperties.parity.Length);
            Array.Copy(BasicProperties.stopBits, stopBits, BasicProperties.stopBits.Length);
            Array.Copy(BasicProperties.protocol, protocol, BasicProperties.protocol.Length);

            fileSettings.baudrate   = "-1";
            fileSettings.datalength = "-1";
            fileSettings.mode       = "-1";
            fileSettings.parity     = "-1";
            fileSettings.port       = "-1";
            fileSettings.stopbits   = "-1";
        }
예제 #17
0
        private void btnClose_Click(object sender, EventArgs e)
        {
            SetValuesSingleLine.Addr  = txtbxAddress.Text;
            SetValuesSingleLine.Cmd   = cmbBx_command.SelectedIndex.ToString();
            SetValuesSingleLine.Func  = txtBx_FuncAddress.Text;
            SetValuesSingleLine.WordC = txtBx_WordCount_Writedata.Text;

            isRunning = false;

            if (th != null)
            {
                if (th.IsAlive && th.ThreadState == ThreadState.Running)
                {
                    th.Abort();
                }
            }

            if (modbusobj.IsSerialPortOpen())
            {
                modbusobj.CloseSerialPort();
                modbusobj = null;
            }
            this.Close();
        }
 public frmViewDeviceInfo()
 {
     modbusobj = new clsModbus();
     FillDict();
     InitializeComponent();
 }
예제 #19
0
        private void TcSettings_Load(object sender, EventArgs e)
        {
            lblMsg.Visible = false;
            lblMsg.Text    = "";
            string[] ports = SerialPort.GetPortNames();

            cmbBxBaudrate.DataSource   = BasicProperties.baudRates;
            cmbBxDataLength.DataSource = BasicProperties.dataLengths;
            cmbBxParityBit.DataSource  = BasicProperties.parity;
            cmbBxStopBits.DataSource   = BasicProperties.stopBits;
            cmbBxProtocol.DataSource   = BasicProperties.protocol;

            btnSet.Enabled = false;

            cmbBxBaudrate1.DataSource   = baudRates;
            cmbBxDataLength1.DataSource = dataLengths;
            cmbBxParityBit1.DataSource  = parity;
            cmbBxStopBits1.DataSource   = stopBits;
            cmbBxProtocol1.DataSource   = protocol;

            try
            {
                dsFile.ReadXml(m_exePath1);

                if (dsFile.Tables.Count > 0)
                {
                    DataTable dt = new DataTable();
                    dt = dsFile.Tables["settings"];

                    if (dt.Rows.Count > 0)
                    {
                        fileSettings.baudrate   = string.IsNullOrEmpty(Convert.ToString(dt.Rows[0]["baudrate"])) ? "-1" : Convert.ToString(dt.Rows[0]["baudrate"]);
                        fileSettings.datalength = string.IsNullOrEmpty(Convert.ToString(dt.Rows[0]["datalength"])) ? "-1" : Convert.ToString(dt.Rows[0]["datalength"]);
                        fileSettings.mode       = string.IsNullOrEmpty(Convert.ToString(dt.Rows[0]["mode"])) ? "-1" : Convert.ToString(dt.Rows[0]["mode"]);
                        fileSettings.parity     = string.IsNullOrEmpty(Convert.ToString(dt.Rows[0]["parity"])) ? "-1" : Convert.ToString(dt.Rows[0]["parity"]);
                        portNo = fileSettings.port = string.IsNullOrEmpty(Convert.ToString(dt.Rows[0]["port"])) ? "-1" : Convert.ToString(dt.Rows[0]["port"]);
                        fileSettings.stopbits = string.IsNullOrEmpty(Convert.ToString(dt.Rows[0]["stopbits"])) ? "-1" : Convert.ToString(dt.Rows[0]["stopbits"]);
                    }
                }

                cmbBxBaudrate.SelectedIndex   = Convert.ToInt32(fileSettings.baudrate);
                cmbBxDataLength.SelectedIndex = Convert.ToInt32(fileSettings.datalength);
                cmbBxParityBit.SelectedIndex  = Convert.ToInt32(fileSettings.parity);
                cmbBxStopBits.SelectedIndex   = Convert.ToInt32(fileSettings.stopbits);
                cmbBxProtocol.SelectedIndex   = Convert.ToInt32(fileSettings.mode);

                cmbBxBaudrate1.SelectedIndex   = Convert.ToInt32(fileSettings.baudrate);
                cmbBxDataLength1.SelectedIndex = Convert.ToInt32(fileSettings.datalength);
                cmbBxParityBit1.SelectedIndex  = Convert.ToInt32(fileSettings.parity);
                cmbBxStopBits1.SelectedIndex   = Convert.ToInt32(fileSettings.stopbits);
                cmbBxProtocol1.SelectedIndex   = Convert.ToInt32(fileSettings.mode);

                txtBx_TC_Address.Text = txtBx_PC_Address.Text;

                if (modObj != null)
                {
                    if (modObj.IsSerialPortOpen())
                    {
                        modObj.CloseSerialPort();
                    }
                }
                else
                {
                    modObj = new clsModbus();
                    modObj._GetLRCResultResult += new clsModbus.GetLRCResult(singlecmdtxt);
                }
            }
            catch (Exception ex)
            {
                lblMsg.Visible = true;
                lblMsg.Text    = "(R)Default settings file not found.";
                errorCode++;
            }
        }
예제 #20
0
        private void btnSet_Click(object sender, EventArgs e)
        {
            //MessageBoxEx.Show(this, "Please Connect FLA0102TV Expansion & Power-ON to enable Auto Sync", "Communication Auto Sync Success", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

            try
            {
                #region Port Settings
                string portNameN   = SetValues.Set_PortName;
                int    baudRateN   = Convert.ToInt32(SetValues.Set_Baudrate);
                string parityN     = SetValues.Set_parity;
                int    bitsLengthN = SetValues.Set_BitsLength;
                int    stopBitsN   = Convert.ToInt32(SetValues.Set_StopBits);

                if (modObj == null)
                {
                    modObj = new clsModbus();
                    modObj._GetLRCResultResult += new clsModbus.GetLRCResult(singlecmdtxt);
                }
                else
                {
                    if (modObj.IsSerialPortOpen())
                    {
                        modObj.CloseSerialPort();
                    }
                }

                //  if (modObj.OpenSerialPort(portNameN, baudRateN, parityN, stopBitsN, bitsLengthN))
                {
                    //LogWriter.WriteToFile("Load() =>", "Port Opened"
                    //        , "OnlineMonitor");

                    string finalString    = "";
                    string oLdNodeAddress = txtBx_PC_Address.Text; // Convert.ToInt32(txtBx_PC_Address.Text).ToString("X2").PadLeft(2, '0');
                    string newValue       = txtBx_TC_Address.Text; // Convert.ToInt32(txtBx_TC_Address.Text).ToString("X2").PadLeft(2, '0');

                    finalString += cmbBxBaudrate1.SelectedItem + "-";
                    finalString += cmbBxDataLength1.SelectedItem + "-";
                    finalString += cmbBxParityBit1.SelectedItem + "-";
                    finalString += cmbBxStopBits1.SelectedItem;

                    if (cmbBxProtocol1.SelectedIndex == 1)
                    {
                        finalString += "-A";
                    }
                    else if (cmbBxProtocol1.SelectedIndex == 2)
                    {
                        finalString += "-R";
                    }

                    //need to USB Setting
                    // finalString += "-R";

                    if (TcMap.ContainsKey(finalString))
                    {
                        string val = Convert.ToInt16(newValue).ToString("X2").PadLeft(2, '0') + Convert.ToInt16(TcMap[finalString]).ToString("X2").PadLeft(2, '0');
                        //MessageBox.Show(val);
                        int    b = Convert.ToInt32(cmbBxBaudrate.SelectedItem);
                        string p = Convert.ToString(cmbBxParityBit.SelectedItem);
                        int    d = Convert.ToInt32(cmbBxDataLength.SelectedItem);
                        int    s = Convert.ToInt32(cmbBxStopBits.SelectedItem);

                        th = new Thread(() =>
                        {
                            int res = CreateFrames(oLdNodeAddress, "06", "470B", val, false, SetValues.Set_PortName, b, p, d, s);

                            if (res == 1)
                            {
                                try
                                {
                                    XElement xEmp = XElement.Load(m_exePath1);

                                    var settingsDetails = from emps in xEmp.Elements("settings")
                                                          select emps;

                                    settingsDetails.First().Element("port").Value = Convert.ToString(portNo);
                                    string valBaud = "0";
                                    cmbBxBaudrate1.Invoke((Action)(() =>
                                    {
                                        valBaud = Convert.ToString(cmbBxBaudrate1.SelectedIndex);
                                        SetValues.Set_Baudrate = cmbBxBaudrate1.SelectedItem.ToString();
                                    }));
                                    settingsDetails.First().Element("baudrate").Value = valBaud;

                                    string valParity = "0";
                                    cmbBxParityBit1.Invoke((Action)(() =>
                                    {
                                        valParity = Convert.ToString(cmbBxParityBit1.SelectedIndex);
                                        SetValues.Set_parity = cmbBxParityBit1.SelectedItem.ToString();
                                    }));

                                    settingsDetails.First().Element("parity").Value = valParity;

                                    string valData = "0";
                                    cmbBxDataLength1.Invoke((Action)(() =>
                                    {
                                        valData = Convert.ToString(cmbBxDataLength1.SelectedIndex);
                                        SetValues.Set_BitsLength = Convert.ToInt32(cmbBxDataLength1.SelectedItem.ToString());
                                    }));
                                    settingsDetails.First().Element("datalength").Value = valData;

                                    string valStop = "0";
                                    cmbBxStopBits1.Invoke((Action)(() =>
                                    {
                                        valStop = Convert.ToString(cmbBxStopBits1.SelectedIndex);
                                        SetValues.Set_StopBits = cmbBxStopBits1.SelectedItem.ToString();
                                    }));
                                    settingsDetails.First().Element("stopbits").Value = valStop;

                                    string valProtocol = "0";
                                    cmbBxProtocol1.Invoke((Action)(() =>
                                    {
                                        valProtocol = Convert.ToString(cmbBxProtocol1.SelectedIndex);
                                        SetValues.Set_CommunicationProtocol = cmbBxProtocol1.SelectedItem.ToString();
                                    }));
                                    settingsDetails.First().Element("mode").Value = valProtocol;

                                    xEmp.Save(m_exePath1);

                                    string valNode = "0";
                                    txtBx_TC_Address.Invoke((Action)(() => { valNode = Convert.ToString(txtBx_TC_Address.Text); }));
                                    txtBx_PC_Address.Invoke((Action)(() => txtBx_PC_Address.Text = valNode));

                                    cmbBxBaudrate.Invoke((Action)(() =>
                                    {
                                        cmbBxBaudrate.SelectedIndex = Convert.ToInt32(valBaud);
                                    }));

                                    cmbBxDataLength.Invoke((Action)(() =>
                                    {
                                        cmbBxDataLength.SelectedIndex = Convert.ToInt32(valData);
                                    }));

                                    cmbBxParityBit.Invoke((Action)(() =>
                                    {
                                        cmbBxParityBit.SelectedIndex = Convert.ToInt32(valParity);
                                    }));

                                    cmbBxStopBits.Invoke((Action)(() =>
                                    {
                                        cmbBxStopBits.SelectedIndex = Convert.ToInt32(valStop);
                                    }));

                                    cmbBxProtocol.Invoke((Action)(() =>
                                    {
                                        cmbBxProtocol.SelectedIndex = Convert.ToInt32(valProtocol);
                                    }));

                                    string successMsg = "Updated ";

                                    if (chkBx_Autosync.Checked)
                                    {
                                        try
                                        {
                                            if (SetValues.Set_BoolUSBdata != true)
                                            {
                                                Thread.Sleep(500);
                                                IWin32Window ownerr = this;
                                                //if (clicked == DialogResult.OK)
                                                //{
                                                int res1 = CreateFrames(newValue, "06", "1022", "0001", false,
                                                                        SetValues.Set_PortName, Convert.ToInt32(SetValues.Set_Baudrate),
                                                                        SetValues.Set_parity, SetValues.Set_BitsLength, Convert.ToInt32(SetValues.Set_StopBits));

                                                if (res1 == 1)
                                                {
                                                    this.Invoke((Action)(() =>
                                                    {
                                                        DialogResult clicked = MessageBoxEx.Show(ownerr, "Please Connect FLA0102TV Expansions & Re-Power ON to enable Auto Sync", "Communication Auto Sync Success", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                                                    }));

                                                    successMsg += " and Auto Synced expansions ";
                                                }
                                            }
                                            else
                                            {
                                                Thread.Sleep(500);
                                                IWin32Window ownerr = this;
                                                //if (clicked == DialogResult.OK)
                                                //{
                                                int res1 = CreateFramesRTUonly(newValue, "06", "1022", "0001", false,
                                                                               SetValues.Set_PortName, Convert.ToInt32(SetValues.Set_Baudrate),
                                                                               SetValues.Set_parity, SetValues.Set_BitsLength, Convert.ToInt32(SetValues.Set_StopBits));

                                                if (res1 == 1)
                                                {
                                                    this.Invoke((Action)(() =>
                                                    {
                                                        DialogResult clicked = MessageBoxEx.Show(ownerr, "Please Connect FLA0102TV Expansions & Re-Power ON to enable Auto Sync", "Communication Auto Sync Success", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                                                    }));

                                                    successMsg += " and Auto Synced expansions ";
                                                }
                                            }
                                        }
                                        catch (Exception em)
                                        {
                                        }
                                    }

                                    lblMsg.Invoke((Action)(() =>
                                    {
                                        Thread.Sleep(400);
                                        lblMsg.Visible = true;
                                        lblMsg.Text = successMsg + " successfully.";
                                    }));
                                }
                                catch (Exception ex)
                                {
                                    lblMsg.Invoke((Action)(() =>
                                    {
                                        lblMsg.Visible = true;
                                        lblMsg.Text = "(W)Default settings file not found.";
                                        errorCode++;
                                    }));
                                }
                            }
                            else if (res == 2)
                            {
                                lblMsg.Invoke((Action)(() =>
                                {
                                    lblMsg.Visible = true;
                                    lblMsg.Text = "Something went wrong. Or Check connection";
                                }));
                            }
                            else if (res == 3)
                            {
                                lblMsg.Invoke((Action)(() =>
                                {
                                    lblMsg.Visible = true;
                                    lblMsg.Text = "Something went wrong.";
                                }));
                            }
                        });
                        th.IsBackground = true;
                        th.Start();
                    }
                    else
                    {
                        lblMsg.Invoke((Action)(() =>
                        {
                            lblMsg.Visible = true;
                            lblMsg.Text = "(K)Error occurred.";
                        }));
                    }
                }
                //else
                //{
                //    lblMsg.Visible = true;
                //    lblMsg.Text = "Connection error.";
                //}
                #endregion
            }
            catch (Exception ex)
            {
                this.Invoke((Action)(() =>
                {
                    MessageBoxEx.Show(ex.Message);
                }));
            }
            finally
            {
                //if (modObj != null)
                //{
                //    if (modObj.IsSerialPortOpen())
                //        modObj.CloseSerialPort();
                //}
            }
        }