public bool InsertRowMQVariables(Model.MQCVariable mQCVariable, string data, string line, string item) { try { string datetimeserno = DateTime.Now.ToString("yyyyMMdd_HHmmss"); string serno = mQCVariable.QRMES; QRMQC_MES qRMQC_MES = QRSpilittoClass.QRstring2MQCFormat(mQCVariable.QRMES); QRIDMES qRIDMES = QRSpilittoClass.QRstring2IDFormat(mQCVariable.QRID); string site = "B01"; string factory = "TECHLINK"; string process = "MQC"; string status = ""; string date_ = DateTime.Now.ToString("yyyy-MM-dd"); string time_ = DateTime.Now.ToString("HH:mm:ss"); string sqlQuerry = ""; string Remark = ""; if (item == "OUTPUT") { Remark = "OP"; } else if (item.Contains("NG")) { Remark = "NG"; } else if (item.Contains("RW")) { Remark = "RW"; } else if (item.Contains("Reset")) { Remark = "Reset"; } // string model = GetModelFromLot(lot,setting); string model = qRMQC_MES.Product;// lay model tren server, neu ko lay dc thi lay o local sqlQuerry += "insert into t_mqc_realtime (serno, lot, model, site, factory, line, process,item,inspectdate,inspecttime, data, judge,status,remark,inspector,TL01 ) values( '"; sqlQuerry += serno + "', '" + qRMQC_MES.PO + "', '" + model + "', '" + site + "', '" + factory + "', '" + line + "', '" + process + "', '" + item + "', '" + date_ + "', '" + time_ + "', '" + data + "', '" + "" + "', '" + status + "', '" + Remark + "' , '" + qRIDMES.ID + "', '" + qRIDMES.FullName + "' )"; MysqlMES mysqlMES = new MysqlMES(); return(mysqlMES.sqlExecuteNonQuery(sqlQuerry, false)); } catch (Exception ex) { SystemLog.Output(SystemLog.MSG_TYPE.Err, "InsertToMQC_Realtime(string lot, string line, string item, string data, string Remark, int judge)", ex.Message); return(false); } }
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); } }
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); }
void bg_DoWork(object sender, DoWorkEventArgs e) { // does a job like writing to serial communication, webservices etc var worker = sender as BackgroundWorker; stopwatch = new Stopwatch(); try { stopwatch.Start(); // if (ListMachines != null) { machineOperations = new List <MachineOperation>(); //MachineItem machine = new MachineItem(); //machine.IP = "172.16.1.145"; //machine.Line = "L03"; LoadAdress();//load list PLC machine foreach (var machine in ListMachines) { try { // machine.IP = MQCVariable mQCOld = new MQCVariable(); if (DicMQCVariableIP.ContainsKey(machine.IP)) { mQCOld = DicMQCVariableIP[machine.IP]; } MQCVariable mQCPLC = GetMQCVariableRealtime(machine.IP, mQCOld); MQCVariable mQCChanged = new MQCVariable(); if (mQCPLC.Connection == 0) { bool isChanged = false; List <string> listChanged = new List <string>(); // if (mQCPLC.DicSPLCtatus[VariablePLC.FlagKT] == true) { mQCChanged = GetMQCVariableDisCrepancy(mQCPLC, mQCOld, ref isChanged, ref listChanged); if (DicMQCVariableIP.ContainsKey(machine.IP)) { DicMQCVariableIP[machine.IP] = mQCPLC; } else { DicMQCVariableIP.Add(machine.IP, mQCPLC); } if (listChanged.Contains("Reset")) { //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; //operation.Output = mQCPLC.ListMQCQty[0]; //operation.NG = mQCPLC.ListMQCQty[1]; //operation.Rework = mQCPLC.ListMQCQty[2]; //operation.Status = "Reset"; SQLUpload.SQLQRUpdate sQLQRUpdate = new SQLQRUpdate(); sQLQRUpdate.UpdateOrInsertQRRecordTable(mQCOld, machine.Line); SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "QR MES Reset", mQCOld.QRMES); SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "QR Inspector", mQCOld.QRID); SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "Ouput Quantity", mQCOld.ListMQCQty[0].ToString()); SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "NG Quantity", mQCOld.ListMQCQty[1].ToString()); SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "RW Quantity", mQCOld.ListMQCQty[2].ToString()); return; } UploadPLCtoDatabase(mQCChanged, listChanged, machine.Line); QRMQC_MES qRMQC_MES = QRSpilittoClass.QRstring2MQCFormat(mQCPLC.QRMES); QRIDMES qRIDMES = QRSpilittoClass.QRstring2IDFormat(mQCPLC.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 (mQCPLC.ListMQCQty.Count == 3) { operation.Output = mQCPLC.ListMQCQty[0]; operation.NG = mQCPLC.ListMQCQty[1]; operation.Rework = mQCPLC.ListMQCQty[2]; operation.Status = "Updating"; } else if (mQCPLC.ListMQCQty.Count == 0) { operation.Output = 0; operation.NG = 0; operation.Rework = 0; operation.Status = "Waiting"; } //if (listChanged.Contains("Reset")) //{ // operation.Status = "Reset"; // //SQLUpload.SQLQRUpdate sQLQRUpdate = new SQLQRUpdate(); // //sQLQRUpdate.UpdateOrInsertQRRecordTable(mQCPLC, machine.Line); // SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "QR MES Reset", mQCPLC.QRMES); // SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "QR Inspector", mQCPLC.QRID); // SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "Ouput Quantity", mQCPLC.ListMQCQty[0].ToString()); // SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "NG Quantity", mQCPLC.ListMQCQty[1].ToString()); // SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "RW Quantity", mQCPLC.ListMQCQty[2].ToString()); //} //else //{ // operation.Status = "Updating"; //} machineOperations.Add(operation); } } else { MachineOperation operation = new MachineOperation(); operation.IP = machine.IP; operation.Line = machine.Line; Sharp7.S7Client client = new Sharp7.S7Client(); operation.Status = client.ErrorText(mQCPLC.Connection); machineOperations.Add(operation); } } catch (Exception ex) { SystemLog.Output(SystemLog.MSG_TYPE.Err, "PLC IP : " + machine.IP, ex.Message); } } } stopwatch.Stop(); } catch (Exception ex) { SystemLog.Output(SystemLog.MSG_TYPE.Err, "Read PLC fail", ex.Message); } System.Threading.Thread.Sleep(100); }