using LabelManager2; using Microsoft.Win32; using System; using System.Data; using System.Drawing; 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; namespace UAS_MES.Make { public partial class Make_CartonBoxWeigh : Form { AutoSizeFormClass asc = new AutoSizeFormClass(); DataHelper dh; DataTable dt; LogStringBuilder sql = new LogStringBuilder(); ApplicationClass lbl; Thread thread; //创建串口实例 SerialPort serialPort1 = new SerialPort(); //true的时候表示从串口读取数据 bool GetData = true; //箱内总数 int pa_totalqty; //称量的标准重量 int Weight; //最大重量 int MaxWeight; //最小重量 int MinWeight; DataTable ListC = new DataTable(); public Make_CartonBoxWeigh() { InitializeComponent(); } private void 卡通箱称重_Load(object sender, EventArgs e) { CheckForIllegalCrossThreadCalls = false; asc.controllInitializeSize(this); dh = new DataHelper(); lbl = new ApplicationClass(); } private void 卡通箱称重_SizeChanged(object sender, EventArgs e) { asc.controlAutoSize(this); } private void sncode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { //根据箱号查询表单数据 pd_barcode.Text = dh.getFieldDataByCondition("makeserial left join package on ms_prodcode=pa_prodcode", "ms_sncode", "pa_outboxcode='" + outboxcode.Text + "'").ToString(); sql.Clear(); sql.Append("select pa_totalqty,pd_barcode,ma_code,mcd_okqty,ma_qty,ma_qty-mcd_okqty as mcd_waitqty,ma_salecode,pr_detail,pr_cartonunit,pr_code,pr_cartongw,"); sql.Append("pr_cartonmaxw,pr_cartonminw from package left join product on pr_code=pa_prodcode left join make on ma_prodcode=pr_code left join makecraftdetail "); sql.Append("on mcd_macode=ma_code left join packagedetail on pd_outboxcode=pa_outboxcode where pa_outboxcode='" + outboxcode.Text + "' and mcd_stepcode='" + User.CurrentStepCode + "'"); dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); //填充Form的值 BaseUtil.SetFormValue(this.Controls, dt); //填充打印文件选项的DataGridView if (dt.Rows.Count > 0) { string ErrorMessage; //重量的临时变量 string _weight = dt.Rows[0]["pr_cartongw"].ToString(); string _maxweight = dt.Rows[0]["pr_cartonminw"].ToString(); string _minweight = dt.Rows[0]["pr_cartonmaxw"].ToString(); Weight = int.Parse(_weight == "" ? "0" : _weight); MaxWeight = int.Parse(_maxweight == "" ? "0" : _maxweight); MinWeight = int.Parse(_minweight == "" ? "0" : _minweight); pa_totalqty = int.Parse(dt.Rows[0]["pa_totalqty"].ToString()); bool ifFirst; if (LogicHandler.CheckCurrentStepAndIfFirst(pd_barcode.Text, ma_code.Text, User.UserSourceCode, Tag.ToString(), out ifFirst, out ErrorMessage)) { sql.Clear(); sql.Append("select count(1) cn from MES_PACKAGE_VIEW left join makeserial on v_prodcode=ms_prodcode where (ms_sncode="); sql.Append("v_barcode or ms_psn=v_barcode) and v_outboxcode='" + outboxcode.Text + "'"); int ActualWeight = int.Parse(weight.Text == "" ? "0" : weight.Text); if (ActualWeight > MinWeight && ActualWeight < MaxWeight) { if (PriLabel.Checked) { Print.CodeSoft(lbl, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, pd_barcode.Text); AfterWeighPass(); } else if (SupPriLabel.Checked) { Print.CodeSoft(lbl, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, pd_barcode.Text); dh.ExecuteSql("update package set pa_printcount= pa_printcount+1 where pa_outboxcode='" + outboxcode.Text + "'", "update"); AfterWeighPass(); } OperateResult.AppendText(">>检测合格\n", Color.Green); } else { OperateResult.AppendText(">>检测未通过\n", Color.Red); } } else { OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red); } } else { OperateResult.AppendText(">>箱号" + outboxcode.Text + "不存在\n", Color.Red); } } } 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 AfterWeighPass() { string ErrorMessage; dh.ExecuteSql("update package set pa_weight=" + weight.Text + ", pa_printcount= nvl(pa_printcount,0)+1 where pa_outboxcode='" + outboxcode.Text + "'", "update"); if (!LogicHandler.UpdateMakeMessage(pd_barcode.Text, ma_code.Text, User.UserSourceCode, User.UserName, "称量合格", out ErrorMessage)) OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red); } //确认打印 private void Confirm_Click(object sender, EventArgs e) { } //取消调整 private void Cancel_Click(object sender, EventArgs e) { } //停止进程,关闭串口 private void StopWeight_Click(object sender, EventArgs e) { GetData = false; SystemInf.OpenPort.Remove(serialPort1.PortName); serialPort1.Close(); } //只能默认勾选一个复选框 private void PrintLabel_CellClick(object sender, DataGridViewCellEventArgs e) { //if (PrintLabel.Columns[e.ColumnIndex].Name == "pl_isdefault") //{ // for (int i = 0; i < PrintLabel.Rows.Count; i++) // { // if (i != e.RowIndex && PrintLabel.CurrentCell.ColumnIndex == 0) // { // DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)PrintLabel.Rows[i].Cells[0]; // cell.Value = false; // } // } //} } //关闭窗口的时候停止进程,不再读取串口数据 private void 卡通箱称重_FormClosing(object sender, FormClosingEventArgs e) { lbl.Quit(); if (serialPort1.IsOpen) { GetData = false; serialPort1.Close(); SystemInf.OpenPort.Remove(serialPort1.PortName); thread.Interrupt(); } } private void StartWeight_Click(object sender, EventArgs e) { thread = new Thread(getSerialData); try { GetData = true; serialPort1.PortName = this.ComList.Text; serialPort1.BaudRate = int.Parse(BaudRate.Text); serialPort1.Open(); thread.Start(); } catch (Exception mes) { MessageBox.Show(mes.Message); } } 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"; } } }