Make_CartonBoxWeigh.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using LabelManager2;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.IO.Ports;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12. using UAS_MES.DataOperate;
  13. using UAS_MES.Entity;
  14. using UAS_MES.PublicMethod;
  15. namespace UAS_MES.Make
  16. {
  17. public partial class Make_CartonBoxWeigh : Form
  18. {
  19. AutoSizeFormClass asc = new AutoSizeFormClass();
  20. DataHelper dh;
  21. DataTable dt;
  22. LogStringBuilder sql = new LogStringBuilder();
  23. ApplicationClass lbl;
  24. Thread thread;
  25. //创建串口实例
  26. SerialPort serialPort1 = new SerialPort();
  27. //true的时候表示从串口读取数据
  28. bool GetData = true;
  29. //箱内总数
  30. int pa_totalqty;
  31. //称量的标准重量
  32. int Weight;
  33. //最大重量
  34. int MaxWeight;
  35. //最小重量
  36. int MinWeight;
  37. DataTable ListC = new DataTable();
  38. public Make_CartonBoxWeigh()
  39. {
  40. InitializeComponent();
  41. }
  42. private void 卡通箱称重_Load(object sender, EventArgs e)
  43. {
  44. CheckForIllegalCrossThreadCalls = false;
  45. asc.controllInitializeSize(this);
  46. dh = new DataHelper();
  47. lbl = new ApplicationClass();
  48. ComList.Text = Properties.Settings.Default.PortName;
  49. BaudRate.Text = Properties.Settings.Default.BaudRate;
  50. }
  51. private void 卡通箱称重_SizeChanged(object sender, EventArgs e)
  52. {
  53. asc.controlAutoSize(this);
  54. }
  55. private void sncode_KeyDown(object sender, KeyEventArgs e)
  56. {
  57. if (e.KeyCode == Keys.Enter)
  58. {
  59. //根据箱号查询表单数据
  60. pd_barcode.Text = dh.getFieldDataByCondition("makeserial left join package on ms_prodcode=pa_prodcode", "ms_sncode", "pa_outboxcode='" + outboxcode.Text + "'").ToString();
  61. sql.Clear();
  62. sql.Append("select pa_totalqty,pd_barcode,ma_code,mcd_okqty,ma_qty,ma_qty-mcd_okqty as mcd_waitqty,ma_salecode,pr_detail,pr_cartonunit,pr_code,pr_cartongw,");
  63. sql.Append("pr_cartonmaxw,pr_cartonminw from package left join product on pr_code=pa_prodcode left join make on ma_prodcode=pr_code left join makecraftdetail ");
  64. sql.Append("on mcd_macode=ma_code left join packagedetail on pd_outboxcode=pa_outboxcode where pa_outboxcode='" + outboxcode.Text + "' and mcd_stepcode='" + User.CurrentStepCode + "'");
  65. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  66. //填充Form的值
  67. BaseUtil.SetFormValue(this.Controls, dt);
  68. //填充打印文件选项的DataGridView
  69. if (dt.Rows.Count > 0)
  70. {
  71. string ErrorMessage;
  72. //重量的临时变量
  73. string _weight = dt.Rows[0]["pr_cartongw"].ToString();
  74. string _maxweight = dt.Rows[0]["pr_cartonminw"].ToString();
  75. string _minweight = dt.Rows[0]["pr_cartonmaxw"].ToString();
  76. Weight = int.Parse(_weight == "" ? "0" : _weight);
  77. MaxWeight = int.Parse(_maxweight == "" ? "0" : _maxweight);
  78. MinWeight = int.Parse(_minweight == "" ? "0" : _minweight);
  79. pa_totalqty = int.Parse(dt.Rows[0]["pa_totalqty"].ToString());
  80. bool ifFirst;
  81. if (LogicHandler.CheckCurrentStepAndIfFirst(pd_barcode.Text, ma_code.Text, User.UserSourceCode, Tag.ToString(), out ifFirst, out ErrorMessage))
  82. {
  83. sql.Clear();
  84. sql.Append("select count(1) cn from MES_PACKAGE_VIEW left join makeserial on v_prodcode=ms_prodcode where (ms_sncode=");
  85. sql.Append("v_barcode or ms_psn=v_barcode) and v_outboxcode='" + outboxcode.Text + "'");
  86. int ActualWeight = int.Parse(weight.Text == "" ? "0" : weight.Text);
  87. if (ActualWeight > MinWeight && ActualWeight < MaxWeight)
  88. {
  89. if (PriLabel.Checked)
  90. {
  91. Print.CodeSoft(lbl, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, pd_barcode.Text);
  92. AfterWeighPass();
  93. }
  94. else if (SupPriLabel.Checked)
  95. {
  96. Print.CodeSoft(lbl, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, pd_barcode.Text);
  97. dh.ExecuteSql("update package set pa_printcount= pa_printcount+1 where pa_outboxcode='" + outboxcode.Text + "'", "update");
  98. AfterWeighPass();
  99. }
  100. OperateResult.AppendText(">>检测合格\n", Color.Green);
  101. }
  102. else
  103. {
  104. OperateResult.AppendText(">>检测未通过\n", Color.Red);
  105. }
  106. }
  107. else
  108. {
  109. OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red);
  110. }
  111. }
  112. else
  113. {
  114. OperateResult.AppendText(">>箱号" + outboxcode.Text + "不存在\n", Color.Red);
  115. }
  116. outboxcode.Text = "";
  117. }
  118. }
  119. private void getSerialData()
  120. {
  121. if (serialPort1.IsOpen)
  122. {
  123. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  124. {
  125. SystemInf.OpenPort.Add(serialPort1.PortName);
  126. try
  127. {
  128. while (GetData)
  129. {
  130. try
  131. {
  132. weight.Text = Regex.Replace(serialPort1.ReadLine(), "\\D+", "");
  133. }
  134. catch (Exception)
  135. {
  136. GetData = false;
  137. }
  138. }
  139. }
  140. catch (IOException ex) { MessageBox.Show(ex.Message); }
  141. }
  142. else
  143. MessageBox.Show("端口已被占用,请关闭其他窗口");
  144. }
  145. }
  146. private void AfterWeighPass()
  147. {
  148. string ErrorMessage;
  149. dh.ExecuteSql("update package set pa_weight=" + weight.Text + ", pa_printcount= nvl(pa_printcount,0)+1 where pa_outboxcode='" + outboxcode.Text + "'", "update");
  150. if (!LogicHandler.UpdateMakeMessage(pd_barcode.Text, ma_code.Text, "卡通箱称重", User.UserSourceCode, User.UserName, "称量合格", out ErrorMessage))
  151. OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red);
  152. }
  153. //确认打印
  154. private void Confirm_Click(object sender, EventArgs e)
  155. {
  156. }
  157. //取消调整
  158. private void Cancel_Click(object sender, EventArgs e)
  159. {
  160. }
  161. //停止进程,关闭串口
  162. private void StopWeight_Click(object sender, EventArgs e)
  163. {
  164. GetData = false;
  165. SystemInf.OpenPort.Remove(serialPort1.PortName);
  166. serialPort1.Close();
  167. }
  168. //只能默认勾选一个复选框
  169. private void PrintLabel_CellClick(object sender, DataGridViewCellEventArgs e)
  170. {
  171. //if (PrintLabel.Columns[e.ColumnIndex].Name == "pl_isdefault")
  172. //{
  173. // for (int i = 0; i < PrintLabel.Rows.Count; i++)
  174. // {
  175. // if (i != e.RowIndex && PrintLabel.CurrentCell.ColumnIndex == 0)
  176. // {
  177. // DataGridViewCheckBoxCell cell = (DataGridViewCheckBoxCell)PrintLabel.Rows[i].Cells[0];
  178. // cell.Value = false;
  179. // }
  180. // }
  181. //}
  182. }
  183. //关闭窗口的时候停止进程,不再读取串口数据
  184. private void 卡通箱称重_FormClosing(object sender, FormClosingEventArgs e)
  185. {
  186. lbl.Quit();
  187. if (serialPort1.IsOpen)
  188. {
  189. GetData = false;
  190. serialPort1.Close();
  191. SystemInf.OpenPort.Remove(serialPort1.PortName);
  192. thread.Interrupt();
  193. }
  194. }
  195. private void StartWeight_Click(object sender, EventArgs e)
  196. {
  197. thread = new Thread(getSerialData);
  198. try
  199. {
  200. GetData = true;
  201. serialPort1.PortName = this.ComList.Text;
  202. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  203. serialPort1.Open();
  204. thread.Start();
  205. }
  206. catch (Exception mes)
  207. {
  208. if (BaudRate.Text == "" || ComList.Text == "")
  209. MessageBox.Show("请先在电子秤调试界面维护波特率和串口");
  210. else
  211. MessageBox.Show(mes.Message);
  212. }
  213. }
  214. private void pr_code_TextChanged(object sender, EventArgs e)
  215. {
  216. PrintLabel.DataSource = dh.ExecuteSql("select pl_labelcode,pl_labelname from productlabel where pl_prodcode='" + pr_code.Text + "'", "select");
  217. PrintLabel.DisplayMember = "pl_labelname";
  218. PrintLabel.ValueMember = "pl_labelcode";
  219. }
  220. }
  221. }