using Microsoft.Win32; using System; using System.Data; using System.IO; using System.IO.Ports; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Windows.Forms; using UAS_MES.DataOperate; using UAS_MES.Entity; using UAS_MES.PublicMethod; using System.Drawing; using LabelManager2; using UAS_MES.PublicForm; using DevComponents.DotNetBar.Controls; namespace UAS_MES.Make { public partial class Make_ColorBoxWeigh : Form { AutoSizeFormClass asc = new AutoSizeFormClass(); ApplicationClass lbl; Document doc; DataHelper dh; DataTable dt; LogStringBuilder sql = new LogStringBuilder(); //启用线程进行称重数据读取 Thread thread; SerialPort serialPort1 = new SerialPort(); //称量的标准重量 Double Weight; //最大重量 Double MaxWeight; //最小重量 Double MinWeight; //是否通过串口获取数据 bool GetData = true; string LastSncode; bool WeightChange = false; Regex re = new Regex("\\d+.\\d+\\w+"); string ErrorMessage; Thread InitPrint; System.DateTime[] indate; private bool AutoCut; public Make_ColorBoxWeigh() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; } private void Make_ColorBoxWeigh_Load(object sender, EventArgs e) { asc.controllInitializeSize(this); ComList.Text = BaseUtil.GetCacheData("PortName").ToString(); BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString(); InitPrint = new Thread(InPrint); SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序"); BaseUtil.SetFormCenter(stw); stw.ShowDialog(); StartWeight.PerformClick(); dh = SystemInf.dh; StepCount.StepCode = User.CurrentStepCode; StepCount.Source = User.UserSourceCode; StepCount.LineCode = User.UserLineCode; StepCount.Dh = dh; StepCount.Start(); } private void InPrint() { try { lbl = new ApplicationClass(); BaseUtil.WriteLbl(); } catch (Exception ex) { OperateResult.AppendText("未正确安装CodeSoft软件\n", Color.Red); } } private void sncode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { if (LogicHandler.CheckStepAttribute(Tag.ToString(), User.UserSourceCode, out ErrorMessage)) { sql.Clear(); sql.Append("select ms_makecode,ma_qty,ma_salecode,pr_machinetype,pr_colorboxunit,pr_code,pr_colorboxgw,"); sql.Append("pr_colorboxunit,pr_colorboxmaxw,pr_colorboxminw,nvl(PR_CHECKCOLORBOXW,'0') PR_CHECKCOLORBOXW,nvl(pr_sendchecktype,'LineCode')"); sql.Append("pr_sendchecktype from makeserial left join make on ms_makecode=ma_code left join product on "); sql.Append("ms_prodcode=pr_code where ms_sncode='" + sncode.Text + "' order by ms_id desc"); dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); if (dt.Rows.Count > 0) { //重量的临时变量 pr_code.Text = dt.Rows[0]["pr_code"].ToString(); string _weight = dt.Rows[0]["pr_colorboxgw"].ToString(); string _maxweight = dt.Rows[0]["pr_colorboxmaxw"].ToString(); string _minweight = dt.Rows[0]["pr_colorboxminw"].ToString(); string pr_colorunit = dt.Rows[0]["pr_colorboxunit"].ToString(); string ma_salecode_text = dt.Rows[0]["ma_salecode"].ToString(); string PR_CHECKCOLORBOXW = dt.Rows[0]["PR_CHECKCOLORBOXW"].ToString(); //赋值重量单位 Weight = double.Parse(_weight == "" ? "0" : _weight); MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight); MinWeight = double.Parse(_minweight == "" ? "0" : _minweight); if (locksalecode.Checked && ma_salecode.Text != "" && ma_salecode.Text != ma_salecode_text) { OperateResult.AppendText(">>序列号" + sncode.Text + "所属订单号" + ma_salecode_text + "与界面订单号" + ma_salecode.Text + "不同,无法采集\n", Color.Red, sncode); return; } ma_salecode.Text= dt.Rows[0]["ma_salecode"].ToString(); BaseUtil.SetFormValue(this.Controls, dt); if (Weight - MinWeight == MaxWeight - Weight) pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_colorboxunit"].ToString(); else pr_colorboxgw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_colorboxunit"].ToString(); string oMakeCode = ""; string oMsID = ""; if (LogicHandler.CheckStepSNAndMacode(ms_makecode.Text, User.UserSourceCode, sncode.Text, User.UserCode, out oMakeCode, out oMsID, out ErrorMessage)) { string mscode = dh.getFieldDataByCondition("makeserial LEFT JOIN craft ON ms_craftcode = cr_code and ms_prodcode = cr_prodcode LEFT JOIN craftdetail ON cr_id = cd_crid LEFT JOIN STEP ON CD_NEXTSTEPCODE = ST_CODE", "ms_code", "ms_id = '" + oMsID + "' AND CD_STEPCODE = '" + User.CurrentStepCode + "' AND ST_IFOQC = -1").ToString(); if (mscode != "" && (dh.CheckExist("makeserial", "ms_id = '" + oMsID + "' and NVL(MS_BADTIMES,0)>0 ") || dh.CheckExist("makeserial", "ms_sncode = '" + mscode + "' and NVL(MS_BADTIMES,0)>0 "))) { OperateResult.AppendText(">>" + sncode.Text + " 序列号即将送检,此序列号经过维修,提示\n", Color.Purple); } string nextstepcode = dh.getFieldDataByCondition("makeserial", "ms_nextstepcode", "ms_id='" + oMsID + "'").ToString(); if (nextstepcode != User.CurrentStepCode) { OperateResult.AppendText("<<序列号:" + sncode.Text + "下一工序是" + nextstepcode + ",不是当前岗位的工序\n", Color.Red, sncode); return; } //if (dh.CheckExist("makeserial LEFT JOIN craft ON ms_craftcode = cr_code and ms_prodcode = cr_prodcode LEFT JOIN craftdetail ON cr_id = cd_crid LEFT JOIN STEP ON CD_NEXTSTEPCODE = ST_CODE ", "ms_id = '" + oMsID + "' AND CD_STEPCODE = '" + User.CurrentStepCode + "' AND NVL(MS_BADTIMES,0)>0 AND ST_IFOQC = -1 ")) //{ // OperateResult.AppendText(">>" + sncode.Text + " 序列号即将送检,此序列号经过维修,提示\n", Color.Purple); //} //如果未打开串口设置为0 //if (pr_colorboxunit.Text == "kg" && !WeightChange) //{ // WeightChange = true; //} //重量信息若需要检测则必须符合检测要求才更新 double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text.Replace("kg", "").Trim()); if (PR_CHECKCOLORBOXW != "0"&&!checkBox1.Checked) { if (MinWeight == 0 || MaxWeight == 0) { OperateResult.AppendText(">>产品" + pr_code.Text + "未维护彩盒重量范围\n", Color.Red); return; } if ((ActualWeight >= MinWeight) && (ActualWeight <= MaxWeight)) { OperateResult.AppendText(">>彩盒" + sncode.Text + "重量检测检测合格\n", Color.Green); LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ms_makecode.Text, User.UserLineCode, User.UserSourceCode, "彩盒称重:" + weight.Text, "称量合格", sncode.Text, ""); LogicHandler.RecordProdWeight(sncode.Text, "SN", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName); dh.ExecuteSql("update makeserial set ms_grossw='" + ActualWeight + "' where ms_id='" + oMsID + "'", "update"); } else { OperateResult.AppendText(">>彩盒" + sncode.Text + "重量检测未通过\n", Color.Red); LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ms_makecode.Text, User.UserLineCode, User.UserSourceCode, "彩盒称重:" + weight.Text, "称量不合格", sncode.Text, ""); LogicHandler.RecordProdWeight(sncode.Text, "SN", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName); return; } } else { LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ms_makecode.Text, User.UserLineCode, User.UserSourceCode, "彩盒称重:" + weight.Text, "称量完成", sncode.Text, ""); LogicHandler.RecordProdWeight(sncode.Text, "SN", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName); dh.ExecuteSql("update makeserial set ms_grossw='" + ActualWeight + "' where ms_id='" + oMsID + "'", "update"); } ListViewItem lsi = new ListViewItem(); lsi.SubItems.Add(sncode.Text); lsi.SubItems.Add(ActualWeight.ToString()); lsi.SubItems.Add(System.DateTime.Now.ToString()); WeighRecord.Items.Add(lsi); OperateResult.AppendText(">>彩盒" + sncode.Text + "称重完成\n", Color.Green); if (checkBox1.Checked) { checkBox1.Checked = false; } //显示最近的三个称量记录 if (LogicHandler.SetStepResult(ms_makecode.Text, User.UserSourceCode, sncode.Text, "彩盒称量", "称量合格", User.UserCode, out ErrorMessage)) { //提示正确返回时传递的信息 if (ErrorMessage.Contains("AFTERSUCCESS")) OperateResult.AppendText(">>" + ErrorMessage + "\n"); LoadCheckQTY(); //记录送检前的最后一个序列号 LastSncode = sncode.Text; if (AutoPrint.Checked) { OperateResult.AppendText(">>打印条码号" + sncode.Text + "\n", Color.Green); Confirm.PerformClick(); } } else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red); if (CollectRemark.Checked && Remark.Text != "") { LogicHandler.CollectRemarkInf(sncode.Text, "", "", oMakeCode, pr_code.Text, Remark.Text); } LoadCollectedNum(); sncode.Clear(); } else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, sncode); } else OperateResult.AppendText(">>序列号不存在\n", Color.Red, sncode); } else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, sncode); } } private void getSerialData() { if (serialPort1.IsOpen) { if (!SystemInf.OpenPort.Contains(serialPort1.PortName)) { SystemInf.OpenPort.Add(serialPort1.PortName); try { while (GetData) { try { weight.Text = re.Match(serialPort1.ReadLine().Trim()).Groups[0].Value; } catch (Exception) { GetData = false; } } } catch (IOException ex) { MessageBox.Show(ex.Message); } } else OperateResult.AppendText(">>端口已被占用,请关闭其他窗口\n", Color.Red); } } private void 彩盒称重_SizeChanged(object sender, EventArgs e) { weight.Location = new Point(weight_label.Location.X + weight_label.Size.Width - 10, weight_label.Location.Y - 20); asc.controlAutoSize(this); } protected override void OnVisibleChanged(EventArgs e) { base.OnVisibleChanged(e); if (!IsHandleCreated) { this.Close(); } } private void Clean_Click(object sender, EventArgs e) { OperateResult.Clear(); } //关闭窗口的时候停止进程读取串口数据 private void 彩盒称重_FormClosing(object sender, FormClosingEventArgs e) { BaseUtil.ClosePrint(lbl); StopWeight.PerformClick(); InitPrint.Abort(); if (serialPort1.IsOpen) { GetData = false; serialPort1.Close(); SystemInf.OpenPort.Remove(serialPort1.PortName); thread.Interrupt(); } //thread.Abort(); } private void Confirm_Click(object sender, EventArgs e) { if (PrintLabel.Items.Count != 0) { //doc = lbl.Documents.Open(PrintLabel.Text); string oErrorMessage; if (!Print.CodeSoft(Tag.ToString(), ref lbl, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), PrinterList.Text, sncode.Text, int.Parse(PrintNum.Text), ms_makecode.Text, pr_code.Text, "彩盒标", "0", out oErrorMessage)) { OperateResult.AppendText(oErrorMessage + "\n", Color.Red); } } else { OperateResult.AppendText(">>产品" + pr_code.Text + "未维护打印标签\n", Color.Red); } } private void StartWeight_Click(object sender, EventArgs e) { thread = new Thread(getSerialData); try { serialPort1.PortName = ComList.Text; serialPort1.BaudRate = int.Parse(BaudRate.Text); serialPort1.Open(); GetData = true; thread.Start(); } catch (Exception mes) { if (BaudRate.Text == "" || ComList.Text == "") OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red); else OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red); } } private void StopWeight_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) { GetData = false; serialPort1.Close(); SystemInf.OpenPort.Remove(serialPort1.PortName); thread.Abort(); } } private void pr_code_TextChanged(object sender, EventArgs e) { LoadCheckQTY(); DataTable _dt = (DataTable)dh.ExecuteSql("select la_id,la_url,la_isdefault from label where la_prodcode='" + pr_code.Text + "' and la_templatetype='彩盒标' and la_statuscode='AUDITED' order by la_isdefault", "select"); PrintLabel.DataSource = _dt; PrintLabel.DisplayMember = "la_url"; PrintLabel.ValueMember = "la_id"; } private void SendCheck_Click(object sender, EventArgs e) { sql.Clear(); sql.Append("update oqcbatch set ob_status='UNCHECK',ob_breakingdate=sysdate where ob_checkno ='" + ob_checkno.Text + "'"); dh.ExecuteSql(sql.GetString(), "select"); ob_nowcheckqty.Text = ""; ob_batchqty.Text = ""; ob_nowcheckqty.ForeColor = Color.Black; SendCheck.Enabled = false; OperateResult.AppendText(">>批次" + ob_checkno.Text + "送检成功\n", Color.Green); LogicHandler.InsertMakeProcess(LastSncode, ms_makecode.Text, User.UserSourceCode, "手动送检", "手动送检成功", User.UserCode); //记录操作日志 LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ms_makecode.Text, User.UserLineCode, User.UserSourceCode, "手动送检", "手动送检成功", "", ob_checkno.Text); ob_checkno.Text = ""; } private void LoadCheckQTY() { sql.Clear(); string condition = ""; int nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text); int batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text); if (nowcheckqty + 1 == batchqty && AutoCut) { condition = "and ob_status='UNCHECK' and ob_checkno='" + ob_checkno.Text + "'"; } else { condition = "and ob_status='ENTERING' "; } if (pr_sendchecktype.Text == "SaleCode") { condition += " and ob_salecode='" + ma_salecode.Text + "'"; } else if (pr_sendchecktype.Text == "MakeCode") { condition += " and ob_makecode='" + ms_makecode.Text + "'"; } sql.Append("select ob_batchqty,ob_nowcheckqty,ob_checkno from oqcbatch where "); sql.Append("ob_linecode='" + User.UserLineCode + "' and ob_prodcode='" + pr_code.Text + "' and "); sql.Append("ob_stepcode='" + User.CurrentStepCode + "' " + condition); DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); if (dt.Rows.Count > 0) { ob_batchqty.Text = dt.Rows[0]["ob_batchqty"].ToString(); ob_nowcheckqty.Text = dt.Rows[0]["ob_nowcheckqty"].ToString(); ob_checkno.Text = dt.Rows[0]["ob_checkno"].ToString(); nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text); batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text); if (nowcheckqty == batchqty) { ob_nowcheckqty.ForeColor = Color.Red; OperateResult.AppendText(">>当前采集数量已达到送检数量\n", Color.Red); if (AutoCut) { OperateResult.AppendText(">>批次" + ob_checkno.Text + "自动断批\n", Color.Green); } } } else { ob_batchqty.Text = ""; ob_nowcheckqty.Text = ""; ob_checkno.Text = ""; SendCheck.Enabled = false; } if (ob_batchqty.Text != "") { SendCheck.Enabled = true; } else { SendCheck.Enabled = false; } } private void ob_checkno_TextChanged(object sender, EventArgs e) { if (ob_checkno.Text != "") { string Cut = dh.getFieldDataByCondition("product left join oqcbatch on ob_prodcode=pr_code", "pr_ifautocutcheckno", "ob_checkno='" + ob_checkno.Text + "'").ToString(); if (Cut == "" || Cut == "0") AutoCut = false; else AutoCut = true; SendCheck.Enabled = true; } } private void PrintLabel_SelectedValueChanged(object sender, EventArgs e) { if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView") { string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString(); PrintNum.Text = (PrintNums == "" ? "1" : PrintNums); } } private void RefreshWeigh_Click(object sender, EventArgs e) { sql.Clear(); sql.Append("select pr_colorboxgw,pr_colorboxmaxw,pr_colorboxminw,pr_colorboxunit from product where pr_code='" + pr_code.Text + "'"); DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); if (dt.Rows.Count > 0) { //重量的临时变量 string _weight = dt.Rows[0]["pr_colorboxgw"].ToString(); string _maxweight = dt.Rows[0]["pr_colorboxmaxw"].ToString(); string _minweight = dt.Rows[0]["pr_colorboxminw"].ToString(); //赋值重量单位 Weight = double.Parse(_weight == "" ? "0" : _weight); MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight); MinWeight = double.Parse(_minweight == "" ? "0" : _minweight); if (Weight - MinWeight == MaxWeight - Weight) pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_colorboxunit"].ToString(); else pr_colorboxgw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_colorboxunit"].ToString(); } } private void LoadCollectedNum() { dt = (DataTable)dh.ExecuteSql("select mcd_inqty,ma_qty-mcd_inqty mcd_remainqty from make left join makecraftdetail on mcd_macode=ma_code where ma_code='" + ms_makecode.Text + "' and mcd_stepcode='" + User.CurrentStepCode + "'", "select"); BaseUtil.SetFormValue(Controls, dt); } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked) checkBox1.Enabled = true; else checkBox1.Enabled = false; } private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { string password = dh.getFieldDataByCondition("employee", "em_password", "em_code = 'FD001'").ToString(); if (textBox1.Text.Equals(password)) { checkBox1.Checked = true; textBox1.Clear(); sncode.Focus(); } else { OperateResult.AppendText(">>密码错误\n", Color.Red,textBox1); } } } } }