| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- using System;
- using System.Data;
- using System.IO;
- using System.IO.Ports;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Windows.Forms;
- using UAS_MES_NEW.DataOperate;
- using UAS_MES_NEW.Entity;
- using UAS_MES_NEW.PublicMethod;
- using System.Drawing;
- using LabelManager2;
- using UAS_MES_NEW.PublicForm;
- using System.Net.Sockets;
- using System.Collections.Generic;
- using System.Net;
- using System.Text;
- using Seagull.BarTender.Print;
- namespace UAS_MES_NEW.Make
- {
- public partial class Make_MidBoxWeigh : 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 LastSncode;
- DataTable Dbfind;
- Regex re = new Regex("\\d+.\\W+");
- string ErrorMessage;
- Thread InitPrint;
- private bool AutoCut;
- public Make_MidBoxWeigh()
- {
- InitializeComponent();
- CheckForIllegalCrossThreadCalls = false;
- }
- Engine engine;
- private void InPrint()
- {
- try
- {
- engine = new Engine(true);
- }
- catch (Exception ex)
- {
- OperateResult.AppendText(ex.Message + ex.StackTrace, Color.Red);
- }
- }
- 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();
- //设置锁定工单
- LockMakeCode.GetMakeCodeCtl(ma_code);
- ma_code.SetLockCheckBox(LockMakeCode);
- //工单号放大镜配置
- ma_code.TableName = "make left join product on ma_prodcode=pr_code";
- ma_code.SelectField = "ma_code # 工单号,pr_code # 产品编号,pr_spec # 产品规格";
- ma_code.FormName = Name;
- ma_code.SetValueField = new string[] { "ma_code", "pr_code", "pr_spec" };
- ma_code.Condition = "ma_statuscode='STARTED'";
- ma_code.DbChange += Ma_code_DbChange;
- StartWeight.PerformClick();
- dh = SystemInf.dh;
- StepCount.StepCode = User.CurrentStepCode;
- StepCount.Source = User.UserSourceCode;
- StepCount.LineCode = User.UserLineCode;
- StepCount.Dh = dh;
- StepCount.Start();
- AutoPrint.Checked = false;
- try
- {
- if (File.Exists("MidBoxWeigh" + ".txt"))
- {
- File.Delete("MidBoxWeigh" + ".txt");
- }
- StreamWriter sw = File.AppendText("MidBoxWeigh" + ".txt");
- sw.WriteLine(sncode.Handle);
- sw.WriteLine(weight.Handle);
- sw.Close();
- }
- catch (Exception) { }
- }
- private void Ma_code_DbChange(object sender, EventArgs e)
- {
- Dbfind = ma_code.ReturnData;
- BaseUtil.SetFormValue(this.Controls, Dbfind);
- //获取工单的其他信息
- sql.Clear();
- sql.Append("select ma_code,ma_prodcode as pr_code , pr_detail as pr_spec,");
- sql.Append("pr_spec,ma_qty - nvl(mcd_inqty, 0) mcd_remainqty from make left join makecraftdetail_view on ");
- sql.Append("mcd_maid=ma_id left join product on pr_code=ma_prodcode where ma_code='" + ma_code.Text + "' and mcd_stepcode='" + User.CurrentStepCode + "'");
- dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
- if (dt.Rows.Count > 0)
- {
- BaseUtil.SetFormValue(this.Controls, dt);
- }
- }
- private void sncode_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter)
- {
- if (LogicHandler.CheckStepAttribute(Tag.ToString(), User.UserSourceCode, out ErrorMessage))
- {
- string oMakeCode = "";
- string oMsID = "";
- dt = (DataTable)dh.ExecuteSql("select ms_sncode from makeserial where ms_imei1='" + sncode.Text + "' order by ms_id desc", "select");
- if (dt.Rows.Count > 0)
- {
- sncode.Text = dt.Rows[0]["ms_sncode"].ToString();
- }
- else
- {
- dt = (DataTable)dh.ExecuteSql("select ms_sncode from makeserial where ms_imei2='" + sncode.Text + "' order by ms_id desc", "select");
- if (dt.Rows.Count > 0)
- {
- sncode.Text = dt.Rows[0]["ms_sncode"].ToString();
- }
- }
- if (LogicHandler.CheckStepSNAndMacode(ma_code.Text, User.UserSourceCode, sncode.Text, User.UserCode, out oMakeCode, out oMsID, out ErrorMessage))
- {
- //string nextstepcode = dh.getFieldDataByCondition("makeserial", "ms_nextstepcode", "ms_id='" + oMsID + "'").ToString();
- //if (nextstepcode != User.CurrentStepCode)
- //{
- // OperateResult.AppendText("<<序列号:" + sncode.Text + "下一工序是" + nextstepcode + ",不是当前岗位的工序\n", Color.Red, sncode);
- // return;
- //}
- sql.Clear();
- sql.Append("select ms_makecode ma_code,ma_qty,ma_salecode,pr_detail pr_spec,pr_colorboxunit,pr_code,pr_colorboxgw,");
- sql.Append("pr_colorboxunit,pr_midboxmaxweight pr_colorboxmaxw,pr_midboxminweight pr_colorboxminw,nvl(PR_CHECKCOLORBOXW,'0') PR_CHECKCOLORBOXW,nvl(pr_sendchecktype,'LineCode')");
- sql.Append("pr_sendchecktype from makeserial left join make on ms_makecode=ma_code left join product on ");
- sql.Append("ms_prodcode=pr_code where ms_sncode='" + sncode.Text + "' order by ms_id desc");
- dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
- if (dt.Rows.Count > 0)
- {
- //重量的临时变量
- pr_code.Text = dt.Rows[0]["pr_code"].ToString();
- 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();
- string ma_salecode_text = dt.Rows[0]["ma_salecode"].ToString();
- string PR_CHECKCOLORBOXW = dt.Rows[0]["PR_CHECKCOLORBOXW"].ToString();
- //赋值重量单位
- Weight = double.Parse(_weight == "" ? "0" : _weight);
- MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
- MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
- if (locksalecode.Checked && ma_salecode.Text != "" && ma_salecode.Text != ma_salecode_text)
- {
- OperateResult.AppendText(">>序列号" + sncode.Text + "所属订单号" + ma_salecode_text + "与界面订单号" + ma_salecode.Text + "不同,无法采集\n", Color.Red, sncode);
- return;
- }
- ma_salecode.Text = dt.Rows[0]["ma_salecode"].ToString();
- BaseUtil.SetFormValue(this.Controls, dt);
- if (Weight - MinWeight == MaxWeight - Weight)
- pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_colorboxunit"].ToString();
- else
- pr_colorboxgw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_colorboxunit"].ToString();
- //重量信息若需要检测则必须符合检测要求才更新
- double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text.Replace("kg", "").Replace("g", "").Trim());
- if (PR_CHECKCOLORBOXW != "0")
- {
- if (MinWeight == 0 || MaxWeight == 0)
- {
- OperateResult.AppendText(">>产品" + pr_code.Text + "未维护中箱重量范围\n", Color.Red);
- return;
- }
- if ((ActualWeight >= MinWeight) && (ActualWeight <= MaxWeight))
- {
- OperateResult.AppendText(">>中箱" + sncode.Text + "重量检测检测合格\n", Color.Green);
- LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "中箱称重:" + weight.Text, "称量合格", sncode.Text, "");
- LogicHandler.RecordProdWeight(sncode.Text, "SN", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName);
- dh.ExecuteSql("update makeserial set ms_midboxweight='" + ActualWeight + "' where ms_id='" + oMsID + "'", "update");
- }
- else
- {
- OperateResult.AppendText(">>中箱" + sncode.Text + "重量检测未通过\n", Color.Red);
- LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "中箱称重:" + weight.Text, "称量不合格", sncode.Text, "");
- LogicHandler.RecordProdWeight(sncode.Text, "SN", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName);
- return;
- }
- }
- else
- {
- LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "中箱称重:" + weight.Text, "称量完成", sncode.Text, "");
- LogicHandler.RecordProdWeight(sncode.Text, "SN", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName);
- dh.ExecuteSql("update makeserial set ms_midboxweight='" + ActualWeight + "' where ms_id='" + oMsID + "'", "update");
- }
- 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);
- WeighRecord.Items[this.WeighRecord.Items.Count - 1].EnsureVisible();
- OperateResult.AppendText(">>中箱" + sncode.Text + "称重完成\n", Color.Green);
- //显示最近的三个称量记录
- if (LogicHandler.SetStepResult(ma_code.Text, User.UserSourceCode, sncode.Text, "中箱称量", "称量合格", User.UserCode, out ErrorMessage))
- {
- //提示正确返回时传递的信息
- if (ErrorMessage.Contains("AFTERSUCCESS"))
- OperateResult.AppendText(">>" + ErrorMessage + "\n");
- LoadCheckQTY();
- //记录送检前的最后一个序列号
- LastSncode = sncode.Text;
- if (AutoPrint.Checked)
- {
- OperateResult.AppendText(">>打印条码号" + sncode.Text + "\n", Color.Green);
- Confirm.PerformClick();
- }
- Remark.Clear();
- }
- else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red);
- //if (CollectRemark.Checked && Remark.Text != "")
- //{
- // LogicHandler.CollectRemarkInf(sncode.Text, "", "", oMakeCode, pr_code.Text, Remark.Text);
- //}
- LoadCollectedNum();
- sncode.Clear();
- }
- else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, sncode);
- }
- else OperateResult.AppendText(">>" + ErrorMessage + "\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
- {
- int len = serialPort1.BytesToRead;
- Byte[] readBuffer = new Byte[len];
- serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
- string weigh = Encoding.Default.GetString(readBuffer);
- Remark.AppendText(weigh + "\n");
- if (weigh != "")
- {
- weight.Text = re.Match(weigh).Value;
- }
- }
- catch (Exception)
- {
- GetData = false;
- }
- }
- }
- catch (IOException ex) { MessageBox.Show(ex.Message); }
- }
- else OperateResult.AppendText(">>端口已被占用,请关闭其他窗口\n", Color.Red);
- }
- }
- private void 中箱称重_SizeChanged(object sender, EventArgs e)
- {
- weight.Location = new Point(weight_label.Location.X + weight_label.Size.Width - 10, weight_label.Location.Y - 20);
- 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();
- InitPrint.Abort();
- if (serialPort1.IsOpen)
- {
- GetData = false;
- serialPort1.Close();
- SystemInf.OpenPort.Remove(serialPort1.PortName);
- thread.Interrupt();
- }
- //thread.Abort();
- }
- private void Confirm_Click(object sender, EventArgs e)
- {
- if (PrintLabel.Items.Count != 0)
- {
- string oErrorMessage;
- if (!Print.BarTender(Tag.ToString(), ref engine, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), PrinterList.Text, sncode.Text, int.Parse(PrintNum.Text), ma_code.Text, pr_code.Text, "中箱标", "0", out oErrorMessage))
- {
- OperateResult.AppendText(oErrorMessage + "\n", Color.Red);
- }
- }
- else
- {
- OperateResult.AppendText(">>产品" + pr_code.Text + "未维护打印标签\n", Color.Red);
- }
- }
- Thread threadWatch = null; //负责监听客户端的线程
- Socket socketWatch = null; //负责监听客户端的套接字
- Socket socConnection;
- 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)
- {
- if (serialPort1.IsOpen)
- {
- GetData = false;
- serialPort1.Close();
- SystemInf.OpenPort.Remove(serialPort1.PortName);
- thread.Abort();
- }
- }
- private void pr_code_TextChanged(object sender, EventArgs e)
- {
- LoadCheckQTY();
- DataTable _dt = (DataTable)dh.ExecuteSql("select la_id,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_url";
- PrintLabel.ValueMember = "la_id";
- }
- private void SendCheck_Click(object sender, EventArgs e)
- {
- sql.Clear();
- sql.Append("update oqcbatch set ob_status='UNCHECK',ob_breakingdate=sysdate 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(LastSncode, ma_code.Text, User.UserSourceCode, "手动送检", "手动送检成功", User.UserCode);
- //记录操作日志
- LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.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' ";
- }
- if (pr_sendchecktype.Text == "SaleCode")
- {
- condition += " and ob_salecode='" + ma_salecode.Text + "'";
- }
- 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);
- if (AutoCut)
- {
- OperateResult.AppendText(">>批次" + ob_checkno.Text + "自动断批\n", Color.Green);
- }
- }
- }
- 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;
- }
- }
- 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);
- }
- }
- private void RefreshWeigh_Click(object sender, EventArgs e)
- {
- sql.Clear();
- sql.Append("select pr_colorboxgw,pr_midboxmaxweight pr_colorboxmaxw,pr_midboxminweight pr_colorboxminw,pr_colorboxunit from product where pr_code='" + pr_code.Text + "'");
- DataTable 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 = double.Parse(_weight == "" ? "0" : _weight);
- MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
- MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
- if (Weight - MinWeight == MaxWeight - Weight)
- pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_colorboxunit"].ToString();
- else
- pr_colorboxgw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_colorboxunit"].ToString();
- }
- }
- private void LoadCollectedNum()
- {
- dt = (DataTable)dh.ExecuteSql("select mcd_inqty,ma_qty-mcd_inqty mcd_remainqty from make left join makecraftdetail_view on mcd_macode=ma_code where ma_code='" + ma_code.Text + "' and mcd_stepcode='" + User.CurrentStepCode + "'", "select");
- BaseUtil.SetFormValue(Controls, dt);
- }
- private void StepCount_Load(object sender, EventArgs e)
- {
- }
- }
- }
|