Packing_BigBoxWeight.cs 11 KB

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