Make_BigBoxWeight.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.DataOperate;
  15. using UAS_MES.Entity;
  16. using UAS_MES.PublicForm;
  17. using UAS_MES.PublicMethod;
  18. namespace UAS_MES.Make
  19. {
  20. public partial class Make_BigBoxWeight : Form
  21. {
  22. AutoSizeFormClass asc = new AutoSizeFormClass();
  23. System.DateTime[] indate;
  24. DataHelper dh;
  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 Make_BigBoxWeight()
  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. dh = new DataHelper();
  64. pa_outboxcode.Focus();
  65. OperateResult.AppendText(">>请输入大箱号\n");
  66. printcount = 0;
  67. weightsum.Text = printcount + "";
  68. }
  69. private void InPrint()
  70. {
  71. try
  72. {
  73. lbl = new ApplicationClass();
  74. BaseUtil.WriteLbl(lbl);
  75. }
  76. catch
  77. {
  78. OperateResult.AppendText("未正确安装CodeSoft软件\n", Color.Red);
  79. }
  80. }
  81. private void Make_BigBoxWeight_SizeChanged(object sender, EventArgs e)
  82. {
  83. asc.controlAutoSize(this);
  84. }
  85. private void pa_outboxcode_KeyDown(object sender, KeyEventArgs e)
  86. {
  87. if (e.KeyCode == Keys.Enter)
  88. {
  89. sql.Clear();
  90. sql.Append(" select pa_id,pa_prodcode,pr_spec,pr_detail,pa_makecode,pa_salecode,pa_totalqty, ");
  91. sql.Append(" pa_packageqty from package left join product on pr_code=pa_prodcode where ");
  92. sql.Append(" pa_outboxcode='" + pa_outboxcode.Text + "' and pa_type=2");
  93. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  94. outboxcode = pa_outboxcode.Text;
  95. BaseUtil.CleanForm(this);
  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. int ActualWeight = int.Parse(weight.Text == "" ? "0" : weight.Text);
  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 + ",重量:" + ActualWeight + "克\n", Color.Green);
  115. //记录makeprocess
  116. sql.Clear();
  117. sql.Append("insert into MakeProcess(mp_id,mp_makecode,mp_maid,mp_stepcode,mp_stepname ,");
  118. sql.Append("mp_kind,mp_result,mp_indate,mp_inman,mp_wccode,mp_linecode,mp_sourcecode,mp_snoutboxcode)");
  119. sql.Append("select MakeProcess_seq.nextval, ma_code,ma_id,'" + User.CurrentStepCode + "','" + User.CurrentStepName + "',");
  120. sql.Append("'" + "大箱称重" + "','" + "称重成功,大箱箱号" + pa_outboxcode.Text + ",重量:" + ActualWeight + "克" + "',sysdate,'" + User.UserName + "',ma_wccode,'" + User.UserLineCode + "','" + User.UserSourceCode + "',");
  121. sql.Append("'" + pa_outboxcode.Text + "' from make left join makeserial on ms_makecode=ma_code ");
  122. sql.Append("where ma_code='" + pa_makecode.Text + "'");
  123. dh.ExecuteSql(sql.GetString(), "insert");
  124. OperateResult.AppendText("<<<更新成功\n", Color.Green);
  125. //刷新grid的历史称重信息;
  126. recordResult(pa_outboxcode.Text, weight.Text, System.DateTime.Now.ToString());
  127. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, pa_makecode.Text, User.UserLineCode, User.UserSourceCode, "大箱称重", "称重成功", "", "");
  128. if (autoprint.Checked == true)
  129. {
  130. try
  131. {
  132. Print.CodeSoft(Tag.ToString(), lbl, PrintLabel.Text.Split(':')[1], PrintLabel.SelectedValue.ToString(), Printer.Text, pa_outboxcode.Text, int.Parse(PrintNum.Text), indate[PrintLabel.SelectedIndex]);
  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
  148. OperateResult.AppendText("<<<打印失败:" + eb.Message + "\n", Color.Red);
  149. }
  150. }
  151. else
  152. {
  153. OperateResult.AppendText("<<<需打印请勾选自动打印\n", Color.Red);
  154. }
  155. }
  156. else
  157. {
  158. OperateResult.AppendText("<<<大箱重量应大于0\n", Color.Red);
  159. }
  160. }
  161. }
  162. private void recordResult(string pa_outboxcode, string weight, string dateTime)
  163. {
  164. //创建一个item
  165. ListViewItem lvi = new ListViewItem();
  166. //分条赋值
  167. lvi.SubItems.Add(pa_outboxcode);
  168. lvi.SubItems.Add(weight);
  169. lvi.SubItems.Add(dateTime);
  170. //添加结果的信息进去
  171. showResult.Items.Add(lvi);
  172. }
  173. private void pa_prodcode_TextChanged(object sender, EventArgs e)
  174. {
  175. DataTable _dt = (DataTable)dh.ExecuteSql("select pl_labelcode ||':'||pl_labelname pl_name,pl_labelname,pl_labelcode,pl_labelurl,pl_indate from productlabel where pl_prodcode='" + pa_prodcode.Text + "' and PL_LABELTYPE='大箱标' order by PL_ISDEFAULT DESC", "select");
  176. PrintLabel.DataSource = _dt;
  177. PrintLabel.DisplayMember = "pl_name";
  178. PrintLabel.ValueMember = "pl_labelcode";
  179. ftpOperater ftp = new ftpOperater();
  180. indate = new System.DateTime[_dt.Rows.Count];
  181. for (int i = 0; i < _dt.Rows.Count; i++)
  182. {
  183. BaseUtil.GetPrintLabel(_dt.Rows[i]["pl_labelname"].ToString(), _dt.Rows[i]["pl_labelurl"].ToString(), _dt.Rows[i]["pl_indate"].ToString());
  184. indate[i] = Convert.ToDateTime(_dt.Rows[i]["pl_indate"].ToString());
  185. }
  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 = Regex.Replace(serialPort1.ReadLine(), "\\D+", "");
  220. }
  221. catch (Exception)
  222. {
  223. GetData = false;
  224. }
  225. }
  226. }
  227. catch (IOException ex) { MessageBox.Show(ex.Message); }
  228. }
  229. else
  230. MessageBox.Show("端口已被占用,请关闭其他窗口");
  231. }
  232. }
  233. private void StopWeight_Click(object sender, EventArgs e)
  234. {
  235. GetData = false;
  236. SystemInf.OpenPort.Remove(serialPort1.PortName);
  237. serialPort1.Close();
  238. }
  239. private void Make_BigBoxWeight_FormClosing(object sender, FormClosingEventArgs e)
  240. {
  241. BaseUtil.ClosePrint(lbl);
  242. if (serialPort1.IsOpen)
  243. {
  244. GetData = false;
  245. serialPort1.Close();
  246. SystemInf.OpenPort.Remove(serialPort1.PortName);
  247. thread.Interrupt();
  248. }
  249. }
  250. }
  251. }