Make_CartonBoxWeigh.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. private System.DateTime[] indate;
  39. public Make_CartonBoxWeigh()
  40. {
  41. InitializeComponent();
  42. }
  43. private void 卡通箱称重_Load(object sender, EventArgs e)
  44. {
  45. CheckForIllegalCrossThreadCalls = false;
  46. asc.controllInitializeSize(this);
  47. dh = new DataHelper();
  48. lbl = new ApplicationClass();
  49. ComList.Text = Properties.Settings.Default.PortName;
  50. BaudRate.Text = Properties.Settings.Default.BaudRate;
  51. }
  52. private void 卡通箱称重_SizeChanged(object sender, EventArgs e)
  53. {
  54. asc.controlAutoSize(this);
  55. }
  56. private void sncode_KeyDown(object sender, KeyEventArgs e)
  57. {
  58. if (e.KeyCode == Keys.Enter)
  59. {
  60. //根据箱号查询表单数据
  61. pd_barcode.Text = dh.getFieldDataByCondition("makeserial left join package on ms_prodcode=pa_prodcode", "ms_sncode", "pa_outboxcode='" + outboxcode.Text + "'").ToString();
  62. sql.Clear();
  63. 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,");
  64. 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 ");
  65. 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 + "'");
  66. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  67. //填充Form的值
  68. BaseUtil.SetFormValue(this.Controls, dt);
  69. //填充打印文件选项的DataGridView
  70. if (dt.Rows.Count > 0)
  71. {
  72. string ErrorMessage;
  73. //重量的临时变量
  74. string _weight = dt.Rows[0]["pr_cartongw"].ToString();
  75. string _maxweight = dt.Rows[0]["pr_cartonmaxw"].ToString();
  76. string _minweight = dt.Rows[0]["pr_cartonminw"].ToString();
  77. Weight = int.Parse(_weight == "" ? "0" : _weight);
  78. MaxWeight = int.Parse(_maxweight == "" ? "0" : _maxweight);
  79. MinWeight = int.Parse(_minweight == "" ? "0" : _minweight);
  80. pa_totalqty = int.Parse(dt.Rows[0]["pa_totalqty"].ToString());
  81. if (Weight - MinWeight == MaxWeight - Weight)
  82. pr_cartongw.Text = Weight + "±" + (MaxWeight - Weight);
  83. else
  84. pr_cartongw.Text = MinWeight + "-" + MaxWeight;
  85. int ActualWeight = int.Parse(weight.Text == "" ? "0" : weight.Text);
  86. if (ActualWeight >= MinWeight && ActualWeight <= MaxWeight)
  87. {
  88. OperateResult.AppendText(">>箱号" + outboxcode.Text + "检测合格\n", Color.Green);
  89. if (AutoPrint.Checked)
  90. {
  91. OperateResult.AppendText(">>箱号" + outboxcode.Text + "检测合格\n", Color.Green);
  92. Print.CodeSoft(lbl, PrintLabel.Text.Split(':')[1], PrintLabel.SelectedValue.ToString(), PrinterList.Text, outboxcode.Text, int.Parse(PrintNum.Text), indate[PrintLabel.SelectedIndex]);
  93. }
  94. AfterWeighPass();
  95. }
  96. else OperateResult.AppendText(">>箱号" + outboxcode.Text + "检测未通过\n", Color.Red, outboxcode);
  97. }
  98. else OperateResult.AppendText(">>箱号" + outboxcode.Text + "不存在\n", Color.Red, outboxcode);
  99. }
  100. }
  101. private void getSerialData()
  102. {
  103. if (serialPort1.IsOpen)
  104. {
  105. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  106. {
  107. SystemInf.OpenPort.Add(serialPort1.PortName);
  108. try
  109. {
  110. while (GetData)
  111. {
  112. try
  113. {
  114. weight.Text = Regex.Replace(serialPort1.ReadLine(), "\\D+", "");
  115. }
  116. catch (Exception)
  117. {
  118. GetData = false;
  119. }
  120. }
  121. }
  122. catch (IOException ex) { OperateResult.AppendText(">>" + ex.Message + "\n", Color.Red, outboxcode); }
  123. }
  124. else OperateResult.AppendText(">>端口已被占用,请关闭其他窗口\n", Color.Red, outboxcode);
  125. }
  126. }
  127. private void AfterWeighPass()
  128. {
  129. string ErrorMessage;
  130. dh.ExecuteSql("update package set pa_weight=" + weight.Text + ", pa_printcount= nvl(pa_printcount,0)+1 where pa_outboxcode='" + outboxcode.Text + "'", "update");
  131. outboxcode.Text = "";
  132. }
  133. //确认打印
  134. private void Confirm_Click(object sender, EventArgs e)
  135. {
  136. Print.CodeSoft(lbl, PrintLabel.Text.Split(':')[1], PrintLabel.SelectedValue.ToString(), PrinterList.Text, outboxcode.Text, int.Parse(PrintNum.Text), indate[PrintLabel.SelectedIndex]);
  137. }
  138. //停止进程,关闭串口
  139. private void StopWeight_Click(object sender, EventArgs e)
  140. {
  141. GetData = false;
  142. SystemInf.OpenPort.Remove(serialPort1.PortName);
  143. serialPort1.Close();
  144. }
  145. //关闭窗口的时候停止进程,不再读取串口数据
  146. private void 卡通箱称重_FormClosing(object sender, FormClosingEventArgs e)
  147. {
  148. lbl.Quit();
  149. if (serialPort1.IsOpen)
  150. {
  151. GetData = false;
  152. serialPort1.Close();
  153. SystemInf.OpenPort.Remove(serialPort1.PortName);
  154. thread.Interrupt();
  155. }
  156. }
  157. private void StartWeight_Click(object sender, EventArgs e)
  158. {
  159. thread = new Thread(getSerialData);
  160. try
  161. {
  162. GetData = true;
  163. serialPort1.PortName = this.ComList.Text;
  164. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  165. serialPort1.Open();
  166. thread.Start();
  167. }
  168. catch (Exception mes)
  169. {
  170. if (BaudRate.Text == "" || ComList.Text == "")
  171. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  172. else
  173. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  174. }
  175. }
  176. private void pr_code_TextChanged(object sender, EventArgs e)
  177. {
  178. DataTable dt = (DataTable)dh.ExecuteSql("select pl_labelcode||':'||pl_labelname pl_laname,pl_labelname,pl_labelcode,pl_labelurl,pl_indate from productlabel where pl_prodcode='" + pr_code.Text + "' order by pl_isdefault desc ", "select");
  179. PrintLabel.DataSource = dt;
  180. PrintLabel.DisplayMember = "pl_laname";
  181. PrintLabel.ValueMember = "pl_labelcode";
  182. ftpOperater ftp = new ftpOperater();
  183. indate = new System.DateTime[dt.Rows.Count];
  184. for (int i = 0; i < dt.Rows.Count; i++)
  185. {
  186. BaseUtil.GetPrintLabel(dt.Rows[i]["pl_labelname"].ToString(), dt.Rows[i]["pl_labelurl"].ToString(), dt.Rows[i]["pl_indate"].ToString());
  187. indate[i] = Convert.ToDateTime(dt.Rows[i]["pl_indate"].ToString());
  188. }
  189. }
  190. }
  191. }