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.DataOperate; using UAS_MES.Entity; using UAS_MES.PublicForm; using UAS_MES.PublicMethod; namespace UAS_MES.Make { public partial class Make_BigBoxWeight : Form { AutoSizeFormClass asc = new AutoSizeFormClass(); System.DateTime[] indate; DataHelper dh; //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 Make_BigBoxWeight() { 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(); dh = new DataHelper(); pa_outboxcode.Focus(); OperateResult.AppendText(">>请输入大箱号\n"); printcount = 0; weightsum.Text = printcount + ""; } private void InPrint() { try { lbl = new ApplicationClass(); BaseUtil.WriteLbl(lbl); } 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; BaseUtil.CleanForm(this); weightsum.Text = printcount + ""; if (dt.Rows.Count > 0) { BaseUtil.SetFormValue(this.Controls, dt); pa_outboxcode.Text = outboxcode; } else { OperateResult.AppendText(">>箱号不存在\n", Color.Red); return; } int ActualWeight = int.Parse(weight.Text == "" ? "0" : weight.Text); 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 + ",重量:" + ActualWeight + "克\n", Color.Green); //记录makeprocess sql.Clear(); sql.Append("insert into MakeProcess(mp_id,mp_makecode,mp_maid,mp_stepcode,mp_stepname ,"); sql.Append("mp_kind,mp_result,mp_indate,mp_inman,mp_wccode,mp_linecode,mp_sourcecode,mp_snoutboxcode)"); sql.Append("select MakeProcess_seq.nextval, ma_code,ma_id,'" + User.CurrentStepCode + "','" + User.CurrentStepName + "',"); sql.Append("'" + "大箱称重" + "','" + "称重成功,大箱箱号" + pa_outboxcode.Text + ",重量:" + ActualWeight + "克" + "',sysdate,'" + User.UserName + "',ma_wccode,'" + User.UserLineCode + "','" + User.UserSourceCode + "',"); sql.Append("'" + pa_outboxcode.Text + "' from make left join makeserial on ms_makecode=ma_code "); sql.Append("where ma_code='" + pa_makecode.Text + "'"); dh.ExecuteSql(sql.GetString(), "insert"); 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,"大箱称重","称重成功","",""); if (autoprint.Checked == true) { try { Print.CodeSoft(Tag.ToString(),lbl, PrintLabel.Text.Split(':')[1], PrintLabel.SelectedValue.ToString(), Printer.Text, pa_outboxcode.Text, int.Parse(PrintNum.Text), indate[PrintLabel.SelectedIndex]); 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.Text = ""; pa_outboxcode.Focus(); }else OperateResult.AppendText("<<<打印失败:"+eb.Message+"\n", Color.Red); } } else { OperateResult.AppendText("<<<需打印请勾选自动打印\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 pl_labelcode ||':'||pl_labelname pl_name,pl_labelname,pl_labelcode,pl_labelurl,pl_indate from productlabel where pl_prodcode='" + pa_prodcode.Text + "' and PL_LABELTYPE='大箱标' order by PL_ISDEFAULT DESC", "select"); PrintLabel.DataSource = _dt; PrintLabel.DisplayMember = "pl_name"; 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()); } } 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 = 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) { GetData = false; SystemInf.OpenPort.Remove(serialPort1.PortName); serialPort1.Close(); } private void Make_BigBoxWeight_FormClosing(object sender, FormClosingEventArgs e) { BaseUtil.ClosePrint(lbl); if (serialPort1.IsOpen) { GetData = false; serialPort1.Close(); SystemInf.OpenPort.Remove(serialPort1.PortName); thread.Interrupt(); } } } }