| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- using System;
- using System.Data;
- using System.IO.Ports;
- using System.Windows.Forms;
- using UAS_MES_NEW.DataOperate;
- using UAS_MES_NEW.Entity;
- using UAS_MES_NEW.PublicMethod;
- namespace UAS_MES_NEW.Make
- {
- public partial class Make_SplitBoard : Form
- {
- DataHelper dh = null;
- SerialPort serialPort1 = new SerialPort();
- DataTable Dbfind;
- public Make_SplitBoard()
- {
- InitializeComponent();
- }
- private void 分板作业_Load(object sender, EventArgs e)
- {
- dh = SystemInf.dh;
- ma_code.TableName = "make";
- ma_code.SelectField = "ma_code # 工单号";
- ma_code.FormName = Name;
- ma_code.DBTitle = "工单查询";
- ma_code.SetValueField = new string[] { "ma_code" };
- ma_code.Condition = "";
- ma_code.DbChange += nr_rule_DBChange;
- serialPort1.DataReceived += SerialPort1_DataReceived;
- try
- {
- ComPort.Text = BaseUtil.GetCacheData("ComPort").ToString();
- BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
- }
- catch (Exception ex) { MessageBox.Show(ex.Message); }
- }
- private void nr_rule_DBChange(object sender, EventArgs e)
- {
- BaseUtil.SetFormValue(this.Controls, Dbfind);
- }
- private void StartWatch_Click(object sender, EventArgs e)
- {
- BaseUtil.SetCacheData("ComPort", ComPort.Text);
- BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
- if (!dh.CheckExist("Make", "ma_code='" + ma_code.Text + "'"))
- {
- OperateResult.AppendText("工单号不能为空\n");
- return;
- }
- string ExitConfirm = MessageBox.Show(this, "确认计数器是否置为0?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
- if (ExitConfirm != "Yes")
- {
- return;
- }
- //设置按钮不可点击
- StartWatch.Enabled = false;
- ma_code.Enabled = false;
- StopWatch.Enabled = true;
- try
- {
- serialPort1.PortName = ComPort.Text;
- serialPort1.BaudRate = int.Parse(BaudRate.Text);
- serialPort1.Open();
- Timer.Start();
- }
- catch (Exception mes)
- {
- if (BaudRate.Text == "" || BaudRate.Text == "")
- OperateResult.AppendText(">>请先维护波特率和串口\n");
- else
- OperateResult.AppendText(">>" + mes.Message + "\n");
- }
- OperateResult.AppendText("开始执行监控\n");
- }
- double lastqty = 0;
- private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- int len = serialPort1.BytesToRead;
- Byte[] readBuffer = new Byte[len];
- serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
- string data = BitConverter.ToString(readBuffer, 0, readBuffer.Length).Replace("-", "");
- if (data != "")
- {
- double qty = Convert.ToInt32(data.Substring(6, 8), 16);
- if (lastqty > qty)
- {
- lastqty = 0;
- }
- NowQTY.Text = qty.ToString();
- string ma_endqty = dh.getFieldDataByCondition("make", "nvl(ma_endqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
- //dh.ExecuteSql("update make set ma_endqty=nvl(ma_endqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
- if (qty - lastqty > 0) {
- dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_qty,mhc_sourcecode,mhc_inman,mhc_linecode,mhc_stepcode,mhc_pcbcount)" +
- "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + (qty - lastqty) + "','" + User.UserSourceCode + "','" + User.UserCode + "','" + User.UserLineCode + "','" + User.CurrentStepCode + "','" + pr_pcbacount.Value + "')", "insert");
- }
-
- lastqty = qty;
- }
- }
- private void StopWatch_Click(object sender, EventArgs e)
- {
- XmlWatcher.EnableRaisingEvents = false;
- StartWatch.Enabled = true;
- ma_code.Enabled = true;
- StopWatch.Enabled = false;
- OperateResult.AppendText("停止执行监控\n");
- }
- private void Timer_Tick(object sender, EventArgs e)
- {
- if (serialPort1.IsOpen)
- {
- byte[] data = HexStringToBytes("01 03 00 01 00 06 94 08");
- serialPort1.Write(data, 0, data.Length);
- }
- }
- private byte[] HexStringToBytes(string hs)//十六进制字符串转byte
- {
- string a = hs.Replace(" ", "");
- int bytelength = 0;
- if (a.Length % 2 == 0)
- {
- bytelength = a.Length / 2;
- }
- else
- {
- bytelength = a.Length / 2 + 1;
- }
- byte[] b = new byte[bytelength];
- //逐个字符变为16进制字节数据
- for (int i = 0; i < bytelength; i++)
- {
- if (i == bytelength - 1)
- {
- b[i] = Convert.ToByte(a.Substring(i * 2), 16);
- }
- else
- {
- b[i] = Convert.ToByte(a.Substring(i * 2, 2), 16);
- }
- }
- //按照指定编码将字节数组变为字符串
- return b;
- }
- }
- }
|