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.PublicForm; 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; Document doc; Thread thread; //创建串口实例 SerialPort serialPort1 = new SerialPort(); //true的时候表示从串口读取数据 bool GetData = true; //箱内总数 int pa_totalqty; //称量的标准重量 Double Weight; //最大重量 Double MaxWeight; //最小重量 Double MinWeight; DataTable ListC = new DataTable(); Thread InitPrint; public Make_CartonBoxWeigh() { InitializeComponent(); } protected override void OnVisibleChanged(EventArgs e) { base.OnVisibleChanged(e); if (!IsHandleCreated) { this.Close(); } } private void 卡通箱称重_Load(object sender, EventArgs e) { CheckForIllegalCrossThreadCalls = false; asc.controllInitializeSize(this); 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(); StartWeight.PerformClick(); dh = new DataHelper(); } private void InPrint() { try { lbl = new ApplicationClass(); BaseUtil.WriteLbl(lbl); } catch (Exception) { OperateResult.AppendText("未正确安装CodeSoft软件\n", Color.Red); } } private void 卡通箱称重_SizeChanged(object sender, EventArgs e) { asc.controlAutoSize(this); } private void sncode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { //获取序列号的箱号 string ms_outboxcode = dh.getFieldDataByCondition("makeserial", "ms_outboxcode", "ms_sncode='" + outboxcode.Text + "' order by ms_id desc").ToString(); if (ms_outboxcode != "") { outboxcode.Text = ms_outboxcode; } //根据箱号查询表单数据 sql.Clear(); sql.Append("select pa_totalqty,ma_code,mcd_inqty,ma_qty,ma_qty-mcd_inqty as mcd_waitqty,ma_salecode,pr_detail,pr_cartonunit,pr_code,pr_cartongw,"); sql.Append("pr_cartonmaxw,pr_cartonminw,nvl(PR_CHECKCARTONW,'0') PR_CHECKCARTONW from package left join product on pr_code=pa_prodcode left join make on ma_prodcode=pr_code "); sql.Append("left join makecraftdetail on mcd_macode=ma_code where pa_outboxcode='" + outboxcode.Text + "' and mcd_stepcode='" + User.CurrentStepCode + "'"); DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); //填充Form的值 BaseUtil.SetFormValue(this.Controls, dt); //填充打印文件选项的DataGridView if (dt.Rows.Count > 0) { string ErrorMessage; //重量的临时变量 string PR_CHECKCARTONW = dt.Rows[0]["PR_CHECKCARTONW"].ToString(); string _weight = dt.Rows[0]["pr_cartongw"].ToString(); string _maxweight = dt.Rows[0]["pr_cartonmaxw"].ToString(); string _minweight = dt.Rows[0]["pr_cartonminw"].ToString(); string pa_pr_cartonunit = dt.Rows[0]["pr_cartonunit"].ToString(); Weight = double.Parse(_weight == "" ? "0" : _weight); MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight); MinWeight = double.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) + dt.Rows[0]["pr_cartonunit"].ToString(); else pr_cartongw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_cartonunit"].ToString(); //if (pr_cartonunit.Text == "kg") //{ // weight.Text = (float.Parse(weight.Text) / 1000).ToString(); //} double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text.Replace("kg", "").Trim()); //需要检查称重重量 if (PR_CHECKCARTONW != "0") { //称量合格或不合格都记录重量 if (ActualWeight >= MinWeight && ActualWeight <= MaxWeight) { LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "卡通箱称重", "称量合格:" + weight.Text, outboxcode.Text, ""); OperateResult.AppendText(">>箱号" + outboxcode.Text + "检测合格\n", Color.Green); } else { LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "卡通箱称重", "称量不合格:" + weight.Text, outboxcode.Text, ""); OperateResult.AppendText(">>箱号" + outboxcode.Text + "检测未通过\n", Color.Red); return; } } else { LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "卡通箱称重", "称量完成:" + weight.Text, outboxcode.Text, ""); } if (AutoPrint.Checked) { if (PrintLabel.Items.Count > 0) { doc = lbl.Documents.Open(ftpOperater.DownLoadTo + PrintLabel.Text); Print.CodeSoft(Tag.ToString(), doc, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), PrinterList.Text, outboxcode.Text, int.Parse(PrintNum.Text)); } else { OperateResult.AppendText(">>产品" + pr_code.Text + "未维护卡通标签\n", Color.Green); } } ListViewItem lsi = new ListViewItem(); lsi.SubItems.Add(outboxcode.Text); lsi.SubItems.Add(ActualWeight.ToString() + pr_cartonunit.Text); lsi.SubItems.Add(System.DateTime.Now.ToString()); WeighRecord.Items.Add(lsi); OperateResult.AppendText(">>箱号" + outboxcode.Text + "称重完成\n", Color.Green); dh.ExecuteSql("update package set pa_weight='" + ActualWeight + "', pa_printcount= nvl(pa_printcount,0)+1 where pa_outboxcode='" + outboxcode.Text + "'", "update"); dh.UpdateByCondition("package", "pa_status=1,pa_packageqty=pa_currentqty,pa_totalqty=pa_currentqty", "pa_outboxcode='" + outboxcode.Text + "'"); outboxcode.Clear(); } 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 = serialPort1.ReadLine(); //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 Confirm_Click(object sender, EventArgs e) { doc = lbl.Documents.Open(ftpOperater.DownLoadTo + PrintLabel.Text); Print.CodeSoft(Tag.ToString(), doc, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), PrinterList.Text, outboxcode.Text, int.Parse(PrintNum.Text)); } //停止进程,关闭串口 private void StopWeight_Click(object sender, EventArgs e) { GetData = false; SystemInf.OpenPort.Remove(serialPort1.PortName); serialPort1.Close(); } //关闭窗口的时候停止进程,不再读取串口数据 private void 卡通箱称重_FormClosing(object sender, FormClosingEventArgs e) { BaseUtil.ClosePrint(lbl); StopWeight.PerformClick(); 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) { 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='" + pr_code.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 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); } } } }