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; namespace UAS_MES.Make { public partial class Make_ColorBoxWeigh : Form { AutoSizeFormClass asc = new AutoSizeFormClass(); ApplicationClass lbl; DataHelper dh; DataTable dt; LogStringBuilder sql = new LogStringBuilder(); //启用线程进行称重数据读取 Thread thread; SerialPort serialPort1 = new SerialPort(); //称量的标准重量 int Weight; //最大重量 int MaxWeight; //最小重量 int MinWeight; //是否通过串口获取数据 bool GetData = true; public Make_ColorBoxWeigh() { InitializeComponent(); } private void 彩盒上料打印_Load(object sender, EventArgs e) { lbl = new ApplicationClass(); CheckForIllegalCrossThreadCalls = false; dh = new DataHelper(); asc.controllInitializeSize(this); } private void sncode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { sql.Clear(); sql.Append("select ms_makecode,mcd_okqty,ma_qty,ma_qty-mcd_okqty as mcd_waitqty,ma_salecode,pr_detail,pr_colorboxunit,pr_code,pr_colorboxgw,"); sql.Append("pr_colorboxunit,pr_colorboxmaxw,pr_colorboxminw from makeserial left join make on ms_makecode=ma_code left join product on "); sql.Append("ms_prodcode=pr_code left join makecraftdetail on ms_makecode=mcd_macode where ms_sncode='" + sncode.Text + "'"); 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 = int.Parse(_weight == "" ? "0" : _weight); MaxWeight = int.Parse(_maxweight == "" ? "0" : _maxweight); MinWeight = int.Parse(_minweight == "" ? "0" : _minweight); BaseUtil.SetFormValue(this.Controls, dt); if (Weight - MinWeight == MaxWeight - Weight) { pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight); } else { pr_colorboxgw.Text = MinWeight + "-" + MaxWeight; } string ErrorMessage; bool ifFirst; if (LogicHandler.CheckCurrentStepAndIfFirst(sncode.Text, ms_makecode.Text, User.UserSourceCode, "Make!ColorBoxWeigh", out ifFirst, out ErrorMessage)) { string YN = dh.GetConfig("BatchNumber", "MESSetting").ToString(); //表示需要验证判断md_qty 是否大于等于md_baseqty if (YN != "0") { sql.Clear(); sql.Append("select wm_concat(md_prodcode) ,count(1) cn from makeprepare left join "); sql.Append("makepreparedetail on mp_id=md_mpid where mp_makecode='" + ms_makecode.Text + "' and md_qty>物料:" + pr_code.Text + " ,岗位用料不足\n", Color.Red); return; } } //如果未打开串口设置为0 int ActualWeight = int.Parse(weight.Text == "" ? "0" : weight.Text); if (ActualWeight > MinWeight && ActualWeight < MaxWeight) { OperateResult.AppendText(">>检测合格\n", Color.Green); //显示最近的三个称量记录 Temp3.Text = Temp2.Text; Temp2.Text = Temp1.Text; Temp1.Text = sncode.Text; } else OperateResult.AppendText(">>检测未通过\n", Color.Red); } else { OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red); } sncode.Text = ""; } else { OperateResult.AppendText(">>序列号不存在\n", Color.Red); return; } } } private void getSerialData() { if (serialPort1.IsOpen) { if (!SystemInf.OpenPort.Contains(serialPort1.PortName)) { SystemInf.OpenPort.Add(serialPort1.PortName); try { while (GetData) { try { weight.Text = Regex.Replace(serialPort1.ReadLine(), "\\D+", ""); } catch (Exception) { GetData = false; } } } catch (IOException ex) { MessageBox.Show(ex.Message); } } else MessageBox.Show("端口已被占用,请关闭其他窗口"); } } private void 彩盒称重_SizeChanged(object sender, EventArgs e) { asc.controlAutoSize(this); } private void Clean_Click(object sender, EventArgs e) { OperateResult.Clear(); } //关闭窗口的时候停止进程读取串口数据 private void 彩盒称重_FormClosing(object sender, FormClosingEventArgs e) { if (serialPort1.IsOpen) { GetData = false; serialPort1.Close(); SystemInf.OpenPort.Remove(serialPort1.PortName); thread.Interrupt(); thread.Join(); } } private void Confirm_Click(object sender, EventArgs e) { Print.CodeSoft(lbl, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, sncode.Text); } private void Cancel_Click(object sender, EventArgs e) { string Error = ""; } 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) { MessageBox.Show(mes.Message); } } private void StopWeight_Click(object sender, EventArgs e) { GetData = false; SystemInf.OpenPort.Remove(serialPort1.PortName); serialPort1.Close(); } private void pr_code_TextChanged(object sender, EventArgs e) { PrintLabel.DataSource = dh.ExecuteSql("select pl_labelcode,pl_labelname from productlabel where pl_prodcode='" + pr_code.Text + "'", "select"); PrintLabel.DisplayMember = "pl_labelname"; PrintLabel.ValueMember = "pl_labelcode"; } private void pr_colorboxunit_Click(object sender, EventArgs e) { } } }