Packing_BigBoxWeight.cs 11 KB

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