using Microsoft.Win32; using System; using System.Data; 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; using System.Drawing; using LabelManager2; using UAS_MES.PublicForm; namespace UAS_MES.Make { public partial class Make_ColorBoxWeigh : Form { AutoSizeFormClass asc = new AutoSizeFormClass(); ApplicationClass lbl; DataHelper dh; DataTable dt; LogStringBuilder sql = new LogStringBuilder(); //启用线程进行称重数据读取 Thread thread; SerialPort serialPort1 = new SerialPort(); //称量的标准重量 int Weight; //最大重量 int MaxWeight; //最小重量 int MinWeight; //是否通过串口获取数据 bool GetData = true; Thread InitPrint; System.DateTime[] indate; public Make_ColorBoxWeigh() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; } private void Make_ColorBoxWeigh_Load(object sender, EventArgs e) { 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 ex) { OperateResult.AppendText("未正确安装CodeSoft软件\n", Color.Red); } } private void sncode_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { sql.Clear(); sql.Append("select ms_makecode,mcd_inqty,ma_qty,ma_qty-mcd_inqty as mcd_waitqty,ma_salecode,pr_detail,pr_colorboxunit,pr_code,pr_colorboxgw,"); sql.Append("pr_colorboxunit,pr_colorboxmaxw,pr_colorboxminw from makeserial left join make on ms_makecode=ma_code left join product on "); sql.Append("ms_prodcode=pr_code left join makecraftdetail on ms_makecode=mcd_macode where ms_sncode='" + sncode.Text + "' order by ms_id desc"); dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select"); if (dt.Rows.Count > 0) { //重量的临时变量 string _weight = dt.Rows[0]["pr_colorboxgw"].ToString(); string _maxweight = dt.Rows[0]["pr_colorboxmaxw"].ToString(); string _minweight = dt.Rows[0]["pr_colorboxminw"].ToString(); //赋值重量单位 Weight = int.Parse(_weight == "" ? "0" : _weight); MaxWeight = int.Parse(_maxweight == "" ? "0" : _maxweight); MinWeight = int.Parse(_minweight == "" ? "0" : _minweight); BaseUtil.SetFormValue(this.Controls, dt); if (Weight - MinWeight == MaxWeight - Weight) pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight); else pr_colorboxgw.Text = MinWeight + "-" + MaxWeight; string ErrorMessage; string oMakeCode = ""; string oMsID = ""; if (LogicHandler.CheckStepSNAndMacode(ms_makecode.Text, User.UserSourceCode, sncode.Text, User.UserCode, out oMakeCode, out oMsID, out ErrorMessage)) { string YN = dh.GetConfig("BatchNumber", "MESSetting").ToString(); //表示需要验证判断md_qty 是否大于等于md_baseqty if (YN != "0") { sql.Clear(); sql.Append("select wm_concat(md_prodcode) ,count(1) cn from makeprepare left join makepreparedetail "); sql.Append("on mp_id=md_mpid where mp_makecode='" + ms_makecode.Text + "' and md_qty>物料:" + pr_code.Text + " ,岗位用料不足\n", Color.Red, sncode); return; } } //如果未打开串口设置为0 int ActualWeight = int.Parse(weight.Text == "" ? "0" : weight.Text); if ((ActualWeight >= MinWeight) && (ActualWeight <= MaxWeight)) { ListViewItem lsi = new ListViewItem(); lsi.SubItems.Add(sncode.Text); lsi.SubItems.Add(ActualWeight.ToString()); lsi.SubItems.Add(System.DateTime.Now.ToString()); WeighRecord.Items.Add(lsi); //显示最近的三个称量记录 if (LogicHandler.SetStepResult(ms_makecode.Text, User.UserSourceCode, sncode.Text, "彩盒称量", "称量合格", User.UserCode, out ErrorMessage)) { OperateResult.AppendText(">>" + sncode.Text + "重量检测检测合格\n", Color.Green); if (AutoPrint.Checked) { OperateResult.AppendText(">>打印条码号" + sncode.Text + "\n", Color.Green); Confirm.PerformClick(); } sncode.Text = ""; } else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, sncode); } else OperateResult.AppendText(">>" + sncode.Text + "重量检测未通过\n", Color.Red); //称量是否通过都记录重量 dh.ExecuteSql("update makeserial set ms_grossw='" + ActualWeight + "' where ms_sncode='" + sncode.Text + "' and ms_makecode='" + ms_makecode.Text + "'", "update"); sncode.Clear(); } else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, sncode); } else OperateResult.AppendText(">>序列号不存在\n", Color.Red, sncode); } } 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 OperateResult.AppendText(">>端口已被占用,请关闭其他窗口\n", Color.Red); } } private void 彩盒称重_SizeChanged(object sender, EventArgs e) { asc.controlAutoSize(this); } protected override void OnVisibleChanged(EventArgs e) { base.OnVisibleChanged(e); if (!IsHandleCreated) { this.Close(); } } private void Clean_Click(object sender, EventArgs e) { OperateResult.Clear(); } //关闭窗口的时候停止进程读取串口数据 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(); thread.Join(); } } private void Confirm_Click(object sender, EventArgs e) { if (PrintLabel.Items.Count != 0) { Print.CodeSoft(lbl, PrintLabel.Text.Split(':')[1], PrintLabel.SelectedValue.ToString(), PrinterList.Text, sncode.Text, int.Parse(PrintNum.Text), indate[PrintLabel.SelectedIndex]); } else { OperateResult.AppendText(">>产品" + pr_code.Text + "未维护打印标签\n", Color.Red); } } private void StartWeight_Click(object sender, EventArgs e) { thread = new Thread(getSerialData); try { serialPort1.PortName = ComList.Text; serialPort1.BaudRate = int.Parse(BaudRate.Text); serialPort1.Open(); GetData = true; 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 StopWeight_Click(object sender, EventArgs e) { GetData = false; SystemInf.OpenPort.Remove(serialPort1.PortName); serialPort1.Close(); } private void pr_code_TextChanged(object sender, EventArgs e) { 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()); } } private void LoadCollectNum() { DataTable dt = (DataTable)dh.ExecuteSql("select mcd_inqty,ma_qty-mcd_inqty mcd_waitqty from make left join makecraftdetail on mcd_macode=ma_code where ma_code='" + ms_makecode.Text + "' and mcd_stepcode='" + User.CurrentStepCode + "'", "select"); BaseUtil.SetFormValue(Controls, dt); } } }