using LabelManager2; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.IO.Ports; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Windows.Forms; using UAS_MES_NEW.DataOperate; using UAS_MES_NEW.Entity; using UAS_MES_NEW.PublicForm; using UAS_MES_NEW.PublicMethod; namespace UAS_MES_NEW.Packing { public partial class Packing_BigBoxWeigh : Form { AutoSizeFormClass asc = new AutoSizeFormClass(); DataHelper dh; Document doc; //true的时候表示从串口读取数据 bool GetData = true; DataTable dt; LogStringBuilder sql = new LogStringBuilder(); ApplicationClass lbl; Thread thread; Thread InitPrint; //打印计数 int printcount; //创建串口实例 SerialPort serialPort1 = new SerialPort(); //存储当前大箱号 string outboxcode = ""; //记录称重串口号和波特率 string Comlist = ""; string Baurate = ""; public Packing_BigBoxWeigh() { InitializeComponent(); } protected override void OnVisibleChanged(EventArgs e) { base.OnVisibleChanged(e); if (!IsHandleCreated) { this.Close(); } } private void Make_BigBoxWeight_Load(object sender, EventArgs e) { asc.controllInitializeSize(this); CheckForIllegalCrossThreadCalls = false; Comlist = BaseUtil.GetCacheData("PortName").ToString(); Baurate = BaseUtil.GetCacheData("BaudRate").ToString(); InitPrint = new Thread(InPrint); SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序"); BaseUtil.SetFormCenter(stw); stw.ShowDialog(); pa_outboxcode.Focus(); OperateResult.AppendText(">>请输入大箱号\n"); printcount = 0; weightsum.Text = printcount + ""; StartWeight.PerformClick(); dh = SystemInf.dh; } private void InPrint() { try { lbl = new ApplicationClass(); BaseUtil.WriteLbl(); } catch { OperateResult.AppendText("未正确安装CodeSoft软件\n", Color.Red); } } private void Make_BigBoxWeight_SizeChanged(object sender, EventArgs e) { asc.controlAutoSize(this); } private void pa_outboxcode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { sql.Clear(); sql.Append("select pa_id,pa_prodcode,pr_spec,pr_detail,pa_makecode,pa_salecode,pa_totalqty,"); sql.Append("pa_packageqty from package left join product on pr_code=pa_prodcode where "); sql.Append("pa_outboxcode='" + pa_outboxcode.Text + "' and pa_type=2"); dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); outboxcode = pa_outboxcode.Text; weightsum.Text = printcount + ""; if (dt.Rows.Count > 0) { BaseUtil.SetFormValue(this.Controls, dt); pa_outboxcode.Text = outboxcode; } else { OperateResult.AppendText(">>箱号不存在\n", Color.Red); return; } double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text.Replace("kg", "").Trim()); if (ActualWeight > 0) { //更新大箱重量 sql.Clear(); sql.Append("update package set pa_weight='" + ActualWeight + "' where pa_outboxcode= '" + pa_outboxcode.Text + "'"); dh.ExecuteSql(sql.GetString(), "update"); OperateResult.AppendText("<<<箱号:" + pa_outboxcode.Text + ",重量:" + weight.Text + "\n", Color.Green); LogicHandler.InsertMakeProcess(pa_outboxcode.Text, pa_makecode.Text, User.UserSourceCode, "大箱称量", "大箱称量合格", User.UserCode); OperateResult.AppendText("<<<更新成功\n", Color.Green); //刷新grid的历史称重信息; recordResult(pa_outboxcode.Text, weight.Text, System.DateTime.Now.ToString()); LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, pa_makecode.Text, User.UserLineCode, User.UserSourceCode, "大箱" + pa_outboxcode.Text + "称重:" + weight.Text, "称重成功", pa_outboxcode.Text, ""); LogicHandler.RecordProdWeight(pa_outboxcode.Text, "BIGBOX", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pa_prodcode.Text, User.UserSourceCode, User.UserName); if (autoprint.Checked == true) { try { //doc = lbl.Documents.Open(ftpOperater.DownLoadTo + PrintLabel.Text); string ErrorMessage = ""; if (!Print.CodeSoft(Tag.ToString(), ref lbl, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, pa_outboxcode.Text, int.Parse(PrintNum.Text), pa_makecode.Text, pa_prodcode.Text, "大箱标", "0", out ErrorMessage)) { OperateResult.AppendText(ErrorMessage + "\n", Color.Red); } dh.ExecuteSql("update package set pa_printcount= pa_printcount+1 where pa_outboxcode='" + pa_outboxcode.Text + "'", "update"); printcount++; weightsum.Text = printcount + ""; //清空输入框中的值,扫描框置空定焦 pa_outboxcode.Text = ""; pa_outboxcode.Focus(); } catch (Exception eb) { if (PrintLabel.SelectedValue == null) { OperateResult.AppendText(">>产品:" + pa_prodcode.Text + "未维护大箱标签模板\n", Color.Red, pa_outboxcode); pa_outboxcode.Focus(); } else OperateResult.AppendText("<<<打印失败:" + eb.Message + "\n", Color.Red); } } } else { OperateResult.AppendText("<<<大箱重量应大于0\n", Color.Red); } } } private void recordResult(string pa_outboxcode, string weight, string dateTime) { //创建一个item ListViewItem lvi = new ListViewItem(); //分条赋值 lvi.SubItems.Add(pa_outboxcode); lvi.SubItems.Add(weight); lvi.SubItems.Add(dateTime); //添加结果的信息进去 showResult.Items.Add(lvi); } private void pa_prodcode_TextChanged(object sender, EventArgs e) { DataTable _dt = (DataTable)dh.ExecuteSql("select la_id,substr(la_url,instr(la_url,'\\',-1)+1)la_name,la_url,la_isdefault from label where la_prodcode='" + pa_prodcode.Text + "' and la_templatetype='大箱标' and la_statuscode='AUDITED' order by la_isdefault", "select"); PrintLabel.DataSource = _dt; PrintLabel.DisplayMember = "la_name"; PrintLabel.ValueMember = "la_id"; ftpOperater ftp = new ftpOperater(); for (int i = 0; i < _dt.Rows.Count; i++) { BaseUtil.GetPrintLabel(_dt.Rows[i]["la_name"].ToString(), _dt.Rows[i]["la_url"].ToString()); } } private void StartWeight_Click(object sender, EventArgs e) { thread = new Thread(getSerialData); try { GetData = true; serialPort1.PortName = Comlist; serialPort1.BaudRate = int.Parse(Baurate); serialPort1.Open(); thread.Start(); } catch (Exception mes) { if (Baurate == "" || Comlist == "") OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red); else OperateResult.AppendText(">>" + mes.Message + "\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 = serialPort1.ReadLine(); //weight.Text = Regex.Replace(serialPort1.ReadLine(), "\\D+", ""); } catch (Exception) { GetData = false; } } } catch (IOException ex) { MessageBox.Show(ex.Message); } } else MessageBox.Show("端口已被占用,请关闭其他窗口"); } } private void StopWeight_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) { GetData = false; serialPort1.Close(); SystemInf.OpenPort.Remove(serialPort1.PortName); thread.Abort(); } } private void Make_BigBoxWeight_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 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); } } } }