Make_CartonBoxWeigh.cs 11 KB

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