public void ParameterChange(object msgobj)
        {
            MainNetworkClass networkmain = (MainNetworkClass)msgobj;

            PLCTelnet2 = new TelnetClient();
            #region Connection PLC1,PLC2,Micron Server
            Log1.Info("ParameterChange Thread Start");
            while (!bTerminate)
            {
                try
                {
                    Thread.Sleep(100);
                    #region PLC 1
                    //PLC Read Write Cycle
                    if (PLCTelnet != null)
                    {
                        if (PLCTelnet.connected)
                        {
                            #region Parameter Change
                            //bool bypass = true;



                            if ((PLCWriteCommand[459] == 0x08 && PLCQueryRx[263] == 0x08))  //D419 and D126
                            {
                                byte[] PName = new byte[2];
                                Array.Copy(PLCQueryRx, 265, PName, 0, 2); //D127
                                Int32 PNint = (Int32)(BitConverter.ToInt16(PName, 0));
                                ParaName = PNint.ToString();


                                byte[] POld = new byte[4];
                                Array.Copy(PLCQueryRx, 267, POld, 0, 4); //D128~D129
                                Int32 POldint = (Int32)(BitConverter.ToInt32(POld, 0));
                                ParaOldValue = POldint.ToString();



                                byte[] PNew = new byte[4];
                                Array.Copy(PLCQueryRx, 271, PNew, 0, 4); //D130~D131
                                Int32 PNewint = (Int32)(BitConverter.ToInt32(PNew, 0));
                                ParaNewValue = PNewint.ToString();

                                if (PNint != -9999 && POldint != -9999 && PNewint != -9999 && parameterflag == 0)
                                {
                                    try
                                    {
                                        ParaName = ParameterDesPLC1(int.Parse(ParaName));


                                        networkmain.Client_SendParameterchange1(ParaName, ParaOldValue, ParaNewValue);
                                        parameter.Info("parameter change at PLC1 " + ParaName + "," + ParaOldValue + "," + ParaNewValue);
                                    }
                                    catch {
                                    }

                                    Thread.Sleep(10);
                                    PLCWriteCommand[469] = 0x07;//D424
                                    parameterflag        = 1;
                                }

                                if (PNint == -9999 && parameterflag == 1)
                                {
                                    PLCWriteCommand[469] = 0x00;//D424
                                    parameterflag        = 0;
                                }
                            }
                            if (PLCQueryRx[263] == 0x0F && PLCWriteCommand[459] == 0x08)
                            {
                                PLCWriteCommand[459] = 0x00;
                                string token = networkmain.Token1.ToString();
                                networkmain.Client_SendParameterchangeLogout("DENY", "1", "1", token);
                                parameter.Info("parameter change at PLC1 logout " + token);
                            }



                            #endregion


                            #region


                            #endregion

                            #region 1DM Each Stop

                            try{
                                if ((PLCQueryRx[PLCQueryRx_DM198] == 0x01) &&
                                    (PLCQueryRx6[PLCQueryRx_DM5199] == 0x01))

                                {
                                    RoyalFlush();


                                    PLCWriteCommand[PLCWriteCommand_DM303] = 0x07;

                                    PLCWriteCommand6[PLCWriteCommand_DM5353] = 0x07;
                                }
                                else
                                {
                                    PLCWriteCommand[PLCWriteCommand_DM303] = 0x00;

                                    PLCWriteCommand6[PLCWriteCommand_DM5353] = 0x00;
                                }
                            }

                            catch (Exception ex)
                            {
                                Log1.Info(ex.ToString());
                            }

                            #endregion



                            #region test DM6110 to DM6499 Reading

                            byte[] TestPara = new byte[2];
                            Array.Copy(PLCQueryRxPara, 11, TestPara, 0, 2); //D6100
                            Int32 TestPara1      = (Int32)(BitConverter.ToInt16(TestPara, 0));
                            int   TestParastring = TestPara1;
                            if (TestParastring > 0)
                            {
                                //Blocktest.Info("PLC1 Testing DM6100 ," + TestParastring);
                            }

                            byte[] TestPara2 = new byte[2];
                            Array.Copy(PLCQueryRxPara, 809, TestPara2, 0, 2); //D6499
                            Int32 TestPara3       = (Int32)(BitConverter.ToInt16(TestPara2, 0));
                            int   TestParastring1 = TestPara3;
                            if (TestParastring1 > 0)
                            {
                                // Blocktest.Info("PLC1 Testing DM6499 ," + TestParastring1);
                            }


                            #endregion
                        }
                    }
                    #endregion
                    #region PLC 2
                    if (PLCTelnet2.connected)
                    {
                        #region Parameter Change
                        //bool bypass = true;

                        if ((PLCWriteCommand6[315] == 0x08 && PLCQueryRx6[353] == 0x08)) //  ,D5347,D5171
                        {
                            byte[] PName = new byte[2];
                            Array.Copy(PLCQueryRx6, 303, PName, 0, 2);  //D5146
                            Int32 PNint = (Int32)(BitConverter.ToInt16(PName, 0));
                            ParaName1 = PNint.ToString();


                            byte[] POld = new byte[4];
                            Array.Copy(PLCQueryRx6, 383, POld, 0, 4);  //D5186~D5187
                            Int32 POldint = (Int32)(BitConverter.ToInt32(POld, 0));
                            ParaOldValue1 = POldint.ToString();



                            byte[] PNew = new byte[4];
                            Array.Copy(PLCQueryRx6, 387, PNew, 0, 4);  //D5188~D5189
                            Int32 PNewint = (Int32)(BitConverter.ToInt32(PNew, 0));
                            ParaNewValue1 = PNewint.ToString();

                            if (PNint != -9999 && POldint != -9999 && PNewint != -9999 && parameterflag2 == 0)
                            {
                                try
                                {
                                    ParaName1 = ParameterDesPLC2(int.Parse(ParaName1));



                                    networkmain.Client_SendParameterchange1(ParaName1, ParaOldValue1, ParaNewValue1);
                                    parameter.Info("parameter change at PLC2 " + ParaName1 + "," + ParaOldValue1 + "," + ParaNewValue1);
                                }
                                catch
                                {
                                }
                                Thread.Sleep(10);
                                PLCWriteCommand6[465] = 0x07;//D5422
                                parameterflag2        = 1;
                            }

                            if (PNint == -9999 && parameterflag2 == 1)
                            {
                                PLCWriteCommand6[465] = 0x00;//D5422
                                parameterflag2        = 0;
                            }



                            #region Sealer parameter

                            byte[] SealerPName = new byte[2];
                            Array.Copy(PLCQueryRx6, 279, SealerPName, 0, 2);  //D5134
                            Int32 SealerPNint = (Int32)(BitConverter.ToInt16(SealerPName, 0));
                            SealerParaName = SealerPNint.ToString();


                            byte[] SealerPOld = new byte[12];
                            Array.Copy(PLCQueryRx6, 281, SealerPOld, 0, 12);  //D5135~D5140
                            Int32 SealerPOldint = (Int32)(BitConverter.ToInt16(SealerPOld, 0));
                            SealerParaOldValue = SealerPOldint.ToString();
                            string SealerOld = System.Text.Encoding.Default.GetString(SealerPOld);



                            byte[] SealerPNew = new byte[12];
                            Array.Copy(PLCQueryRx6, 371, SealerPNew, 0, 12);  //D5180~D5185
                            Int32 SealerPNewint = (Int32)(BitConverter.ToInt16(SealerPNew, 0));
                            SealerParaNewValue = SealerPNewint.ToString();
                            string SealerNew = System.Text.Encoding.Default.GetString(SealerPNew);

                            if (SealerPNint > 0 && SealerPOldint > 0 && SealerPNewint > 0 && Sealerparameterflag == 0)
                            {
                                networkmain.Client_SendParameterchange1(SealerParaName, SealerOld, SealerNew);
                                parameter.Info("Sealer parameter change at PLC2 " + SealerParaName + "," + SealerOld + "," + SealerNew);
                                Thread.Sleep(10);
                                PLCWriteCommand6[479] = 0x07;//D5429
                                Sealerparameterflag   = 1;
                            }

                            if (SealerPNint == 0 && Sealerparameterflag == 1)
                            {
                                PLCWriteCommand6[479] = 0x00;//D5429
                                Sealerparameterflag   = 0;
                            }



                            #endregion
                        }


                        if (PLCQueryRx6[353] == 0x0F && PLCWriteCommand6[315] == 0x08)
                        {
                            PLCWriteCommand6[315] = 0x00;
                            string token = networkmain.Token2.ToString();
                            networkmain.Client_SendParameterchangeLogout("DENY", "1", "2", token);
                            parameter.Info("parameter change at PLC2 logout " + token);
                        }



                        #endregion



                        #region check st5 RJ result


                        //byte[] bcarrayst5check = new byte[10];
                        //Array.Copy(PLCQueryRx6, 121, bcarrayst5check, 0, 10);  //D5055
                        //St5CheckFL = System.Text.Encoding.Default.GetString(bcarrayst5check);
                        //if (St5CheckFL !="\0\0\0\0\0\0\0\0\0\0")
                        //{
                        //    checkresultST5.Info("FL For ST5 give to ST6" + St5CheckFL);
                        //}

                        //else
                        //{
                        //    St5CheckFL = "\0\0\0\0\0\0\0\0\0\0";
                        //}
                        //byte[] bcarrayst5checkRJ = new byte[2];
                        //Array.Copy(PLCQueryRx6, 219, bcarrayst5checkRJ, 0, 2);  //D5104
                        //string   St5CheckFLRJ = System.Text.Encoding.Default.GetString(bcarrayst5checkRJ);

                        //checkresultST5.Info("FL RJ For ST5 give to ST6" + St5CheckFLRJ);



                        // byte[] bcarrayst5checkS1 = new byte[2];
                        // Array.Copy(PLCQueryRx6, 165, bcarrayst5checkS1, 0, 2);  //D5077
                        // string   St5CheckFLS1 = System.Text.Encoding.Default.GetString(bcarrayst5checkS1);

                        //checkresultST5.Info("FL RJ For ST5 Sealer 1 give from Server===>" + St5CheckFLS1);

                        //byte[] bcarrayst5checkS2 = new byte[2];
                        // Array.Copy(PLCQueryRx6, 167, bcarrayst5checkS2, 0, 2);  //D5078
                        // string   St5CheckFLS2 = System.Text.Encoding.Default.GetString(bcarrayst5checkS2);

                        //checkresultST5.Info("FL RJ For ST5 Sealer 2 give from Server===>" + St5CheckFLS2);



                        // byte[] bcarrayst5checkS3 = new byte[2];
                        // Array.Copy(PLCQueryRx6, 169, bcarrayst5checkS3, 0, 2);  //D5077
                        // string   St5CheckFLS3 = System.Text.Encoding.Default.GetString(bcarrayst5checkS3);

                        // checkresultST5.Info("FL RJ For ST5 Sealer 3 give from Server===>" + St5CheckFLS3);



                        #endregion



                        #region test DM6110 to DM6499 Reading

                        byte[] TestPara = new byte[2];
                        Array.Copy(PLCQueryRxParaPlc2, 11, TestPara, 0, 2);    //D6100
                        Int32 TestPara1      = (Int32)(BitConverter.ToInt16(TestPara, 0));
                        int   TestParastring = TestPara1;
                        if (TestParastring > 0)
                        {
                            // Blocktest.Info("PLC2 Testing DM6100 ," + TestParastring);
                        }

                        byte[] TestPara2 = new byte[2];
                        Array.Copy(PLCQueryRxParaPlc2, 809, TestPara2, 0, 2);    //D6499
                        Int32 TestPara3       = (Int32)(BitConverter.ToInt16(TestPara2, 0));
                        int   TestParastring1 = TestPara3;
                        if (TestParastring1 > 0)
                        {
                            //Blocktest.Info("PLC2 Testing DM6499 ," + TestParastring1);
                        }


                        #endregion
                    }


                    #endregion
                }
                catch (Exception ex)
                {
                    Log1.Info(ex.ToString());
                }
                // Log1.Info(" ParameterChange Thread Exit");
            }//try first

            #endregion
        }
