Make_SplitBoard.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Data;
  3. using System.IO.Ports;
  4. using System.Windows.Forms;
  5. using UAS_MES_NEW.DataOperate;
  6. using UAS_MES_NEW.Entity;
  7. using UAS_MES_NEW.PublicMethod;
  8. namespace UAS_MES_NEW.Make
  9. {
  10. public partial class Make_SplitBoard : Form
  11. {
  12. DataHelper dh = null;
  13. SerialPort serialPort1 = new SerialPort();
  14. DataTable Dbfind;
  15. public Make_SplitBoard()
  16. {
  17. InitializeComponent();
  18. }
  19. private void 分板作业_Load(object sender, EventArgs e)
  20. {
  21. dh = SystemInf.dh;
  22. ma_code.TableName = "make";
  23. ma_code.SelectField = "ma_code # 工单号";
  24. ma_code.FormName = Name;
  25. ma_code.DBTitle = "工单查询";
  26. ma_code.SetValueField = new string[] { "ma_code" };
  27. ma_code.Condition = "";
  28. ma_code.DbChange += nr_rule_DBChange;
  29. serialPort1.DataReceived += SerialPort1_DataReceived;
  30. try
  31. {
  32. ComPort.Text = BaseUtil.GetCacheData("ComPort").ToString();
  33. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  34. }
  35. catch (Exception ex) { MessageBox.Show(ex.Message); }
  36. }
  37. private void nr_rule_DBChange(object sender, EventArgs e)
  38. {
  39. BaseUtil.SetFormValue(this.Controls, Dbfind);
  40. }
  41. private void StartWatch_Click(object sender, EventArgs e)
  42. {
  43. BaseUtil.SetCacheData("ComPort", ComPort.Text);
  44. BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  45. if (!dh.CheckExist("Make", "ma_code='" + ma_code.Text + "'"))
  46. {
  47. OperateResult.AppendText("工单号不能为空\n");
  48. return;
  49. }
  50. string ExitConfirm = MessageBox.Show(this, "确认计数器是否置为0?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  51. if (ExitConfirm != "Yes")
  52. {
  53. return;
  54. }
  55. //设置按钮不可点击
  56. StartWatch.Enabled = false;
  57. ma_code.Enabled = false;
  58. StopWatch.Enabled = true;
  59. try
  60. {
  61. serialPort1.PortName = ComPort.Text;
  62. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  63. serialPort1.Open();
  64. Timer.Start();
  65. }
  66. catch (Exception mes)
  67. {
  68. if (BaudRate.Text == "" || BaudRate.Text == "")
  69. OperateResult.AppendText(">>请先维护波特率和串口\n");
  70. else
  71. OperateResult.AppendText(">>" + mes.Message + "\n");
  72. }
  73. OperateResult.AppendText("开始执行监控\n");
  74. }
  75. double lastqty = 0;
  76. private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  77. {
  78. int len = serialPort1.BytesToRead;
  79. Byte[] readBuffer = new Byte[len];
  80. serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
  81. string data = BitConverter.ToString(readBuffer, 0, readBuffer.Length).Replace("-", "");
  82. if (data != "")
  83. {
  84. double qty = Convert.ToInt32(data.Substring(6, 8), 16);
  85. if (lastqty > qty)
  86. {
  87. lastqty = 0;
  88. }
  89. NowQTY.Text = qty.ToString();
  90. string ma_endqty = dh.getFieldDataByCondition("make", "nvl(ma_endqty,0)", "ma_code='" + ma_code.Text + "'").ToString();
  91. //dh.ExecuteSql("update make set ma_endqty=nvl(ma_endqty,0)+'" + (qty - lastqty) + "' where ma_code='" + ma_code.Text + "' ", "update");
  92. if (qty - lastqty > 0) {
  93. dh.ExecuteSql("insert into makehourcount(mhc_id,mhc_macode,mhc_indate,mhc_qty,mhc_sourcecode,mhc_inman,mhc_linecode,mhc_stepcode,mhc_pcbcount)" +
  94. "values(makehourcount_seq.nextval,'" + ma_code.Text + "',sysdate,'" + (qty - lastqty) + "','" + User.UserSourceCode + "','" + User.UserCode + "','" + User.UserLineCode + "','" + User.CurrentStepCode + "','" + pr_pcbacount.Value + "')", "insert");
  95. }
  96. lastqty = qty;
  97. }
  98. }
  99. private void StopWatch_Click(object sender, EventArgs e)
  100. {
  101. XmlWatcher.EnableRaisingEvents = false;
  102. StartWatch.Enabled = true;
  103. ma_code.Enabled = true;
  104. StopWatch.Enabled = false;
  105. OperateResult.AppendText("停止执行监控\n");
  106. }
  107. private void Timer_Tick(object sender, EventArgs e)
  108. {
  109. if (serialPort1.IsOpen)
  110. {
  111. byte[] data = HexStringToBytes("01 03 00 01 00 06 94 08");
  112. serialPort1.Write(data, 0, data.Length);
  113. }
  114. }
  115. private byte[] HexStringToBytes(string hs)//十六进制字符串转byte
  116. {
  117. string a = hs.Replace(" ", "");
  118. int bytelength = 0;
  119. if (a.Length % 2 == 0)
  120. {
  121. bytelength = a.Length / 2;
  122. }
  123. else
  124. {
  125. bytelength = a.Length / 2 + 1;
  126. }
  127. byte[] b = new byte[bytelength];
  128. //逐个字符变为16进制字节数据
  129. for (int i = 0; i < bytelength; i++)
  130. {
  131. if (i == bytelength - 1)
  132. {
  133. b[i] = Convert.ToByte(a.Substring(i * 2), 16);
  134. }
  135. else
  136. {
  137. b[i] = Convert.ToByte(a.Substring(i * 2, 2), 16);
  138. }
  139. }
  140. //按照指定编码将字节数组变为字符串
  141. return b;
  142. }
  143. }
  144. }