Make_SplitBoard.cs 5.9 KB

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