Exemplo n.º 2
0
        public void RunPLC01Scan(object msgobj)
        {
            MainNetworkClass networkmain = (MainNetworkClass)msgobj;

            byte[] tmpRx     = new byte[411];
            byte[] tmpRx1    = new byte[611];
            byte[] tmpRx2    = new byte[811];
            bool   plc1Break = false;

            PLCTelnet = new TelnetClient();
            //check for TCP network connection for PLC
            while (!bTerminate)
            {
                Thread.Sleep(100);
                try
                {
                    if (PLCTelnet.connected == false)
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.Load(@"Config.xml");
                        XmlNode node = doc.SelectSingleNode(@"/CONFIG/PLCCONTROLLER1/ADD");
                        System.Net.IPAddress address = System.Net.IPAddress.Parse(node.InnerText);
                        node = doc.SelectSingleNode(@"/CONFIG/PLCCONTROLLER1/PORT");
                        PLCNetworkAddress = "Trying to connect to " + address.ToString() + " Port : " + node.InnerText;
                        if (PLCTelnet.ConnectToHost(address, int.Parse(node.InnerText)) == false)
                        {
                            PLCNetworkAddress = "Connected to PLCServer1 "
                                                + address.ToString()
                                                + " Port : "
                                                + node.InnerText
                                                + " Fail"; //not connected
                            Thread.Sleep(100);             // try to connect to server
                            if (!plc1Break)
                            {
                                MyEventQ.AddQ("5;PLCCommunicationBreak;PLC Number;1");//Push message to stack
                                EvtLog.Info("5;PLCCommunicationBreak;PLC Number;1");
                                plc1Break = true;
                            }
                            continue;
                        }
                        else
                        {
                            PLCNetworkAddress = "Connected to PLCServer1 " + address.ToString() + " Port : " + node.InnerText;//connected
                        }
                        plc1Break = false;
                    }
                    //PLC Read Write Cycle
                    if (PLCTelnet.connected)
                    {
                        //get read data from PLC
                        PLCTelnet.SendDataToHost(PLCQueryCmd);//query data from plc D000 to D199
                        string tmpstr100;
                        tmpstr100 = PLCTelnet.GetDataFromHost(ref tmpRx, tmpRx.Length);
                        if (tmpstr100.StartsWith("d00000ffff") && tmpstr100.Length == 822)
                        {
                            Array.Copy(tmpRx, PLCQueryRx, PLCQueryRx.Length);
                        }
                        else
                        {
                            tmpstr100 = "No Data";
                        }

                        int timeoutcounter = 0;
                        while ((tmpstr100 == "No Data") && PLCTelnet.connected)
                        {
                            Thread.Sleep(waitdelay);
                            //tmpstr100 = PLCTelnet.GetDataFromHost(ref PLCQueryRx, PLCQueryRx.Length);
                            tmpstr100 = PLCTelnet.GetDataFromHost(ref tmpRx, tmpRx.Length);
                            //tmpstr100 = PLCTelnet.GetDataFromHost(ref PLCQueryRx, PLCQueryRx.Length);
                            if (tmpstr100.StartsWith("d00000ffff") && (tmpstr100.Length == 822))
                            {
                                Array.Copy(tmpRx, PLCQueryRx, PLCQueryRx.Length);
                            }
                            else
                            {
                                tmpstr100 = "No Data";
                            }
                            //log.Info(tmpstr100);
                            timeoutcounter++;
                            if (timeoutcounter == 10)
                            {
                                //assume connection broken
                                PLCTelnet.Close();
                                break;
                            }
                        }

                        //Update 14/12/2015 May Pon
                        #region READ  PLC1 Memory 6100 to 6499



                        timeoutcounter = 0;
                        if (PLCTelnet.connected == false)
                        {
                            continue;
                        }
                        PLCTelnet.SendDataToHost(PLCQueryCmdPara);//query data from plc dm700 to dm999
                        //tmpstr100 = PLCTelnet.GetDataFromHost(ref PLCQueryRx7, PLCQueryRx7.Length);
                        tmpstr100 = PLCTelnet.GetDataFromHost(ref tmpRx2, tmpRx2.Length);
                        //tmpstr100 = PLCTelnet.GetDataFromHost(ref PLCQueryRx, PLCQueryRx.Length);
                        if (tmpstr100.StartsWith("d00000ffff") && (tmpstr100.Length == 1622))
                        {
                            Array.Copy(tmpRx2, PLCQueryRxPara, PLCQueryRxPara.Length);
                        }
                        else
                        {
                            tmpstr100 = "No Data";
                        }

                        while ((tmpstr100 == "No Data") && PLCTelnet.connected)
                        {
                            Thread.Sleep(waitdelay);
                            tmpstr100 = PLCTelnet.GetDataFromHost(ref tmpRx2, tmpRx2.Length);
                            //tmpstr100 = PLCTelnet.GetDataFromHost(ref PLCQueryRx, PLCQueryRx.Length);
                            if ((tmpstr100.StartsWith("d00000ffff") && (tmpstr100.Length == 1622)))
                            {
                                Array.Copy(tmpRx2, PLCQueryRxPara, PLCQueryRxPara.Length);
                            }
                            else
                            {
                                tmpstr100 = "No Data";
                            }

                            timeoutcounter++;
                            if (timeoutcounter == 10)
                            {
                                //assume connection broken
                                PLCTelnet.Close();
                                break;
                            }
                        }



                        #endregion


                        //Update 25/7/2015
                        #region   READ  PLC1 Memory 700 to 999

                        //timeoutcounter = 0;
                        //if (PLCTelnet.connected == false) continue;
                        //PLCTelnet.SendDataToHost(PLCQueryCmd7);//query data from plc dm700 to dm999
                        ////tmpstr100 = PLCTelnet.GetDataFromHost(ref PLCQueryRx7, PLCQueryRx7.Length);
                        //tmpstr100 = PLCTelnet.GetDataFromHost(ref tmpRx1, tmpRx1.Length);
                        ////tmpstr100 = PLCTelnet.GetDataFromHost(ref PLCQueryRx, PLCQueryRx.Length);
                        //if (tmpstr100.StartsWith("d00000ffff") && (tmpstr100.Length == 1222))
                        //{
                        //    Array.Copy(tmpRx1, PLCQueryRx7, PLCQueryRx7.Length);
                        //}
                        //else
                        //    tmpstr100 = "No Data";
                        //log.Info(tmpstr100);
                        //while ((tmpstr100 == "No Data") && PLCTelnet.connected)
                        //{
                        //    Thread.Sleep(waitdelay);
                        //    tmpstr100 = PLCTelnet.GetDataFromHost(ref tmpRx1, tmpRx1.Length);
                        //    //tmpstr100 = PLCTelnet.GetDataFromHost(ref PLCQueryRx, PLCQueryRx.Length);
                        //    if ((tmpstr100.StartsWith("d00000ffff") && (tmpstr100.Length == 1222)))
                        //    {
                        //        Array.Copy(tmpRx1, PLCQueryRx7, PLCQueryRx7.Length);
                        //    }
                        //    else
                        //        tmpstr100 = "No Data";
                        //    log.Info(tmpstr100);
                        //    timeoutcounter++;
                        //    if (timeoutcounter == 10)
                        //    {
                        //        //assume connection broken
                        //        PLCTelnet.Close();
                        //        break;
                        //    }
                        //}
                        #endregion


                        timeoutcounter = 0;
                        if (PLCTelnet.connected == false)
                        {
                            continue;
                        }
                        PLCTelnet.SendDataToHost(PLCWriteCommand);
                        tmpstr100 = PLCTelnet.GetDataFromHost(ref PLCWriteCommandRX, PLCWriteCommandRX.Length);//write to dm 200 to dm 599
                        //  log.Info("PLC 1 Write " + tmpstr100);
                        while ((tmpstr100 == "No Data") && PLCTelnet.connected)
                        {
                            Thread.Sleep(waitdelay);
                            tmpstr100 = PLCTelnet.GetDataFromHost(ref PLCWriteCommandRX, PLCWriteCommandRX.Length);
                            //  log.Info("PLC 1 Write " + tmpstr100);
                            timeoutcounter++;
                            if (timeoutcounter == 10)
                            {
                                //assume connection broken
                                PLCTelnet.Close();
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Info("PLC01Polling Thread Error " + ex.ToString());
                }
            }//while ! terminated loop
        }
        public void RunPLC02Scan(object msgobj)
        {
            MainNetworkClass networkmain = (MainNetworkClass)msgobj;

            PLCTelnet2 = new TelnetClient();
            byte[] tmpRx6    = new byte[411];
            byte[] tmpRxPlc2 = new byte[1011];
            bool   plc2Break = false;

            while (!bTerminate)
            {
                Thread.Sleep(100);
                try
                {
                    if (PLCTelnet2.connected == false)
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.Load(@"Config.xml");
                        XmlNode node = doc.SelectSingleNode(@"/CONFIG/PLCCONTROLLER2/ADD");
                        System.Net.IPAddress address = System.Net.IPAddress.Parse(node.InnerText);
                        node = doc.SelectSingleNode(@"/CONFIG/PLCCONTROLLER2/PORT");
                        PLC2NetworkAddress = "Trying to connect to " + address.ToString() + " Port : " + node.InnerText;
                        if (PLCTelnet2.ConnectToHost(address, int.Parse(node.InnerText)) == false)
                        {
                            PLC2NetworkAddress = "Connected to PLCServer2 "
                                                 + address.ToString()
                                                 + " Port : "
                                                 + node.InnerText + " Fail"; //not connected
                            Thread.Sleep(100);                               // try to connect to server
                            if (!plc2Break)
                            {
                                MyEventQ.AddQ("5;PLCCommunicationBreak;PLC Number;2");//Push message to stack
                                EvtLog.Info("5;PLCCommunicationBreak;PLC Number;2");
                                plc2Break = true;
                            }
                            continue;
                        }
                        else
                        {
                            PLC2NetworkAddress = "Connected to PLCServer2 " + address.ToString() + " Port : " + node.InnerText;//connected
                        }
                        plc2Break = false;
                    }
                    if (PLCTelnet2.connected)
                    {
                        string tmpstr;
                        int    timeoutcounter = 0;
                        PLCTelnet2.SendDataToHost(PLCQueryCmd6);//PLC02 Read Cycle
                        Thread.Sleep(waitdelay);
                        tmpstr = PLCTelnet2.GetDataFromHost(ref tmpRx6, PLCQueryRx6.Length);
                        //tmpstr100 = PLCTelnet.GetDataFromHost(ref PLCQueryRx, PLCQueryRx.Length);
                        if (tmpstr != "No Data")
                        {
                            if ((tmpstr.StartsWith("d00000ffff")) && (tmpstr.Length == 822))
                            {
                                Array.Copy(tmpRx6, PLCQueryRx6, PLCQueryRx6.Length);
                            }
                            else
                            {
                                PLCTelnet2.Close();
                            }
                        }
                        //  log.Info(tmpstr);
                        while ((tmpstr == "No Data") && PLCTelnet.connected)
                        {
                            Thread.Sleep(waitdelay);
                            tmpstr = PLCTelnet2.GetDataFromHost(ref tmpRx6, PLCQueryRx6.Length);
                            if (tmpstr != "No Data")
                            {
                                if ((tmpstr.StartsWith("d00000ffff")) && (tmpstr.Length == 822))
                                {
                                    Array.Copy(tmpRx6, PLCQueryRx6, PLCQueryRx6.Length);
                                }
                                else
                                {
                                    PLCTelnet2.Close();
                                    break;
                                }
                            }
                            //No Data loop again
                            //  log.Info(tmpstr);
                            timeoutcounter++;
                            if (timeoutcounter == 10)
                            {
                                //assume connection broken
                                PLCTelnet2.Close();
                                break;
                            }
                        }


                        #region READ  PLC2 Memory 6100 to 6499



                        timeoutcounter = 0;
                        if (PLCTelnet2.connected == false)
                        {
                            continue;
                        }
                        PLCTelnet2.SendDataToHost(PLCQueryCmdParaPlc2);//query data from plc dm700 to dm999
                        //tmpstr100 = PLCTelnet.GetDataFromHost(ref PLCQueryRx7, PLCQueryRx7.Length);
                        tmpstr = PLCTelnet2.GetDataFromHost(ref tmpRxPlc2, tmpRxPlc2.Length);
                        //tmpstr100 = PLCTelnet.GetDataFromHost(ref PLCQueryRx, PLCQueryRx.Length);
                        if (tmpstr.StartsWith("d00000ffff") && (tmpstr.Length == 2022))
                        {
                            Array.Copy(tmpRxPlc2, PLCQueryRxParaPlc2, PLCQueryRxParaPlc2.Length);
                        }
                        else
                        {
                            tmpstr = "No Data";
                        }

                        while ((tmpstr == "No Data") && PLCTelnet2.connected)
                        {
                            Thread.Sleep(waitdelay);
                            tmpstr = PLCTelnet2.GetDataFromHost(ref tmpRxPlc2, tmpRxPlc2.Length);
                            //tmpstr = PLCTelnet.GetDataFromHost(ref PLCQueryRx, PLCQueryRx.Length);
                            if ((tmpstr.StartsWith("d00000ffff") && (tmpstr.Length == 2022)))
                            {
                                Array.Copy(tmpRxPlc2, PLCQueryRxParaPlc2, PLCQueryRxParaPlc2.Length);
                            }
                            else
                            {
                                tmpstr = "No Data";
                            }

                            timeoutcounter++;
                            if (timeoutcounter == 10)
                            {
                                //assume connection broken
                                PLCTelnet2.Close();
                                break;
                            }
                        }



                        #endregion



                        timeoutcounter = 0;
                        PLCTelnet2.SendDataToHost(PLCWriteCommand6);//PLC02 Write cycle
                        Thread.Sleep(waitdelay);
                        tmpstr = PLCTelnet2.GetDataFromHost(ref PLCWriteCommandRX6, PLCWriteCommandRX6.Length);
                        //   log.Info("PLC 2 Write " + tmpstr);
                        while ((tmpstr == "No Data") && PLCTelnet.connected)
                        {
                            Thread.Sleep(waitdelay);
                            tmpstr = PLCTelnet2.GetDataFromHost(ref PLCWriteCommandRX6, PLCWriteCommandRX6.Length);
                            //    log.Info("PLC 2 Write " + tmpstr);
                            timeoutcounter++;
                            if (timeoutcounter == 10)
                            {
                                //assume connection broken
                                PLCTelnet2.Close();
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex.ToString());
                }
            }
        }