Packing_BigBoxWeight.cs 11 KB

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