Make_ColorBoxWeigh.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Data;
  4. using System.IO;
  5. using System.IO.Ports;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading;
  9. using System.Windows.Forms;
  10. using UAS_MES.DataOperate;
  11. using UAS_MES.Entity;
  12. using UAS_MES.PublicMethod;
  13. using System.Drawing;
  14. using LabelManager2;
  15. namespace UAS_MES.Make
  16. {
  17. public partial class Make_ColorBoxWeigh : Form
  18. {
  19. AutoSizeFormClass asc = new AutoSizeFormClass();
  20. ApplicationClass lbl;
  21. DataHelper dh;
  22. DataTable dt;
  23. LogStringBuilder sql = new LogStringBuilder();
  24. //启用线程进行称重数据读取
  25. Thread thread;
  26. SerialPort serialPort1 = new SerialPort();
  27. //称量的标准重量
  28. int Weight;
  29. //最大重量
  30. int MaxWeight;
  31. //最小重量
  32. int MinWeight;
  33. //是否通过串口获取数据
  34. bool GetData = true;
  35. public Make_ColorBoxWeigh()
  36. {
  37. InitializeComponent();
  38. }
  39. private void 彩盒上料打印_Load(object sender, EventArgs e)
  40. {
  41. lbl = new ApplicationClass();
  42. CheckForIllegalCrossThreadCalls = false;
  43. dh = new DataHelper();
  44. asc.controllInitializeSize(this);
  45. }
  46. private void sncode_KeyDown(object sender, KeyEventArgs e)
  47. {
  48. if (e.KeyCode == Keys.Enter)
  49. {
  50. sql.Clear();
  51. sql.Append("select ms_makecode,mcd_okqty,ma_qty,ma_qty-mcd_okqty as mcd_waitqty,ma_salecode,pr_detail,pr_colorboxunit,pr_code,pr_colorboxgw,");
  52. sql.Append("pr_colorboxunit,pr_colorboxmaxw,pr_colorboxminw from makeserial left join make on ms_makecode=ma_code left join product on ");
  53. sql.Append("ms_prodcode=pr_code left join makecraftdetail on ms_makecode=mcd_macode where ms_sncode='" + sncode.Text + "'");
  54. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  55. if (dt.Rows.Count > 0)
  56. {
  57. //重量的临时变量
  58. string _weight = dt.Rows[0]["pr_colorboxgw"].ToString();
  59. string _maxweight = dt.Rows[0]["pr_colorboxmaxw"].ToString();
  60. string _minweight = dt.Rows[0]["pr_colorboxminw"].ToString();
  61. //赋值重量单位
  62. Weight = int.Parse(_weight == "" ? "0" : _weight);
  63. MaxWeight = int.Parse(_maxweight == "" ? "0" : _maxweight);
  64. MinWeight = int.Parse(_minweight == "" ? "0" : _minweight);
  65. BaseUtil.SetFormValue(this.Controls, dt);
  66. if (Weight - MinWeight == MaxWeight - Weight)
  67. {
  68. pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight);
  69. }
  70. else
  71. {
  72. pr_colorboxgw.Text = MinWeight + "-" + MaxWeight;
  73. }
  74. string ErrorMessage;
  75. bool ifFirst;
  76. if (LogicHandler.CheckCurrentStepAndIfFirst(sncode.Text, ms_makecode.Text, User.UserSourceCode, "Make!ColorBoxWeigh", out ifFirst, out ErrorMessage))
  77. {
  78. string YN = dh.GetConfig("BatchNumber", "MESSetting").ToString();
  79. //表示需要验证判断md_qty 是否大于等于md_baseqty
  80. if (YN != "0")
  81. {
  82. sql.Clear();
  83. sql.Append("select wm_concat(md_prodcode) ,count(1) cn from makeprepare left join ");
  84. sql.Append("makepreparedetail on mp_id=md_mpid where mp_makecode='" + ms_makecode.Text + "' and md_qty<md_baseqty");
  85. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  86. if (dt.Rows[0]["CN"].ToString() != "0")
  87. {
  88. OperateResult.AppendText(">>物料:" + pr_code.Text + " ,岗位用料不足\n", Color.Red);
  89. return;
  90. }
  91. }
  92. //如果未打开串口设置为0
  93. int ActualWeight = int.Parse(weight.Text == "" ? "0" : weight.Text);
  94. if (ActualWeight > MinWeight && ActualWeight < MaxWeight)
  95. {
  96. OperateResult.AppendText(">>检测合格\n", Color.Green);
  97. //显示最近的三个称量记录
  98. Temp3.Text = Temp2.Text;
  99. Temp2.Text = Temp1.Text;
  100. Temp1.Text = sncode.Text;
  101. }
  102. else
  103. OperateResult.AppendText(">>检测未通过\n", Color.Red);
  104. }
  105. else
  106. {
  107. OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red);
  108. }
  109. sncode.Text = "";
  110. }
  111. else
  112. {
  113. OperateResult.AppendText(">>序列号不存在\n", Color.Red);
  114. return;
  115. }
  116. }
  117. }
  118. private void getSerialData()
  119. {
  120. if (serialPort1.IsOpen)
  121. {
  122. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  123. {
  124. SystemInf.OpenPort.Add(serialPort1.PortName);
  125. try
  126. {
  127. while (GetData)
  128. {
  129. try
  130. {
  131. weight.Text = Regex.Replace(serialPort1.ReadLine(), "\\D+", "");
  132. }
  133. catch (Exception)
  134. {
  135. GetData = false;
  136. }
  137. }
  138. }
  139. catch (IOException ex) { MessageBox.Show(ex.Message); }
  140. }
  141. else
  142. MessageBox.Show("端口已被占用,请关闭其他窗口");
  143. }
  144. }
  145. private void 彩盒称重_SizeChanged(object sender, EventArgs e)
  146. {
  147. asc.controlAutoSize(this);
  148. }
  149. private void Clean_Click(object sender, EventArgs e)
  150. {
  151. OperateResult.Clear();
  152. }
  153. //关闭窗口的时候停止进程读取串口数据
  154. private void 彩盒称重_FormClosing(object sender, FormClosingEventArgs e)
  155. {
  156. if (serialPort1.IsOpen)
  157. {
  158. GetData = false;
  159. serialPort1.Close();
  160. SystemInf.OpenPort.Remove(serialPort1.PortName);
  161. thread.Interrupt();
  162. thread.Join();
  163. }
  164. }
  165. private void Confirm_Click(object sender, EventArgs e)
  166. {
  167. Print.CodeSoft(lbl, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, sncode.Text);
  168. }
  169. private void Cancel_Click(object sender, EventArgs e)
  170. {
  171. string Error = "";
  172. }
  173. private void StartWeight_Click(object sender, EventArgs e)
  174. {
  175. thread = new Thread(getSerialData);
  176. try
  177. {
  178. serialPort1.PortName = ComList.Text;
  179. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  180. serialPort1.Open();
  181. GetData = true;
  182. thread.Start();
  183. }
  184. catch (Exception mes) { MessageBox.Show(mes.Message); }
  185. }
  186. private void StopWeight_Click(object sender, EventArgs e)
  187. {
  188. GetData = false;
  189. SystemInf.OpenPort.Remove(serialPort1.PortName);
  190. serialPort1.Close();
  191. }
  192. private void pr_code_TextChanged(object sender, EventArgs e)
  193. {
  194. PrintLabel.DataSource = dh.ExecuteSql("select pl_labelcode,pl_labelname from productlabel where pl_prodcode='" + pr_code.Text + "'", "select");
  195. PrintLabel.DisplayMember = "pl_labelname";
  196. PrintLabel.ValueMember = "pl_labelcode";
  197. }
  198. private void pr_colorboxunit_Click(object sender, EventArgs e)
  199. {
  200. }
  201. }
  202. }