| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.IO.Ports;
- using System.Linq;
- 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;
- namespace UAS_MES.Packing
- {
- public partial class Packing_ProdWeightSet : Form
- {
- AutoSizeFormClass asc = new AutoSizeFormClass();
- DataTable Dbfind;//存储工单号查询出来的信息
- //创建串口实例
- SerialPort serialPort1 = new SerialPort();
- DataHelper dh;
- Thread thread;
- //true的时候表示从串口读取数据
- bool GetData = true;
- int samplesCount = 0;
- string maxValue = "";//存储最大值
- string minValue = "";//存储最小值
- public Packing_ProdWeightSet()
- {
- InitializeComponent();
- }
- private void Packing_ProdWeightSet_Load(object sender, EventArgs e)
- {
- asc.controllInitializeSize(this);
- ComList.Text = BaseUtil.GetCacheData("PortName").ToString();
- BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
- //工单号放大镜配置
- ma_code.TableName = "make left join product on ma_prodcode=pr_code";
- ma_code.SelectField = "ma_code # 工单号,pr_code # 产品编号,pr_detail # 产品名称,pr_spec # 规格,ma_qty # 工单数量";
- ma_code.FormName = Name;
- ma_code.SetValueField = new string[] { "ma_code,pr_code,pr_detail,pr_spec" };
- ma_code.DbChange += Ma_code_DbChange;
- dh = new DataHelper();
- //开始称重
- startWeigh.PerformClick();
- }
- private void Ma_code_DbChange(object sender, EventArgs e)
- {
- Dbfind = ma_code.ReturnData;
- BaseUtil.SetFormValue(this.Controls, Dbfind);
- //查询重量设置采样个数
- try
- {
- samplesCount = int.Parse(dh.GetConfig("重量设置采样个数", "MESSetting").ToString());
- }
- catch (Exception ess)
- {
- //如果没维护的话默认是10
- samplesCount = 10;
- }
- OperateResult.AppendText("<<重量设置需采样个数为"+samplesCount+"\n", Color.Black);
- //清空称量记录
- showResult.Items.Clear();
- }
- private void recordResult(int index,string palletcode, string weigh, string time)
- {
- //创建一个item
- ListViewItem lvi = new ListViewItem();
- //分条赋值
- lvi.SubItems.Add(index+"");
- lvi.SubItems.Add(palletcode);
- lvi.SubItems.Add(weigh);
- lvi.SubItems.Add(time);
- //添加结果的信息进去
- showResult.Items.Add(lvi);
- //更新已称重量最大值最小值
- if (index > 1)
- {
- maxValue = double.Parse(weigh) > double.Parse(maxValue) ? weigh : maxValue;
- minValue = double.Parse(weigh) < double.Parse(minValue) ? weigh : minValue;
- }
- else
- {
- maxValue = weigh;
- minValue = weigh;
- }
- }
- 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 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
- MessageBox.Show("端口已被占用,请关闭其他窗口");
- }
- }
- //停止称重
- private void stopWeigh_Click(object sender, EventArgs e)
- {
- GetData = false;
- SystemInf.OpenPort.Remove(serialPort1.PortName);
- serialPort1.Close();
- }
- private void confirm_Click(object sender, EventArgs e)
- {
- //按确认更新产品重量
- if (ma_code.Text=="")
- {
- OperateResult.AppendText("<<请先选择工单\n", Color.Red);
- return;
- }
- //判断是否达到已称数量
- if (showResult.Items.Count<samplesCount)
- {
- OperateResult.AppendText("<<采样个数不足\n", Color.Red);
- return;
- }
- //更新彩盒重量最大值最小值
- dh.ExecuteSql("update product set PR_COLORBOXMAXW ='"+maxValue+"', PR_COLORBOXMINW = '"+minValue+"' where pr_code='"+pr_code.Text+"'", "update");
- OperateResult.AppendText("<<重量设置成功,最大值"+maxValue+"最小值"+minValue+"\n", Color.Green);
- }
- private void sncode_KeyDown(object sender, KeyEventArgs e)
- {
- //按下了enter键
- if (e.KeyCode == Keys.Enter)
- {
- if (sncode.Text == "")
- {
- OperateResult.AppendText("<<输入不能为空\n", Color.Red);
- return;
- }
- if (weight.Text == "")
- {
- OperateResult.AppendText("<<未读取到重量信息\n", Color.Red);
- return;
- }
- if (double.Parse(weight.Text) == 0)
- {
- OperateResult.AppendText("<<重量不能等于0\n", Color.Red, sncode);
- return;
- }
- //验证彩盒是否与工单对应
- if (!dh.CheckExist("makeserial","where ms_makecode='"+ma_code.Text+"' and ms_sncode = '"+sncode.Text+"'"))
- {
- OperateResult.AppendText("<<序列号"+sncode.Text+"不在工单"+ma_code.Text+"中\n", Color.Red, sncode);
- return;
- }
- //记录重量
- recordResult(showResult.Items.Count+1,sncode.Text, weight.Text, System.DateTime.Now.ToString());
- }
- }
- private void Packing_ProdWeightSet_AutoSizeChanged(object sender, EventArgs e)
- {
- }
- private void Packing_ProdWeightSet_SizeChanged(object sender, EventArgs e)
- {
- asc.controlAutoSize(this);
- }
- }
- }
|