Make_BigBoxWeight.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. Document doc;
  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 Make_BigBoxWeight()
  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. dh = new DataHelper();
  65. pa_outboxcode.Focus();
  66. OperateResult.AppendText(">>请输入大箱号\n");
  67. printcount = 0;
  68. weightsum.Text = printcount + "";
  69. }
  70. private void InPrint()
  71. {
  72. try
  73. {
  74. lbl = new ApplicationClass();
  75. BaseUtil.WriteLbl(lbl);
  76. }
  77. catch
  78. {
  79. OperateResult.AppendText("未正确安装CodeSoft软件\n", Color.Red);
  80. }
  81. }
  82. private void Make_BigBoxWeight_SizeChanged(object sender, EventArgs e)
  83. {
  84. asc.controlAutoSize(this);
  85. }
  86. private void pa_outboxcode_KeyDown(object sender, KeyEventArgs e)
  87. {
  88. if (e.KeyCode == Keys.Enter)
  89. {
  90. sql.Clear();
  91. sql.Append(" select pa_id,pa_prodcode,pr_spec,pr_detail,pa_makecode,pa_salecode,pa_totalqty, ");
  92. sql.Append(" pa_packageqty from package left join product on pr_code=pa_prodcode where ");
  93. sql.Append(" pa_outboxcode='" + pa_outboxcode.Text + "' and pa_type=2");
  94. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  95. outboxcode = pa_outboxcode.Text;
  96. BaseUtil.CleanForm(this);
  97. weightsum.Text = printcount + "";
  98. if (dt.Rows.Count > 0)
  99. {
  100. BaseUtil.SetFormValue(this.Controls, dt);
  101. pa_outboxcode.Text = outboxcode;
  102. }
  103. else
  104. {
  105. OperateResult.AppendText(">>箱号不存在\n", Color.Red);
  106. return;
  107. }
  108. int ActualWeight = int.Parse(weight.Text == "" ? "0" : weight.Text);
  109. if (ActualWeight > 0)
  110. {
  111. //更新大箱重量
  112. sql.Clear();
  113. sql.Append("update package set pa_weight='" + ActualWeight + "' where pa_outboxcode= '" + pa_outboxcode.Text + "'");
  114. dh.ExecuteSql(sql.GetString(), "update");
  115. OperateResult.AppendText("<<<箱号:" + pa_outboxcode.Text + ",重量:" + ActualWeight + "克\n", Color.Green);
  116. //记录makeprocess
  117. sql.Clear();
  118. sql.Append("insert into MakeProcess(mp_id,mp_makecode,mp_maid,mp_stepcode,mp_stepname ,");
  119. sql.Append("mp_kind,mp_result,mp_indate,mp_inman,mp_wccode,mp_linecode,mp_sourcecode,mp_snoutboxcode)");
  120. sql.Append("select MakeProcess_seq.nextval, ma_code,ma_id,'" + User.CurrentStepCode + "','" + User.CurrentStepName + "',");
  121. sql.Append("'" + "大箱称重" + "','" + "称重成功,大箱箱号" + pa_outboxcode.Text + ",重量:" + ActualWeight + "克" + "',sysdate,'" + User.UserName + "',ma_wccode,'" + User.UserLineCode + "','" + User.UserSourceCode + "',");
  122. sql.Append("'" + pa_outboxcode.Text + "' from make left join makeserial on ms_makecode=ma_code ");
  123. sql.Append("where ma_code='" + pa_makecode.Text + "'");
  124. dh.ExecuteSql(sql.GetString(), "insert");
  125. OperateResult.AppendText("<<<更新成功\n", Color.Green);
  126. //刷新grid的历史称重信息;
  127. recordResult(pa_outboxcode.Text, weight.Text, System.DateTime.Now.ToString());
  128. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, pa_makecode.Text, User.UserLineCode, User.UserSourceCode, "大箱称重", "称重成功", "", "");
  129. if (autoprint.Checked == true)
  130. {
  131. try
  132. {
  133. doc = lbl.Documents.Open(ftpOperater.DownLoadTo + PrintLabel.Text.Split(':')[1]);
  134. Print.CodeSoft(Tag.ToString(), doc, PrintLabel.Text.Split(':')[1], PrintLabel.SelectedValue.ToString(), Printer.Text, pa_outboxcode.Text, int.Parse(PrintNum.Text), indate[PrintLabel.SelectedIndex]);
  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
  150. OperateResult.AppendText("<<<打印失败:" + eb.Message + "\n", Color.Red);
  151. }
  152. }
  153. else
  154. {
  155. OperateResult.AppendText("<<<需打印请勾选自动打印\n", Color.Red);
  156. }
  157. }
  158. else
  159. {
  160. OperateResult.AppendText("<<<大箱重量应大于0\n", Color.Red);
  161. }
  162. }
  163. }
  164. private void recordResult(string pa_outboxcode, string weight, string dateTime)
  165. {
  166. //创建一个item
  167. ListViewItem lvi = new ListViewItem();
  168. //分条赋值
  169. lvi.SubItems.Add(pa_outboxcode);
  170. lvi.SubItems.Add(weight);
  171. lvi.SubItems.Add(dateTime);
  172. //添加结果的信息进去
  173. showResult.Items.Add(lvi);
  174. }
  175. private void pa_prodcode_TextChanged(object sender, EventArgs e)
  176. {
  177. 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");
  178. PrintLabel.DataSource = _dt;
  179. PrintLabel.DisplayMember = "pl_name";
  180. PrintLabel.ValueMember = "pl_labelcode";
  181. ftpOperater ftp = new ftpOperater();
  182. indate = new System.DateTime[_dt.Rows.Count];
  183. for (int i = 0; i < _dt.Rows.Count; i++)
  184. {
  185. BaseUtil.GetPrintLabel(_dt.Rows[i]["pl_labelname"].ToString(), _dt.Rows[i]["pl_labelurl"].ToString(), _dt.Rows[i]["pl_indate"].ToString());
  186. indate[i] = Convert.ToDateTime(_dt.Rows[i]["pl_indate"].ToString());
  187. }
  188. }
  189. private void StartWeight_Click(object sender, EventArgs e)
  190. {
  191. thread = new Thread(getSerialData);
  192. try
  193. {
  194. GetData = true;
  195. serialPort1.PortName = Comlist;
  196. serialPort1.BaudRate = int.Parse(Baurate);
  197. serialPort1.Open();
  198. thread.Start();
  199. }
  200. catch (Exception mes)
  201. {
  202. if (Baurate == "" || Comlist == "")
  203. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  204. else
  205. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  206. }
  207. }
  208. private void getSerialData()
  209. {
  210. if (serialPort1.IsOpen)
  211. {
  212. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  213. {
  214. SystemInf.OpenPort.Add(serialPort1.PortName);
  215. try
  216. {
  217. while (GetData)
  218. {
  219. try
  220. {
  221. weight.Text = Regex.Replace(serialPort1.ReadLine(), "\\D+", "");
  222. }
  223. catch (Exception)
  224. {
  225. GetData = false;
  226. }
  227. }
  228. }
  229. catch (IOException ex) { MessageBox.Show(ex.Message); }
  230. }
  231. else
  232. MessageBox.Show("端口已被占用,请关闭其他窗口");
  233. }
  234. }
  235. private void StopWeight_Click(object sender, EventArgs e)
  236. {
  237. GetData = false;
  238. SystemInf.OpenPort.Remove(serialPort1.PortName);
  239. serialPort1.Close();
  240. }
  241. private void Make_BigBoxWeight_FormClosing(object sender, FormClosingEventArgs e)
  242. {
  243. BaseUtil.ClosePrint(lbl);
  244. if (serialPort1.IsOpen)
  245. {
  246. GetData = false;
  247. serialPort1.Close();
  248. SystemInf.OpenPort.Remove(serialPort1.PortName);
  249. thread.Interrupt();
  250. }
  251. }
  252. }
  253. }