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(); private System.DateTime[] indate; public Make_CartonBoxWeigh() { InitializeComponent(); } private void 卡通箱称重_Load(object sender, EventArgs e) { CheckForIllegalCrossThreadCalls = false; asc.controllInitializeSize(this); dh = new DataHelper(); lbl = new ApplicationClass(); ComList.Text = Properties.Settings.Default.PortName; BaudRate.Text = Properties.Settings.Default.BaudRate; } 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_cartonmaxw"].ToString(); string _minweight = dt.Rows[0]["pr_cartonminw"].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()); if (Weight - MinWeight == MaxWeight - Weight) pr_cartongw.Text = Weight + "±" + (MaxWeight - Weight); else pr_cartongw.Text = MinWeight + "-" + MaxWeight; int ActualWeight = int.Parse(weight.Text == "" ? "0" : weight.Text); if (ActualWeight >= MinWeight && ActualWeight <= MaxWeight) { OperateResult.AppendText(">>箱号" + outboxcode.Text + "检测合格\n", Color.Green); if (AutoPrint.Checked) { OperateResult.AppendText(">>箱号" + outboxcode.Text + "检测合格\n", Color.Green); Print.CodeSoft(lbl, PrintLabel.Text.Split(':')[1], PrintLabel.SelectedValue.ToString(), PrinterList.Text, outboxcode.Text, int.Parse(PrintNum.Text), indate[PrintLabel.SelectedIndex]); } AfterWeighPass(); } else OperateResult.AppendText(">>箱号" + outboxcode.Text + "检测未通过\n", Color.Red, outboxcode); } else OperateResult.AppendText(">>箱号" + outboxcode.Text + "不存在\n", Color.Red, outboxcode); } } 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) { OperateResult.AppendText(">>" + ex.Message + "\n", Color.Red, outboxcode); } } else OperateResult.AppendText(">>端口已被占用,请关闭其他窗口\n", Color.Red, outboxcode); } } 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"); outboxcode.Text = ""; } //确认打印 private void Confirm_Click(object sender, EventArgs e) { Print.CodeSoft(lbl, PrintLabel.Text.Split(':')[1], PrintLabel.SelectedValue.ToString(), PrinterList.Text, outboxcode.Text, int.Parse(PrintNum.Text), indate[PrintLabel.SelectedIndex]); } //停止进程,关闭串口 private void StopWeight_Click(object sender, EventArgs e) { GetData = false; SystemInf.OpenPort.Remove(serialPort1.PortName); serialPort1.Close(); } //关闭窗口的时候停止进程,不再读取串口数据 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) { if (BaudRate.Text == "" || ComList.Text == "") OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red); else OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red); } } private void pr_code_TextChanged(object sender, EventArgs e) { DataTable dt = (DataTable)dh.ExecuteSql("select pl_labelcode||':'||pl_labelname pl_laname,pl_labelname,pl_labelcode,pl_labelurl,pl_indate from productlabel where pl_prodcode='" + pr_code.Text + "' order by pl_isdefault desc ", "select"); PrintLabel.DataSource = dt; PrintLabel.DisplayMember = "pl_laname"; PrintLabel.ValueMember = "pl_labelcode"; ftpOperater ftp = new ftpOperater(); indate = new System.DateTime[dt.Rows.Count]; for (int i = 0; i < dt.Rows.Count; i++) { BaseUtil.GetPrintLabel(dt.Rows[i]["pl_labelname"].ToString(), dt.Rows[i]["pl_labelurl"].ToString(), dt.Rows[i]["pl_indate"].ToString()); indate[i] = Convert.ToDateTime(dt.Rows[i]["pl_indate"].ToString()); } } } }