Make_PalletWeigh.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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.FunctionCode.Make
  19. {
  20. public partial class Make_PalletWeigh : Form
  21. {
  22. AutoSizeFormClass asc = new AutoSizeFormClass();
  23. LogStringBuilder sql = new LogStringBuilder();
  24. ApplicationClass lbl;
  25. Thread InitPrint;
  26. Thread thread;
  27. DataHelper dh;
  28. DataTable dt;
  29. DataTable listc;
  30. DataTable info;
  31. System.DateTime[] indate;
  32. //创建串口实例
  33. SerialPort serialPort1 = new SerialPort();
  34. //true的时候表示从串口读取数据
  35. bool GetData = true;
  36. public Make_PalletWeigh()
  37. {
  38. InitializeComponent();
  39. }
  40. private void Make_PalletWeigh_Load(object sender, EventArgs e)
  41. {
  42. CheckForIllegalCrossThreadCalls = false;
  43. asc.controllInitializeSize(this);
  44. OperateResult.AppendText(">>请输入栈板号\n", Color.Green);
  45. //栈板号输入框聚焦
  46. palletcode.Focus();
  47. ComList.Text = Properties.Settings.Default.PortName;
  48. BaudRate.Text = Properties.Settings.Default.BaudRate;
  49. dh = new DataHelper();
  50. InitPrint = new Thread(InPrint);
  51. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  52. BaseUtil.SetFormCenter(stw);
  53. stw.ShowDialog();
  54. }
  55. private void InPrint()
  56. {
  57. lbl = new ApplicationClass();
  58. }
  59. private void Make_PalletWeigh_SizeChanged(object sender, EventArgs e)
  60. {
  61. asc.controlAutoSize(this);
  62. }
  63. private void palletcode_KeyDown(object sender, KeyEventArgs e)
  64. {
  65. //按下了enter键
  66. if (e.KeyCode==Keys.Enter) {
  67. if (palletcode.Text=="") {
  68. OperateResult.AppendText("<<输入不能为空\n", Color.Red);
  69. return;
  70. }
  71. //校验栈板号
  72. sql.Clear();
  73. sql.Append("select pa_id,pa_prodcode,pr_spec ||','|| pr_detail pr_detailspec,pa_makecode,pa_salecode,pa_totalqty,");
  74. sql.Append("pa_packageqty from package left join product on pr_code=pa_prodcode where");
  75. sql.Append("pa_outboxcode=:code and pa_type='3'");
  76. dt = (DataTable)dh.ExecuteSql(sql.GetString(),"select",palletcode.Text);
  77. if (dt.Rows.Count == 0)
  78. {
  79. OperateResult.AppendText("<<无该栈板号\n", Color.Red);
  80. palletcode.Text = "";
  81. }
  82. else
  83. {
  84. //给页面赋值
  85. BaseUtil.SetFormValue(this.Controls,dt);
  86. // 更新大箱重量
  87. sql.Clear();
  88. sql.Append("update package set pa_weight=:weight where pa_id=:pa_id ");
  89. dh.ExecuteSql(sql.GetString(),"update",weight.Text,dt.Rows[0]["pa_id"].ToString());
  90. //记录makeprocess
  91. //查询maid
  92. info = (DataTable)dh.ExecuteSql("select ma_id,ma_wccode from make where ma_code='"+pa_makecode.Text+"'","select");
  93. if (info.Rows.Count>0)
  94. {
  95. //有值
  96. //查询
  97. sql.Clear();
  98. sql.Append("insert into MakeProcess(mp_id,mp_makecode,mp_maid,mp_stepcode,mp_stepname ,");
  99. sql.Append("mp_kind,mp_result,mp_indate,mp_inman,mp_wccode,mp_linecode,mp_sourcecode,");
  100. sql.Append("mp_snoutboxcode) values ( makeprocess_seq.nextval,:makecode,:ma_id,:craftcode,:craftname,'栈板称重','称重");
  101. sql.Append("成功',:result,sysdate,:inman,:workcenter,:linecode,:source,:palletcode) ");
  102. dh.ExecuteSql(sql.GetString(), "insert", pa_makecode.Text, info.Rows[0]["ma_id"].ToString(), User.CurrentStepCode, User.CurrentStepName, "栈板号:" + palletcode.Text + ",重量:" + weight.Text + "克", User.UserName, info.Rows[0]["ma_wccode"].ToString(), User.UserLineCode, User.UserSourceCode, palletcode.Text);
  103. //提示信息栏显示:<< 栈板号:XXXX,重量:xxxx 克,
  104. OperateResult.AppendText("<<栈板号:" + palletcode.Text + ",重量:" + weight.Text + "克\n", Color.Green);
  105. //如果用户勾选了自动打印,调用打印, 打印成功,消息提示栏显示“打印成功”,“请输入栈板号”
  106. if (autoPrint.Checked == true)
  107. {
  108. try
  109. {
  110. Print.CodeSoft(lbl, PrintLabel.Text.Split(':')[1], PrintLabel.SelectedValue.ToString(), Printer.Text, palletcode.Text, int.Parse(printNum.Text), indate[PrintLabel.SelectedIndex]);
  111. }
  112. catch (Exception ex)
  113. {
  114. OperateResult.AppendText("<<打印失败\n", Color.Red);
  115. OperateResult.AppendText(">>请输入栈板号\n", Color.Green);
  116. palletcode.Text = "";
  117. }
  118. //打印成功,请输入栈板号
  119. OperateResult.AppendText("<<打印成功\n", Color.Green);
  120. OperateResult.AppendText(">>请输入栈板号\n", Color.Green);
  121. //更新栈板打印次数
  122. sql.Clear();
  123. sql.Append("update package set pa_printcount= pa_printcount+1 where pa_id=:pa_id");
  124. dh.ExecuteSql(sql.GetString(), "update", dt.Rows[0]["pa_id"].ToString());
  125. //刷新称重计数+1
  126. weightedNum.Text = int.Parse(weightedNum.Text) + 1 + "";
  127. //刷新grid的历史称重信息;
  128. //清空输入框中的值,扫描框置空定焦
  129. palletcode.Text = "";
  130. }
  131. }
  132. }
  133. }
  134. }
  135. private void pa_prodcode_TextChanged(object sender, EventArgs e)
  136. {
  137. listc = (DataTable)dh.ExecuteSql("select pl_labelcode ||':'||pl_labelname pl_name,pl_labelname,pl_labelcode, pl_indate,pl_labelurl from productlabel where pl_prodcode='" + pa_prodcode.Text + "'and PL_LABELTYPE='栈板标' order by pl_isdefault desc", "select");
  138. if (listc.Rows.Count == 0)
  139. {
  140. OperateResult.AppendText(">>产品:" + pa_prodcode.Text + "未维护栈板标签模板\n", Color.Red);
  141. }
  142. PrintLabel.DataSource = listc;
  143. PrintLabel.DisplayMember = "pl_name";
  144. PrintLabel.ValueMember = "pl_labelcode";
  145. ftpOperater ftp = new ftpOperater();
  146. indate = new System.DateTime[listc.Rows.Count];
  147. for (int i = 0; i < listc.Rows.Count; i++)
  148. {
  149. BaseUtil.GetPrintLabel(listc.Rows[i]["pl_labelname"].ToString(), listc.Rows[i]["pl_labelurl"].ToString(), listc.Rows[i]["pl_indate"].ToString());
  150. indate[i] = Convert.ToDateTime(listc.Rows[i]["pl_indate"].ToString());
  151. }
  152. }
  153. private void startWeigh_Click(object sender, EventArgs e)
  154. {
  155. thread = new Thread(getSerialData);
  156. try
  157. {
  158. GetData = true;
  159. serialPort1.PortName = this.ComList.Text;
  160. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  161. serialPort1.Open();
  162. thread.Start();
  163. }
  164. catch (Exception mes)
  165. {
  166. if (BaudRate.Text == "" || ComList.Text == "")
  167. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  168. else
  169. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  170. }
  171. }
  172. //停止称重
  173. private void stopWeigh_Click(object sender, EventArgs e)
  174. {
  175. GetData = false;
  176. SystemInf.OpenPort.Remove(serialPort1.PortName);
  177. serialPort1.Close();
  178. }
  179. private void getSerialData()
  180. {
  181. if (serialPort1.IsOpen)
  182. {
  183. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  184. {
  185. SystemInf.OpenPort.Add(serialPort1.PortName);
  186. try
  187. {
  188. while (GetData)
  189. {
  190. try
  191. {
  192. weight.Text = Regex.Replace(serialPort1.ReadLine(), "\\D+", "");
  193. }
  194. catch (Exception)
  195. {
  196. GetData = false;
  197. }
  198. }
  199. }
  200. catch (IOException ex) { MessageBox.Show(ex.Message); }
  201. }
  202. else
  203. MessageBox.Show("端口已被占用,请关闭其他窗口");
  204. }
  205. }
  206. private void Make_PalletWeigh_FormClosing(object sender, FormClosingEventArgs e)
  207. {
  208. lbl.Quit();
  209. if (serialPort1.IsOpen)
  210. {
  211. GetData = false;
  212. serialPort1.Close();
  213. SystemInf.OpenPort.Remove(serialPort1.PortName);
  214. thread.Interrupt();
  215. }
  216. }
  217. }
  218. }