using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Text; using System.Threading; using System.Windows.Forms; using MESClassLibrary.BLL.Log; using grproLib; using InjectionPC; using MESClassLibrary.BLL.Injection; namespace PunchingMistake { public partial class FrmAssemble : Form { private PlcInvoke _plcInvoke = null; private GridppReport Report = new GridppReport(); private string oneBarCode = "", _stationNo = "", stationId = ""; private UniqueBll _uniqueBll = new UniqueBll(); private string _sjBarCode = string.Empty; private string _partNo1 = string.Empty; private string _partNo2 = string.Empty; /// /// 当前设备行数据ID /// private string _lineDataID = string.Empty; public FrmAssemble() { InitializeComponent(); this.StartPosition = FormStartPosition.CenterScreen; string plcRate = ConfigurationManager.AppSettings["刷新频率"] ; int rate = 0; int.TryParse(plcRate, out rate); if (rate == 0) { rate = 200; } _plcInvoke = new PlcInvoke("BBMPT.Assemble", _stationNo); _plcInvoke.ShowLinkInfo = ShowLinkResult; _plcInvoke.ShowMessage = ShowMassege; _plcInvoke.InitDev(rate); } private void FrmAssemble_Load(object sender, EventArgs e) { _stationNo = ConfigurationManager.AppSettings["Station"]; //stationId = ProScreenFunc.GetStationId(stationNo); lbStation.Text = Function2.GetStation(_stationNo); Control.CheckForIllegalCrossThreadCalls = false; Thread t = new Thread(new ThreadStart(TimeGo)); t.Start(); LoadScreen(); } public void LoadScreen() { DataTable dt = Function2.GetProScreen(_stationNo); if (dt != null && dt.Rows.Count > 0) { SetPage(dt); } if(string.IsNullOrEmpty(_lineDataID)) { if (this.Tag != null) { DataTable lineTable = Function2.GetPunchLineInfo(this.Tag.ToString(), _stationNo); if (lineTable.Rows.Count > 0) { _lineDataID = lineTable.Rows[0]["ID"].ToString(); _sjBarCode = lineTable.Rows[0]["SjBarCode"].ToString(); _partNo1 = lineTable.Rows[0]["PartNo1"].ToString(); _partNo2 = lineTable.Rows[0]["PartNo2"].ToString(); ScanAll.barCode1 = lineTable.Rows[0]["barCode1"].ToString(); ScanAll.barCode2 = lineTable.Rows[0]["barCode2"].ToString(); ScanAll.barCode3 = lineTable.Rows[0]["barCode3"].ToString(); ScanAll.barCode4 = lineTable.Rows[0]["barCode4"].ToString(); ScanAll.barCode5 = lineTable.Rows[0]["barCode5"].ToString(); ScanAll.barCode6 = lineTable.Rows[0]["barCode6"].ToString(); _plcInvoke.SetLineID(_lineDataID); } } } LoadScrewTimer_Tick(null, null); } public void SetPage(DataTable dt) { ClearPage(); if (dt.Rows.Count > 0) { this.Tag = dt.Rows[0]["ID"].ToString(); txtBarCode.Tag = dt.Rows[0]["PartNo1"].ToString(); txtBarCode.Text = ""; lbPlan.Text = dt.Rows[0]["OrderNo"].ToString(); lbPart.Text = dt.Rows[0]["PartNo1"].ToString(); lbPartDesc.Text = dt.Rows[0]["Des"].ToString(); lbPlanQty.Text = dt.Rows[0]["OrderCount"].ToString(); lbCompleteQty.Text = dt.Rows[0]["OKCount"].ToString(); lbBadQty.Text = dt.Rows[0]["BadCount"].ToString(); } } void ClearPage() { //lbStation.Text = ""; lbPlan.Text = ""; lbPart.Text = ""; lbPartDesc.Text = ""; } private void btnPlanUp_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(_lineDataID)) { DataTable lineTable = Function2.GetPunchLineInfo(_lineDataID); if (lineTable.Rows.Count > 0) { MessageBox.Show("当前订单未完成,无法切换下一计划."); return; } } LoadPlanScreen(this.Tag?.ToString(), "上一计划"); txtBarCode.Select(); txtBarCode.Focus(); } private void btnPlanDown_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(_lineDataID)) { DataTable lineTable = Function2.GetPunchLineInfo(_lineDataID); if(lineTable.Rows.Count>0) { MessageBox.Show("当前订单未完成,无法切换下一计划."); return; } } LoadPlanScreen(this.Tag?.ToString(), "下一计划"); ClearScanData(true); txtBarCode.Select(); txtBarCode.Focus(); } public void LoadPlanScreen(string id, string plan) { DataTable dt = Function2.GetProScreenPlan(id, _stationNo, plan); if (dt != null && dt.Rows.Count > 0) { SetPage(dt); } else { if (plan == "下一计划") { MessageBox.Show("无下一计划"); txtBarCode.SelectAll(); txtBarCode.Focus(); } else { MessageBox.Show("无上一计划"); txtBarCode.SelectAll(); txtBarCode.Focus(); } } txtBarCode.SelectAll(); txtBarCode.Focus(); } private void txtBarCode_KeyDown(object sender, KeyEventArgs e) { try { if (e.KeyCode != Keys.Enter) { return; } WriteHint("", true); string barcode = txtBarCode.Text?.Trim(); if (string.IsNullOrEmpty(barcode)) { throw new Exception("扫描条码为空!"); } if (string.IsNullOrEmpty(this.Tag?.ToString())) { throw new Exception("当前没有生产计划."); } if (barcode == "88888888") { if (string.IsNullOrEmpty(_sjBarCode)) { throw new Exception("请先扫描半成品塑件条码!"); } else if (string.IsNullOrEmpty(_partNo1)) { throw new Exception("总成零件号为空,无法保存!"); } else if (string.IsNullOrEmpty(_partNo2)) { throw new Exception("半成品零件号为空,无法保存!"); } if(ScrewCompleteVerify() == false) { throw new Exception("螺钉铆钉未打够数量,无法保存!"); } SaveScanRecord(_sjBarCode, _partNo1, _partNo2); Function2. DeleteLineData(_lineDataID); ClearScanData(true); SentToPlc(true, _lineDataID); ShowHint(true); WriteHint("保存成功", true); return; } else { _sjBarCode = txtBarCode.Text?.Trim(); ScanBarCode(); } } catch(Exception ex) { //ClearScanData(false); ShowHint(false); WriteHint(ex.Message,false); } //finally //{ // txtBarCode.Focus(); // txtBarCode.SelectAll(); //} } void ClearScanData(bool isAllClear) { _sjBarCode = string.Empty; _partNo1 = string.Empty; _partNo2 = string.Empty; ScanAll.barCode1 = string.Empty; ScanAll.barCode2 = string.Empty; ScanAll.barCode3 = string.Empty; ScanAll.barCode4 = string.Empty; ScanAll.barCode5 = string.Empty; ScanAll.barCode6 = string.Empty; if(isAllClear == true) { lbScrewL.Text = "0"; lbScrewR.Text = "0"; lbScrewLAll.Text = "0"; lbScrewRAll.Text = "0"; lbRivetL.Text = "0"; lbRivetR.Text = "0"; lbRivetLAll.Text = "0"; lbRivetRAll.Text = "0"; lbIrls.Text = "0"; lbIrlsAll.Text = "0"; } } private void ScanBarCode() { bool verifyPreProcess = Function2.GetVerifyPreProcess(); if(verifyPreProcess == true) { bool isPass = Function2.GetInspectResultCheck(_sjBarCode); if(isPass == false) { throw new Exception($"扫描条码[{_sjBarCode}],未通过上线前质检工序,无法使用."); } } DataTable productTable = PartScanVerify(); _partNo2 = productTable.Rows[0]["PartNo"].ToString(); _partNo1 = Function2.GetAssemblePartCode(_partNo2, _stationNo); if (lbPart.Text?.ToLower() != _partNo1?.ToLower()) { throw new Exception($"扫描半成品码在BOM中对应的总成零件号[{_partNo1}]与当前计划的零件号[{lbPart.Text}]不匹配."); } DataTable bomTable = Function2.GetBomTable(_partNo1, _stationNo); //需要校验的关键件 List list = bomTable.AsEnumerable().Where(p => p.Field("IsChecked") == true).ToList(); //螺钉铆钉数量BOM集合 List list2 = bomTable.AsEnumerable().Where(p => p.Field("PartDesc2").Contains("螺钉") || p.Field("PartDesc2").Contains("铆钉") || p.Field("PartDesc2").Contains("虹膜")).ToList(); if (list.Count > 0) { //需要校验关键件 FrmProScreen3 frm = new FrmProScreen3(list); frm.ScanCompleted += (rst) => { if(rst == false) { ClearScanData(true); WriteHint("扫描关键件已取消", false); return; } if (list2.Count > 0) { //接收到螺钉信号后再保存 SendScrewRivet(bomTable, list2, _sjBarCode); } else { _lineDataID = Function2.SaveLineInfo(_sjBarCode, this.Tag?.ToString(), _stationNo, _partNo1, _partNo2, 0, 0, 0, 0, 0); WriteHint("验证成功,请扫完成码保存", true); //SaveScanRecord(barCode, partNo1, partNo2); } }; frm.ShowDialog(); } else { if (list2.Count > 0) { //接收到螺钉信号后再保存 SendScrewRivet(bomTable, list2, _sjBarCode); } else { _lineDataID = Function2.SaveLineInfo(_sjBarCode, this.Tag?.ToString(), _stationNo, _partNo1, _partNo2, 0, 0, 0, 0, 0); WriteHint("验证成功,请扫完成码保存", true); //SaveScanRecord(barCode, partNo1, partNo2); } } } /// /// 扫描半成品的校验 /// /// DataTable PartScanVerify() { if (_sjBarCode.Length != 20) { throw new Exception("扫描条码格式不正确,请扫描20位塑件条码!"); } DataTable stockInTable = Function2.SearchStockIn(_sjBarCode); if (stockInTable.Rows.Count == 0) { throw new Exception("扫描半成品码没有喷涂完工记录,无法使用."); } DataTable punchTable = Function2.SearchPunchCodeRecord(_sjBarCode); if (punchTable.Rows.Count > 0) { if (punchTable.Rows[0]["Flag"].ToString() == "0" || punchTable.Rows[0]["Flag"].ToString() == "1") { throw new Exception("扫描半成品码已存在装配记录,无法使用."); } } DataTable productTable = Function2.GetProductInfo(_sjBarCode.Substring(0, 10)); if (productTable.Rows.Count == 0) { throw new Exception($"扫描半成品码的存货代码[{_sjBarCode.Substring(0, 10)}]在零件表找不对对应零件号,请检查系统维护."); } return productTable; } bool ScrewCompleteVerify() { int screwL = 0, screwR = 0, rivetL = 0, rivetR = 0, irls = 0; int screwLAll = 0, screwRAll = 0, rivetLAll = 0, rivetRAll = 0, irlsAll = 0; int.TryParse(lbScrewL.Text, out screwL); int.TryParse(lbScrewR.Text, out screwR); int.TryParse(lbRivetL.Text, out rivetL); int.TryParse(lbRivetR.Text, out rivetR); int.TryParse(lbIrls.Text, out irls); int.TryParse(lbScrewLAll.Text, out screwLAll); int.TryParse(lbScrewRAll.Text, out screwRAll); int.TryParse(lbRivetLAll.Text, out rivetLAll); int.TryParse(lbRivetRAll.Text, out rivetRAll); int.TryParse(lbIrlsAll.Text, out irlsAll); if (screwL < screwLAll) return false; if (screwR < screwRAll) return false; if (rivetL < rivetLAll) return false; if (rivetR < rivetRAll) return false; if (irls < irlsAll) return false; return true; } void SendScrewRivet(DataTable bomTable, List list2 ,string barCode) { if (list2.Count > 0) { string sjBarCode = barCode; //螺钉数量 int leftQty1 = 0; int rightQty1 = 0; //铆钉数量 int leftQty2 = 0; int rightQty2 = 0; //虹膜 int leftQty3 = 0; foreach (DataRow dr in list2) { string desc = dr["PartDesc2"].ToString(); if (desc.Contains("螺钉")) { leftQty1 = int.Parse(dr["LeftCount"].ToString()); rightQty1 = int.Parse(dr["RightCount"].ToString()); lbScrewLAll.Text = leftQty1.ToString(); lbScrewRAll.Text = rightQty1.ToString(); } if (desc.Contains("铆钉")) { leftQty2 = int.Parse(dr["LeftCount"].ToString()); rightQty2 = int.Parse(dr["RightCount"].ToString()); lbRivetLAll.Text = leftQty2.ToString(); lbRivetRAll.Text = rightQty2.ToString(); } if (desc.Contains("虹膜")) { leftQty3 = int.Parse(dr["LeftCount"].ToString()); lbIrlsAll.Text = leftQty3.ToString(); } } _lineDataID = Function2.SaveLineInfo(sjBarCode, this.Tag?.ToString(),_stationNo,_partNo1,_partNo2, leftQty2, rightQty2, leftQty1, rightQty1, leftQty3); SentToPlc(false,_lineDataID); } } private void SentToPlc(bool isInit, string lineID) { _plcInvoke.SendToPLC(isInit, lineID); } private void SaveScanRecord(string barcode,string partNo1,string partNo2) { //保存装配记录 //更新计划完成数量 // string serailNo = "0001"; string assembleBarCode = _uniqueBll.GetPunchSerialNo(partNo1, 4,ref serailNo); int res = Function2.SavePartAndProduct_New(barcode, partNo1, partNo2, assembleBarCode, lbPlan.Text.Trim(), lbClass.Text, this.Tag?.ToString(), serailNo, ScanAll.barCode1, ScanAll.barCode2, ScanAll.barCode3, ScanAll.barCode4, ScanAll.barCode5, ScanAll.barCode6, _stationNo, ConfigurationManager.AppSettings["StationDescription"]); Function2.SaveZcCode(barcode, partNo1); Function2.AddCompleteCount(this.Tag?.ToString(),"合格"); PrintAssembleBarCode(assembleBarCode, partNo1, lbPartDesc.Text, serailNo); if (Function2.CheckPlanCompleted(this.Tag.ToString().Trim())) { LoadScreen(); } else { lbCompleteQty.Text = (Tools.NumericParse.StringToInt(lbCompleteQty.Text.Trim()) + 1).ToString(); } } private void PrintAssembleBarCode(string BarCode, string PartNo, string PartName, string SerialNo ) { Printer.PrintPunchAssembleBarCode(BarCode, PartNo, PartName, SerialNo); } void WriteHint(string txt,bool isOk) { lbHint.ForeColor = Color.LimeGreen; if (!isOk) { lbHint.ForeColor = Color.Red; } lbHint.Text = txt; } private void label10_DoubleClick(object sender, EventArgs e) { this.Close(); } private void TimeGo() { System.Timers.Timer timer = new System.Timers.Timer(); timer.Interval = 10; timer.Enabled = true; timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Tick); } private void timer_Tick(object sender, EventArgs e) { try { lbPartDesc.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); Thread.Sleep(500); } catch (Exception ex) { } } void ShowLinkResult(bool isSuccess, bool isWriteLog) { if (isSuccess) { txtLinkState.Text = "连接成功"; if (isWriteLog) WriteLog("连接成功"); txtLinkState.BackColor = Color.Green; // ShowScanResult(false); } else { txtLinkState.Text = "连接失败"; if (isWriteLog) WriteLog("连接失败"); txtLinkState.BackColor = Color.Red; //ShowScanResult(false); } } private void btnPlanSearch_Click(object sender, EventArgs e) { FrmPlanSeach frm = new FrmPlanSeach(this.Tag.ToString()); frm.ShowDialog(); } private void btnPlanFinish_Click(object sender, EventArgs e) { if (MessageBox.Show("计划结束后将不能再进行切换生产,确认将当前计划设置为结束状态?", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel) return; string planID = this.Tag?.ToString(); if (string.IsNullOrEmpty(planID)) { MessageBox.Show("计划信息错误,当前没有正在执行的计划."); return; } else { if (!string.IsNullOrEmpty(_lineDataID)) { DataTable lineTable = Function2.GetPunchLineInfo(_lineDataID); if (lineTable.Rows.Count > 0) { MessageBox.Show("当前订单未完成,无法结束当前计划."); return; } } Function2.UpdatePlanState("3", planID); ClearPage(); ClearScanData(true); MessageBox.Show("设置成功"); LoadScreen(); } } private void showHintTimer_Tick(object sender, EventArgs e) { showHintTimer.Enabled = false; lbDisplay.Visible = false; } /// /// 螺钉铆钉刷新 /// /// /// private void LoadScrewTimer_Tick(object sender, EventArgs e) { if (!string.IsNullOrEmpty(_lineDataID)) { DataTable dt = Function2.GetPunchLineInfo(_lineDataID); if (dt.Rows.Count > 0) { DataRow dr = dt.Rows[0]; lbScrewL.Text = dr["LeftScrew1"].ToString(); lbScrewR.Text = dr["RightScrew1"].ToString(); lbRivetL.Text = dr["LeftRivet1"].ToString(); lbRivetR.Text = dr["RightRivet1"].ToString(); lbIrls.Text = dr["IrIs"].ToString(); lbScrewLAll.Text = dr["LeftScrew1All"].ToString(); lbScrewRAll.Text = dr["RightScrew1All"].ToString(); lbRivetLAll.Text = dr["LeftRivet1All"].ToString(); lbRivetRAll.Text = dr["RightRivet1All"].ToString(); lbIrlsAll.Text = dr["IrIsAll"].ToString(); } } } void ShowMassege(string txt, bool isOK) { if (isOK) { lbScanState.ForeColor = Color.Green; } else { lbScanState.ForeColor = Color.Red; } lbScanState.Text = txt; } public void WriteLog(string log) { try { //this.lstState.Invoke(onMessage, log); } catch { } } private void txtBoxFocusTimer_Tick(object sender, EventArgs e) { lbClass.Text = Function3.GetWorkClass(); txtBarCode.Focus(); txtBarCode.SelectAll(); } /// /// 显示OK和NG 以及提示音 /// /// private void ShowHint(bool isOk) { if (isOk) { lbDisplay.Text = "OK"; lbDisplay.ForeColor = Color.Lime; } else { lbDisplay.Text = "NG"; lbDisplay.ForeColor = Color.Red; } Play(isOk); lbDisplay.Visible = true; showHintTimer.Enabled = true; } void Play(bool isOK) { if (isOK) { PlaySound.PlaySounds.Play(AppDomain.CurrentDomain.BaseDirectory + "Images\\正确.wav"); } else { PlaySound.PlaySounds.Play(AppDomain.CurrentDomain.BaseDirectory + "Images\\错误.wav"); } } } }