Make_CartonBoxWeigh.cs 8.9 KB

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