private void btn_GetValue_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SystemLog.Output(SystemLog.MSG_TYPE.War, "Start read data from PLC", "START");
                stopwatch = new Stopwatch();
                MachineItem machine = new MachineItem();
                machine.IP   = "172.16.1.145";
                machine.Line = "L03";
                int ConnectionPLC                 = -1;
                Sharp.ReadVariablePLC pLC         = new Sharp.ReadVariablePLC(machine.IP, 0, 0, out ConnectionPLC);
                MQCVariable           mQCVariable = new MQCVariable();
                mQCVariable.DicSPLCtatus = pLC.ReadStatusPLCMQC();

                mQCVariable.QRMES           = pLC.ReadAreaByteToString(181, 100, 100);
                mQCVariable.QRID            = pLC.ReadAreaByteToString(181, 0, 100);
                mQCVariable.ListMQCQty      = pLC.ReadQuantityMQC();
                mQCVariable.ListNG38        = pLC.ReadAreaIntToListInt(3, 4, 76);
                mQCVariable.ListRW38        = pLC.ReadAreaIntToListInt(4, 4, 76);
                mQCVariable.ListQtyProduced = pLC.ReadQuantityMQCProduced();
                SystemLog.Output(SystemLog.MSG_TYPE.War, "ConnectionPLC", ConnectionPLC.ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, VariablePLC.FlagKT, mQCVariable.DicSPLCtatus[VariablePLC.FlagKT].ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, VariablePLC.IsReset, mQCVariable.DicSPLCtatus[VariablePLC.IsReset].ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, VariablePLC.OnOFF, mQCVariable.DicSPLCtatus[VariablePLC.OnOFF].ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, VariablePLC.WriteReadyStart, mQCVariable.DicSPLCtatus[VariablePLC.WriteReadyStart].ToString());

                SystemLog.Output(SystemLog.MSG_TYPE.War, "QR MES", mQCVariable.QRMES);
                SystemLog.Output(SystemLog.MSG_TYPE.War, "QR ID", mQCVariable.QRID);

                SystemLog.Output(SystemLog.MSG_TYPE.War, "Output", mQCVariable.ListMQCQty[0].ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, "NG", mQCVariable.ListMQCQty[1].ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, "RW", mQCVariable.ListMQCQty[2].ToString());

                for (int i = 0; i < mQCVariable.ListNG38.Count; i++)
                {
                    SystemLog.Output(SystemLog.MSG_TYPE.War, "NG" + (i + 1).ToString(), mQCVariable.ListNG38[i].ToString());
                }
                for (int i = 0; i < mQCVariable.ListRW38.Count; i++)
                {
                    SystemLog.Output(SystemLog.MSG_TYPE.War, "RW" + (i + 1).ToString(), mQCVariable.ListRW38[i].ToString());
                }

                SystemLog.Output(SystemLog.MSG_TYPE.War, "Output Produced", mQCVariable.ListQtyProduced[0].ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, "NG Produced", mQCVariable.ListQtyProduced [1].ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, "RW Produced", mQCVariable.ListQtyProduced[2].ToString());



                SystemLog.Output(SystemLog.MSG_TYPE.War, "Start read data from PLC", "END");
            }
            catch (Exception ex)
            {
                SystemLog.Output(SystemLog.MSG_TYPE.Err, ex.Source, ex.Message);
            }
        }
        private MQCVariable GetMQCVariableRealtime(string IP, MQCVariable mQCOld)
        {
            MQCVariable qCVariable = new MQCVariable();

            try
            {
                int ConnectionPLC         = -1;
                Sharp.ReadVariablePLC pLC = new Sharp.ReadVariablePLC(IP, 0, 0, out ConnectionPLC);
                qCVariable.Connection = ConnectionPLC;
                if (ConnectionPLC == 0)
                {
                    qCVariable.DicSPLCtatus = pLC.ReadStatusPLCMQC();

                    if (qCVariable.DicSPLCtatus[VariablePLC.FlagKT] == true)
                    {
                        if (qCVariable.DicSPLCtatus[VariablePLC.WriteReadyStart] == false)
                        {
                            string QRMES = pLC.ReadAreaByteToString(181, 100, 100);
                            qCVariable.QRMES = QRMES.Trim();

                            string QRID = pLC.ReadAreaByteToString(181, 0, 100);
                            qCVariable.QRID = QRID.Trim();

                            var ResultValidationQRCode = SubFunction.IsValidationQRCode(qCVariable.QRMES, qCVariable.QRID);
                            pLC.WriteDinttoPLC(ResultValidationQRCode, 181, 206, 2);//write message error to PLC
                            if (ResultValidationQRCode == 0)
                            {
                                SQLUpload.SQLQRUpdate sQLQR      = new SQLQRUpdate();
                                DataTable             dtQRRecord = sQLQR.GetQuanityFromQRMES(qCVariable.QRMES);

                                QRMQC_MES qRMQC_MES = QRSpilittoClass.QRstring2MQCFormat(qCVariable.QRMES);
                                if (dtQRRecord.Rows.Count == 1)
                                {
                                    var OutputQty = Int16.Parse(dtQRRecord.Rows[0]["OutputQty"].ToString());
                                    var NGQty     = Int16.Parse(dtQRRecord.Rows[0]["NGQty"].ToString());
                                    var RWQty     = Int16.Parse(dtQRRecord.Rows[0]["RWQty"].ToString());
                                    var Total     = Int16.Parse(dtQRRecord.Rows[0]["TotalQty"].ToString());
                                    if (qRMQC_MES.quantity > (OutputQty + NGQty + RWQty) && qRMQC_MES.quantity > Total)
                                    {
                                        pLC.WriteMQCProducedQuantitytoPLC(OutputQty, NGQty, RWQty);
                                        pLC.WritebittoPLC(true, 181, 204, 1);//Write FlagKT to PLC
                                    }
                                    else
                                    {
                                        //Write to PLC values to know QR production finished
                                        pLC.WriteDinttoPLC(3, 181, 206, 2);//Completed QR MES
                                    }
                                }
                                else if (dtQRRecord.Rows.Count == 0)
                                {
                                    pLC.WriteMQCProducedQuantitytoPLC(0, 0, 0);
                                    pLC.WritebittoPLC(true, 181, 204, 1);//Write FlagKT to PLC
                                }
                            }
                            else
                            {
                                pLC.WritebittoPLC(false, 181, 204, 1);//Write FlagKT to PLC
                            }

                            //get values from db and write value PLC



                            qCVariable.ListMQCQty = pLC.ReadQuantityMQC();

                            qCVariable.ListQtyProduced = pLC.ReadQuantityMQCProduced();

                            qCVariable.ListNG38 = pLC.ReadAreaIntToListInt(3, 4, 76);
                            qCVariable.ListRW38 = pLC.ReadAreaIntToListInt(4, 4, 76);
                        }
                        else
                        {
                            qCVariable.QRMES      = mQCOld.QRMES;
                            qCVariable.QRID       = mQCOld.QRID;
                            qCVariable.ListMQCQty = pLC.ReadQuantityMQC();
                            if (qCVariable.ListMQCQty[1] > mQCOld.ListMQCQty[1])
                            {
                                qCVariable.ListNG38 = pLC.ReadAreaIntToListInt(3, 4, 76);
                            }
                            else
                            {
                                qCVariable.ListNG38 = mQCOld.ListNG38;
                            }

                            if (qCVariable.ListMQCQty[2] > mQCOld.ListMQCQty[2])
                            {
                                qCVariable.ListRW38 = pLC.ReadAreaIntToListInt(4, 4, 76);
                            }
                            else
                            {
                                qCVariable.ListRW38 = mQCOld.ListRW38;
                            }
                        }
                    }



                    pLC.Diconnect();
                }
            }
            catch (Exception ex)
            {
                SystemLog.Output(SystemLog.MSG_TYPE.Err, "Get MQC variable realtime fail", ex.Message);
            }
            return(qCVariable);
        }
        public void LoadDataMQCStarting()
        {
            try
            {
                stopwatch.Start();
                LoadAdress();
                if (ListMachines.Count > 0)
                {
                    DicMQCVariableIP = new Dictionary <string, MQCVariable>();

                    foreach (var machine in ListMachines)
                    {
                        int                   ConnectionPLC = -1;
                        MQCVariable           mQCVariable   = new MQCVariable();
                        QRMQC_MES             qRMQC_MES     = new QRMQC_MES();
                        QRIDMES               qRIDMES       = new QRIDMES();
                        Sharp.ReadVariablePLC pLC           = new Sharp.ReadVariablePLC(machine.IP, 0, 0, out ConnectionPLC);
                        mQCVariable.Connection = ConnectionPLC;
                        if (ConnectionPLC == 0)
                        {
                            Dictionary <string, bool> DicStatusPLC = new Dictionary <string, bool>();

                            DicStatusPLC             = pLC.ReadStatusPLCMQC();
                            mQCVariable.DicSPLCtatus = DicStatusPLC;

                            if (DicStatusPLC.Count == 4)
                            {
                                if (DicStatusPLC[VariablePLC.FlagKT])
                                {
                                    string QRMES = pLC.ReadAreaByteToString(181, 100, 100);
                                    mQCVariable.QRMES = QRMES;

                                    string QRID = pLC.ReadAreaByteToString(181, 0, 100);
                                    mQCVariable.QRID = QRID;

                                    var ResultValidationQRCode = SubFunction.IsValidationQRCode(QRMES, QRID);
                                    pLC.WriteDinttoPLC(ResultValidationQRCode, 181, 206, 2);//write message error to PLC
                                    if (ResultValidationQRCode == 0)
                                    {
                                        pLC.WritebittoPLC(true, 181, 204, 1);//Write FlagKT to PLC
                                    }
                                    else
                                    {
                                        pLC.WritebittoPLC(false, 181, 204, 1);//Write FlagKT to PLC
                                    }

                                    qRMQC_MES = QRSpilittoClass.QRstring2MQCFormat(mQCVariable.QRMES);
                                    //  get values from db and write value PLC
                                    SQLUpload.SQLQRUpdate sQLQR      = new SQLQRUpdate();
                                    DataTable             dtQRRecord = sQLQR.GetQuanityFromQRMES(mQCVariable.QRMES);

                                    if (dtQRRecord.Rows.Count == 1)
                                    {
                                        var OutputQty = Int16.Parse(dtQRRecord.Rows[0]["OutputQty"].ToString());
                                        var NGQty     = Int16.Parse(dtQRRecord.Rows[0]["NGQty"].ToString());
                                        var RWQty     = Int16.Parse(dtQRRecord.Rows[0]["RWQty"].ToString());
                                        var Total     = Int16.Parse(dtQRRecord.Rows[0]["TotalQty"].ToString());
                                        if (qRMQC_MES.quantity > (OutputQty + NGQty + RWQty) && qRMQC_MES.quantity > Total)
                                        {
                                            pLC.WriteMQCProducedQuantitytoPLC(OutputQty, NGQty, RWQty);
                                        }
                                        else
                                        {
                                            //Write to PLC values to know QR production finished
                                            pLC.WriteDinttoPLC(4, 181, 206, 2);//Completed QR MES
                                        }
                                    }
                                    else if (dtQRRecord.Rows.Count == 0)
                                    {
                                        pLC.WriteMQCProducedQuantitytoPLC(0, 0, 0);
                                    }

                                    List <int> ListMQCQty = pLC.ReadQuantityMQC();
                                    mQCVariable.ListMQCQty = ListMQCQty;
                                    List <int> ListMQCProduced = pLC.ReadQuantityMQCProduced();
                                    mQCVariable.ListQtyProduced = ListMQCProduced;
                                    List <int> ListNG38 = pLC.ReadAreaIntToListInt(3, 4, 76);
                                    mQCVariable.ListNG38 = ListNG38;
                                    List <int> ListRW38 = pLC.ReadAreaIntToListInt(4, 4, 76);
                                    mQCVariable.ListRW38 = ListRW38;
                                }
                            }
                        }

                        DicMQCVariableIP.Add(machine.IP, mQCVariable);
                        pLC.Diconnect();
                        qRMQC_MES = new QRMQC_MES();
                        qRMQC_MES = QRSpilittoClass.QRstring2MQCFormat(mQCVariable.QRMES);
                        qRIDMES   = QRSpilittoClass.QRstring2IDFormat(mQCVariable.QRID);
                        MachineOperation operation = new MachineOperation();
                        operation.IP        = machine.IP;
                        operation.Line      = machine.Line;
                        operation.Lot       = qRMQC_MES.PO;
                        operation.Inspector = qRIDMES.FullName;
                        operation.product   = qRMQC_MES.Product;

                        if (mQCVariable.ListMQCQty.Count == 3)
                        {
                            operation.Output = mQCVariable.ListMQCQty[0];
                            operation.NG     = mQCVariable.ListMQCQty[1];
                            operation.Rework = mQCVariable.ListMQCQty[2];
                            operation.Status = "Updating";
                        }
                        else if (mQCVariable.ListMQCQty.Count == 0)
                        {
                            operation.Output = 0;
                            operation.NG     = 0;
                            operation.Rework = 0;
                            operation.Status = "Waiting";
                        }
                        machineOperations.Add(operation);
                    }
                    datagridMachines.ItemsSource = machineOperations;
                }
            }
            catch (Exception ex)
            {
                SystemLog.Output(SystemLog.MSG_TYPE.Err, "Read PLC fail", ex.Message);
            }
        }