Make_ColorBoxWeigh.cs 9.2 KB

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