Packing_BigBoxWeight.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using LabelManager2;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.IO.Ports;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading;
  13. using System.Windows.Forms;
  14. using UAS_MES_NEW.DataOperate;
  15. using UAS_MES_NEW.Entity;
  16. using UAS_MES_NEW.PublicForm;
  17. using UAS_MES_NEW.PublicMethod;
  18. namespace UAS_MES_NEW.Packing
  19. {
  20. public partial class Packing_BigBoxWeigh : Form
  21. {
  22. AutoSizeFormClass asc = new AutoSizeFormClass();
  23. DataHelper dh;
  24. Document doc;
  25. //true的时候表示从串口读取数据
  26. bool GetData = true;
  27. DataTable dt;
  28. LogStringBuilder sql = new LogStringBuilder();
  29. ApplicationClass lbl;
  30. Thread thread;
  31. Thread InitPrint;
  32. //打印计数
  33. int printcount;
  34. //创建串口实例
  35. SerialPort serialPort1 = new SerialPort();
  36. //存储当前大箱号
  37. string outboxcode = "";
  38. //记录称重串口号和波特率
  39. string Comlist = "";
  40. string Baurate = "";
  41. public Packing_BigBoxWeigh()
  42. {
  43. InitializeComponent();
  44. }
  45. protected override void OnVisibleChanged(EventArgs e)
  46. {
  47. base.OnVisibleChanged(e);
  48. if (!IsHandleCreated)
  49. {
  50. this.Close();
  51. }
  52. }
  53. private void Make_BigBoxWeight_Load(object sender, EventArgs e)
  54. {
  55. asc.controllInitializeSize(this);
  56. CheckForIllegalCrossThreadCalls = false;
  57. Comlist = BaseUtil.GetCacheData("PortName").ToString();
  58. Baurate = BaseUtil.GetCacheData("BaudRate").ToString();
  59. InitPrint = new Thread(InPrint);
  60. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  61. BaseUtil.SetFormCenter(stw);
  62. stw.ShowDialog();
  63. pa_outboxcode.Focus();
  64. OperateResult.AppendText(">>请输入大箱号\n");
  65. printcount = 0;
  66. weightsum.Text = printcount + "";
  67. StartWeight.PerformClick();
  68. dh = SystemInf.dh;
  69. }
  70. private void InPrint()
  71. {
  72. try
  73. {
  74. lbl = new ApplicationClass();
  75. BaseUtil.WriteLbl();
  76. }
  77. catch
  78. {
  79. OperateResult.AppendText("未正确安装CodeSoft软件\n", Color.Red);
  80. }
  81. }
  82. private void Make_BigBoxWeight_SizeChanged(object sender, EventArgs e)
  83. {
  84. asc.controlAutoSize(this);
  85. }
  86. private void pa_outboxcode_KeyDown(object sender, KeyEventArgs e)
  87. {
  88. if (e.KeyCode == Keys.Enter)
  89. {
  90. sql.Clear();
  91. sql.Append("select pa_id,pa_prodcode,pr_spec,pr_detail,pa_makecode,pa_salecode,pa_totalqty,");
  92. sql.Append("pa_packageqty from package left join product on pr_code=pa_prodcode where ");
  93. sql.Append("pa_outboxcode='" + pa_outboxcode.Text + "' and pa_type=2");
  94. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  95. outboxcode = pa_outboxcode.Text;
  96. weightsum.Text = printcount + "";
  97. if (dt.Rows.Count > 0)
  98. {
  99. BaseUtil.SetFormValue(this.Controls, dt);
  100. pa_outboxcode.Text = outboxcode;
  101. }
  102. else
  103. {
  104. OperateResult.AppendText(">>箱号不存在\n", Color.Red);
  105. return;
  106. }
  107. double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text.Replace("kg", "").Trim());
  108. if (ActualWeight > 0)
  109. {
  110. //更新大箱重量
  111. sql.Clear();
  112. sql.Append("update package set pa_weight='" + ActualWeight + "' where pa_outboxcode= '" + pa_outboxcode.Text + "'");
  113. dh.ExecuteSql(sql.GetString(), "update");
  114. OperateResult.AppendText("<<<箱号:" + pa_outboxcode.Text + ",重量:" + weight.Text + "\n", Color.Green);
  115. LogicHandler.InsertMakeProcess(pa_outboxcode.Text, pa_makecode.Text, User.UserSourceCode, "大箱称量", "大箱称量合格", User.UserCode);
  116. OperateResult.AppendText("<<<更新成功\n", Color.Green);
  117. //刷新grid的历史称重信息;
  118. recordResult(pa_outboxcode.Text, weight.Text, System.DateTime.Now.ToString());
  119. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, pa_makecode.Text, User.UserLineCode, User.UserSourceCode, "大箱" + pa_outboxcode.Text + "称重:" + weight.Text, "称重成功", pa_outboxcode.Text, "");
  120. LogicHandler.RecordProdWeight(pa_outboxcode.Text, "BIGBOX", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pa_prodcode.Text, User.UserSourceCode, User.UserName);
  121. if (autoprint.Checked == true)
  122. {
  123. try
  124. {
  125. //doc = lbl.Documents.Open(ftpOperater.DownLoadTo + PrintLabel.Text);
  126. string ErrorMessage = "";
  127. if (!Print.CodeSoft(Tag.ToString(), ref lbl, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, pa_outboxcode.Text, int.Parse(PrintNum.Text), pa_makecode.Text, pa_prodcode.Text, "大箱标", "0", out ErrorMessage))
  128. {
  129. OperateResult.AppendText(ErrorMessage + "\n", Color.Red);
  130. }
  131. dh.ExecuteSql("update package set pa_printcount= pa_printcount+1 where pa_outboxcode='" + pa_outboxcode.Text + "'", "update");
  132. printcount++;
  133. weightsum.Text = printcount + "";
  134. //清空输入框中的值,扫描框置空定焦
  135. pa_outboxcode.Text = "";
  136. pa_outboxcode.Focus();
  137. }
  138. catch (Exception eb)
  139. {
  140. if (PrintLabel.SelectedValue == null)
  141. {
  142. OperateResult.AppendText(">>产品:" + pa_prodcode.Text + "未维护大箱标签模板\n", Color.Red, pa_outboxcode);
  143. pa_outboxcode.Focus();
  144. }
  145. else OperateResult.AppendText("<<<打印失败:" + eb.Message + "\n", Color.Red);
  146. }
  147. }
  148. }
  149. else
  150. {
  151. OperateResult.AppendText("<<<大箱重量应大于0\n", Color.Red);
  152. }
  153. }
  154. }
  155. private void recordResult(string pa_outboxcode, string weight, string dateTime)
  156. {
  157. //创建一个item
  158. ListViewItem lvi = new ListViewItem();
  159. //分条赋值
  160. lvi.SubItems.Add(pa_outboxcode);
  161. lvi.SubItems.Add(weight);
  162. lvi.SubItems.Add(dateTime);
  163. //添加结果的信息进去
  164. showResult.Items.Add(lvi);
  165. }
  166. private void pa_prodcode_TextChanged(object sender, EventArgs e)
  167. {
  168. DataTable _dt = (DataTable)dh.ExecuteSql("select la_id,substr(la_url,instr(la_url,'\\',-1)+1)la_name,la_url,la_isdefault from label where la_prodcode='" + pa_prodcode.Text + "' and la_templatetype='大箱标' and la_statuscode='AUDITED' order by la_isdefault", "select");
  169. PrintLabel.DataSource = _dt;
  170. PrintLabel.DisplayMember = "la_name";
  171. PrintLabel.ValueMember = "la_id";
  172. ftpOperater ftp = new ftpOperater();
  173. for (int i = 0; i < _dt.Rows.Count; i++)
  174. {
  175. BaseUtil.GetPrintLabel(_dt.Rows[i]["la_name"].ToString(), _dt.Rows[i]["la_url"].ToString());
  176. }
  177. }
  178. private void StartWeight_Click(object sender, EventArgs e)
  179. {
  180. thread = new Thread(getSerialData);
  181. try
  182. {
  183. GetData = true;
  184. serialPort1.PortName = Comlist;
  185. serialPort1.BaudRate = int.Parse(Baurate);
  186. serialPort1.Open();
  187. thread.Start();
  188. }
  189. catch (Exception mes)
  190. {
  191. if (Baurate == "" || Comlist == "")
  192. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  193. else
  194. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  195. }
  196. }
  197. private void getSerialData()
  198. {
  199. if (serialPort1.IsOpen)
  200. {
  201. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  202. {
  203. SystemInf.OpenPort.Add(serialPort1.PortName);
  204. try
  205. {
  206. while (GetData)
  207. {
  208. try
  209. {
  210. weight.Text = serialPort1.ReadLine();
  211. //weight.Text = Regex.Replace(serialPort1.ReadLine(), "\\D+", "");
  212. }
  213. catch (Exception)
  214. {
  215. GetData = false;
  216. }
  217. }
  218. }
  219. catch (IOException ex) { MessageBox.Show(ex.Message); }
  220. }
  221. else
  222. MessageBox.Show("端口已被占用,请关闭其他窗口");
  223. }
  224. }
  225. private void StopWeight_Click(object sender, EventArgs e)
  226. {
  227. if (serialPort1.IsOpen)
  228. {
  229. GetData = false;
  230. serialPort1.Close();
  231. SystemInf.OpenPort.Remove(serialPort1.PortName);
  232. thread.Abort();
  233. }
  234. }
  235. private void Make_BigBoxWeight_FormClosing(object sender, FormClosingEventArgs e)
  236. {
  237. BaseUtil.ClosePrint(lbl);
  238. StopWeight.PerformClick();
  239. InitPrint.Abort();
  240. if (serialPort1.IsOpen)
  241. {
  242. GetData = false;
  243. serialPort1.Close();
  244. SystemInf.OpenPort.Remove(serialPort1.PortName);
  245. thread.Interrupt();
  246. }
  247. //thread.Abort();
  248. }
  249. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  250. {
  251. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  252. {
  253. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  254. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  255. }
  256. }
  257. }
  258. }