| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- 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();
- //称量的标准重量
- Double Weight;
- //最大重量
- Double MaxWeight;
- //最小重量
- Double MinWeight;
- //是否通过串口获取数据
- bool GetData = true;
- string ErrorMessage;
- Thread InitPrint;
- System.DateTime[] indate;
- private bool AutoCut;
- 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)
- {
- if (LogicHandler.CheckStepAttribute(Tag.ToString(), User.UserSourceCode, out ErrorMessage))
- {
- 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();
- string pr_colorunit = dt.Rows[0]["pr_colorboxunit"].ToString();
- //赋值重量单位
- Weight = double.Parse(_weight == "" ? "0" : _weight);
- MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
- MinWeight = double.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 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<md_baseqty");
- dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
- if (dt.Rows[0]["CN"].ToString() != "0")
- {
- OperateResult.AppendText(">>物料:" + pr_code.Text + " ,岗位用料不足\n", Color.Red, sncode);
- return;
- }
- }
- //如果未打开串口设置为0
- if (pr_colorboxunit.Text == "kg")
- weight.Text = (float.Parse(weight.Text) / 1000).ToString();
- double ActualWeight = double.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))
- {
- LoadCheckQTY();
- 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);
- }
- else OperateResult.AppendText(">>" + ErrorMessage + "\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(Tag.ToString(), 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)
- {
- LoadCheckQTY();
- 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);
- }
- private void SendCheck_Click(object sender, EventArgs e)
- {
- sql.Clear();
- sql.Append("update oqcbatch set ob_status='UNCHECK' where ob_checkno ='" + ob_checkno.Text + "'");
- dh.ExecuteSql(sql.GetString(), "select");
- ob_nowcheckqty.Text = "";
- ob_batchqty.Text = "";
- ob_nowcheckqty.ForeColor = Color.Black;
- SendCheck.Enabled = false;
- OperateResult.AppendText(">>批次" + ob_checkno.Text + "送检成功\n", Color.Green);
- LogicHandler.InsertMakeProcess(sncode.Text, ms_makecode.Text, User.UserSourceCode, "手动送检", "手动送检成功", User.UserCode);
- //记录操作日志
- LogicHandler.DoCommandLog(Tag.ToString(),User.UserCode, ms_makecode.Text, User.UserLineCode, User.UserSourceCode, "手动送检", "手动送检成功", "", ob_checkno.Text);
- ob_checkno.Text = "";
- }
- private void LoadCheckQTY()
- {
- sql.Clear();
- string condition = "";
- int nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text);
- int batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text);
- if (nowcheckqty + 1 == batchqty && AutoCut)
- {
- condition = "and ob_status='UNCHECK' and ob_checkno='" + ob_checkno.Text + "'";
- }
- else
- {
- condition = "and ob_status='ENTERING' ";
- }
- sql.Append("select ob_batchqty,ob_nowcheckqty,ob_checkno from oqcbatch where ");
- sql.Append("ob_linecode='" + User.UserLineCode + "' and ob_prodcode='" + pr_code.Text + "' and ");
- sql.Append("ob_stepcode='" + User.CurrentStepCode + "' " + condition);
- DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
- if (dt.Rows.Count > 0)
- {
- ob_batchqty.Text = dt.Rows[0]["ob_batchqty"].ToString();
- ob_nowcheckqty.Text = dt.Rows[0]["ob_nowcheckqty"].ToString();
- ob_checkno.Text = dt.Rows[0]["ob_checkno"].ToString();
- nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text);
- batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text);
- if (nowcheckqty == batchqty)
- {
- ob_nowcheckqty.ForeColor = Color.Red;
- OperateResult.AppendText(">>当前采集数量已达到送检数量\n", Color.Red);
- }
- }
- else
- {
- ob_batchqty.Text = "";
- ob_nowcheckqty.Text = "";
- ob_checkno.Text = "";
- SendCheck.Enabled = false;
- }
- if (ob_batchqty.Text != "")
- {
- SendCheck.Enabled = true;
- }
- else
- {
- SendCheck.Enabled = false;
- }
- }
- private void ob_checkno_TextChanged(object sender, EventArgs e)
- {
- if (ob_checkno.Text != "")
- {
- string Cut = dh.getFieldDataByCondition("product left join oqcbatch on ob_prodcode=pr_code", "pr_ifautocutcheckno", "ob_checkno='" + ob_checkno.Text + "'").ToString();
- if (Cut == "" || Cut == "0")
- AutoCut = false;
- else
- AutoCut = true;
- SendCheck.Enabled = true;
- }
- }
- }
- }
|