using LabelManager2; using Seagull.BarTender.Print; using System; using System.Data; using System.Drawing; using System.IO; using System.IO.Ports; 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_PalletWeigh : Form { AutoSizeFormClass asc = new AutoSizeFormClass(); LogStringBuilder sql = new LogStringBuilder(); ApplicationClass lbl; Document doc; Thread InitPrint; Thread thread; DataHelper dh; DataTable dt; DataTable listc; DataTable info; //创建串口实例 SerialPort serialPort1 = new SerialPort(); //true的时候表示从串口读取数据 bool GetData = true; public Packing_PalletWeigh() { InitializeComponent(); } private void Make_PalletWeigh_Load(object sender, EventArgs e) { CheckForIllegalCrossThreadCalls = false; asc.controllInitializeSize(this); OperateResult.AppendText(">>请输入栈板号\n", Color.Black); //栈板号输入框聚焦 ComList.Text = BaseUtil.GetCacheData("PortName").ToString(); BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString(); InitPrint = new Thread(InPrint); SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序"); BaseUtil.SetFormCenter(stw); stw.ShowDialog(); startWeigh.PerformClick(); dh = SystemInf.dh; palletcode.Focus(); } protected override void OnVisibleChanged(EventArgs e) { base.OnVisibleChanged(e); if (!IsHandleCreated) { this.Close(); } } Engine engine; private void InPrint() { try { engine = new Engine(true); BaseUtil.WriteLbl(); } catch (Exception ex) { OperateResult.AppendText("未正确安装BarTender软件\n", Color.Red); } } private void Make_PalletWeigh_SizeChanged(object sender, EventArgs e) { asc.controlAutoSize(this); } private void palletcode_KeyDown(object sender, KeyEventArgs e) { //按下了enter键 if (e.KeyCode == Keys.Enter) { if (palletcode.Text == "") { OperateResult.AppendText("<<输入不能为空\n", Color.Red); return; } if (weight.Text == "") { OperateResult.AppendText("<<未读取到重量信息\n", Color.Red); return; } //校验栈板号 sql.Clear(); sql.Append("select pa_id,pa_prodcode,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=:code and pa_type='3'"); dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select", palletcode.Text); if (dt.Rows.Count == 0) { OperateResult.AppendText("<<无该栈板号\n", Color.Red, palletcode); } else { //给页面赋值 BaseUtil.SetFormValue(this.Controls, dt); //刷新grid的历史称重信息; if (double.Parse(weight.Text.Replace("kg", "").Trim()) == 0) { OperateResult.AppendText("<<重量不能等于0\n", Color.Red, palletcode); return; } // 更新大箱重量 sql.Clear(); sql.Append("update package set pa_weight=:weight where pa_id=:pa_id "); dh.ExecuteSql(sql.GetString(), "update", weight.Text.Replace("kg", "").Trim(), dt.Rows[0]["pa_id"].ToString()); //记录makeprocess //查询maid info = (DataTable)dh.ExecuteSql("select ma_id,ma_wccode from make where ma_code='" + pa_makecode.Text + "'", "select"); if (info.Rows.Count > 0) { //有值 //查询 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,"); sql.Append("mp_snoutboxcode) values ( makeprocess_seq.nextval,:makecode,:ma_id,:craftcode,:craftname,'栈板称重'"); sql.Append(",:result,sysdate,:inman,:workcenter,:linecode,:source,:palletcode) "); dh.ExecuteSql(sql.GetString(), "insert", pa_makecode.Text, info.Rows[0]["ma_id"].ToString(), User.CurrentStepCode, User.CurrentStepName, "称重成功,栈板号:" + palletcode.Text + ",重量:" + weight.Text.Trim(), User.UserName, info.Rows[0]["ma_wccode"].ToString(), User.UserLineCode, User.UserSourceCode, palletcode.Text); //提示信息栏显示:<< 栈板号:XXXX,重量:xxxx 克, OperateResult.AppendText("<<栈板号:" + palletcode.Text + ",重量:" + weight.Text.Trim() + "\n", Color.Green); //记录日志 LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, pa_makecode.Text, User.UserLineCode, User.UserSourceCode, "栈板称重", "重量:" + weight.Text.Trim() + "\n", palletcode.Text, ""); //刷新称重计数+1 weightedNum.Text = double.Parse(weightedNum.Text) + 1 + ""; recordResult(palletcode.Text, weight.Text.Trim(), System.DateTime.Now.ToString()); //插入称重日志表 //sql.Clear(); //sql.Append("insert into weightlog(wl_id,wl_type,wl_scancode,wl_weight,wl_unit,wl_linecode,"); //sql.Append("wl_prodcode,wl_sccode,wl_indate,wl_inman) values (weightlog_seq.nextval,'PALLET',:palletcode,"); //sql.Append(":weight,'kg',:linecode,:prodcode,:sccode,sysdate,:inman)"); //dh.ExecuteSql(sql.GetString(), "insert", palletcode.Text, weight.Text.Replace("kg", "").Trim(), User.UserLineCode, pa_prodcode.Text, User.UserSourceCode, User.UserName); LogicHandler.RecordProdWeight(palletcode.Text, "PALLET", float.Parse(weight.Text.Replace("kg", "").Trim()), "kg", User.UserLineCode, pa_prodcode.Text, User.UserSourceCode, User.UserName); //如果用户勾选了自动打印,调用打印, 打印成功,消息提示栏显示“打印成功”,“请输入栈板号” if (autoPrint.Checked == true) { try { if (PrintLabel.SelectedValue == null) { OperateResult.AppendText(">>产品:" + pa_prodcode.Text + "未维护栈板标签模板\n", Color.Red, palletcode); return; } //doc = lbl.Documents.Open(PrintLabel.Text); string ErrorMessage = ""; if (Print.BarTenderS(Tag.ToString(), ref engine, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, palletcode.Text, int.Parse(PrintNum.Text), pa_makecode.Text, pa_prodcode.Text, "栈板标", "0", out ErrorMessage)) { //打印成功,请输入栈板号 OperateResult.AppendText("<<打印成功\n", Color.Green); } else { OperateResult.AppendText(ErrorMessage + "\n", Color.Red); } } catch (Exception ex) { OperateResult.AppendText("<<打印失败\n", Color.Red); OperateResult.AppendText(">>请输入栈板号\n", Color.Black, palletcode); return; } OperateResult.AppendText(">>请输入栈板号\n", Color.Black); //更新栈板打印次数 sql.Clear(); sql.Append("update package set pa_printcount= pa_printcount+1 where pa_id=:pa_id"); dh.ExecuteSql(sql.GetString(), "update", dt.Rows[0]["pa_id"].ToString()); //清空输入框中的值,扫描框置空定焦 palletcode.Text = ""; } else { OperateResult.AppendText("<<需打印请勾选自动打印\n", Color.Red); } } } } } private void pa_prodcode_TextChanged(object sender, EventArgs e) { listc = (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"); if (listc.Rows.Count == 0) { OperateResult.AppendText(">>产品:" + pa_prodcode.Text + "未维护栈板标签模板\n", Color.Red); } PrintLabel.DataSource = listc; PrintLabel.DisplayMember = "la_url"; PrintLabel.ValueMember = "la_id"; } private void startWeigh_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 stopWeigh_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) { GetData = false; serialPort1.Close(); SystemInf.OpenPort.Remove(serialPort1.PortName); thread.Abort(); } } 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+", ""); weight.Text = serialPort1.ReadLine(); } catch (Exception) { GetData = false; } } } catch (IOException ex) { MessageBox.Show(ex.Message); } } else MessageBox.Show("端口已被占用,请关闭其他窗口"); } } private void Make_PalletWeigh_FormClosing(object sender, FormClosingEventArgs e) { BaseUtil.ClosePrint(lbl); stopWeigh.PerformClick(); InitPrint.Abort(); if (serialPort1.IsOpen) { GetData = false; serialPort1.Close(); SystemInf.OpenPort.Remove(serialPort1.PortName); thread.Interrupt(); } //thread.Abort(); } private void recordResult(string palletcode, string weigh, string time) { //if (showResult.Items.Count >= 50) //{ // //如果有10行的话,则删除最前一行 // showResult.Items.RemoveAt(0); //} //创建一个item ListViewItem lvi = new ListViewItem(); //分条赋值 lvi.SubItems.Add(palletcode); lvi.SubItems.Add(weigh); lvi.SubItems.Add(time); //添加结果的信息进去 showResult.Items.Add(lvi); } 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); } } } }