Packing_BigBoxWeight.cs 11 KB

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