Make_PalletWeigh.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_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.Black);
  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. try
  58. {
  59. lbl = new ApplicationClass();
  60. BaseUtil.WriteLbl(lbl);
  61. }
  62. catch (Exception ex)
  63. {
  64. MessageBox.Show("未安装codesoft");
  65. }
  66. }
  67. private void Make_PalletWeigh_SizeChanged(object sender, EventArgs e)
  68. {
  69. asc.controlAutoSize(this);
  70. }
  71. private void palletcode_KeyDown(object sender, KeyEventArgs e)
  72. {
  73. //按下了enter键
  74. if (e.KeyCode==Keys.Enter) {
  75. if (palletcode.Text=="") {
  76. OperateResult.AppendText("<<输入不能为空\n", Color.Red);
  77. return;
  78. }
  79. //校验栈板号
  80. sql.Clear();
  81. sql.Append("select pa_id,pa_prodcode,pr_spec ||','|| pr_detail pr_detailspec,pa_makecode,pa_salecode,pa_totalqty,");
  82. sql.Append("pa_packageqty from package left join product on pr_code=pa_prodcode where ");
  83. sql.Append("pa_outboxcode=:code and pa_type='3'");
  84. dt = (DataTable)dh.ExecuteSql(sql.GetString(),"select",palletcode.Text);
  85. if (dt.Rows.Count == 0)
  86. {
  87. OperateResult.AppendText("<<无该栈板号\n", Color.Red);
  88. palletcode.Text = "";
  89. }
  90. else
  91. {
  92. //给页面赋值
  93. BaseUtil.SetFormValue(this.Controls,dt);
  94. // 更新大箱重量
  95. sql.Clear();
  96. sql.Append("update package set pa_weight=:weight where pa_id=:pa_id ");
  97. dh.ExecuteSql(sql.GetString(),"update",weight.Text,dt.Rows[0]["pa_id"].ToString());
  98. //记录makeprocess
  99. //查询maid
  100. info = (DataTable)dh.ExecuteSql("select ma_id,ma_wccode from make where ma_code='"+pa_makecode.Text+"'","select");
  101. if (info.Rows.Count>0)
  102. {
  103. //有值
  104. //查询
  105. sql.Clear();
  106. sql.Append("insert into MakeProcess(mp_id,mp_makecode,mp_maid,mp_stepcode,mp_stepname ,");
  107. sql.Append("mp_kind,mp_result,mp_indate,mp_inman,mp_wccode,mp_linecode,mp_sourcecode,");
  108. sql.Append("mp_snoutboxcode) values ( makeprocess_seq.nextval,:makecode,:ma_id,:craftcode,:craftname,'栈板称重'");
  109. sql.Append(",:result,sysdate,:inman,:workcenter,:linecode,:source,:palletcode) ");
  110. 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);
  111. //提示信息栏显示:<< 栈板号:XXXX,重量:xxxx 克,
  112. OperateResult.AppendText("<<栈板号:" + palletcode.Text + ",重量:" + weight.Text + "克\n", Color.Green);
  113. //如果用户勾选了自动打印,调用打印, 打印成功,消息提示栏显示“打印成功”,“请输入栈板号”
  114. if (autoPrint.Checked == true)
  115. {
  116. try
  117. {
  118. if (PrintLabel.SelectedValue.ToString()=="") {
  119. OperateResult.AppendText(">>产品:" + pa_prodcode.Text + "未维护栈板标签模板\n", Color.Red);
  120. palletcode.Text = "";
  121. return;
  122. }
  123. Print.CodeSoft(lbl, PrintLabel.Text.Split(':')[1], PrintLabel.SelectedValue.ToString(), Printer.Text, palletcode.Text, int.Parse(printNum.Text), indate[PrintLabel.SelectedIndex]);
  124. }
  125. catch (Exception ex)
  126. {
  127. OperateResult.AppendText("<<打印失败\n", Color.Red);
  128. OperateResult.AppendText(">>请输入栈板号\n", Color.Black);
  129. palletcode.Text = "";
  130. return;
  131. }
  132. //打印成功,请输入栈板号
  133. OperateResult.AppendText("<<打印成功\n", Color.Green);
  134. OperateResult.AppendText(">>请输入栈板号\n", Color.Black);
  135. //更新栈板打印次数
  136. sql.Clear();
  137. sql.Append("update package set pa_printcount= pa_printcount+1 where pa_id=:pa_id");
  138. dh.ExecuteSql(sql.GetString(), "update", dt.Rows[0]["pa_id"].ToString());
  139. //刷新称重计数+1
  140. weightedNum.Text = int.Parse(weightedNum.Text) + 1 + "";
  141. //刷新grid的历史称重信息;
  142. if (int.Parse(weight.Text)==0) {
  143. OperateResult.AppendText("<<重量不能等于0\n", Color.Red);
  144. palletcode.Text = "";
  145. return;
  146. }
  147. recordResult(palletcode.Text,weight.Text, System.DateTime.Now.ToString());
  148. //清空输入框中的值,扫描框置空定焦
  149. palletcode.Text = "";
  150. }
  151. }
  152. }
  153. }
  154. }
  155. private void pa_prodcode_TextChanged(object sender, EventArgs e)
  156. {
  157. 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");
  158. if (listc.Rows.Count == 0)
  159. {
  160. OperateResult.AppendText(">>产品:" + pa_prodcode.Text + "未维护栈板标签模板\n", Color.Red);
  161. }
  162. PrintLabel.DataSource = listc;
  163. PrintLabel.DisplayMember = "pl_name";
  164. PrintLabel.ValueMember = "pl_labelcode";
  165. ftpOperater ftp = new ftpOperater();
  166. indate = new System.DateTime[listc.Rows.Count];
  167. for (int i = 0; i < listc.Rows.Count; i++)
  168. {
  169. BaseUtil.GetPrintLabel(listc.Rows[i]["pl_labelname"].ToString(), listc.Rows[i]["pl_labelurl"].ToString(), listc.Rows[i]["pl_indate"].ToString());
  170. indate[i] = Convert.ToDateTime(listc.Rows[i]["pl_indate"].ToString());
  171. }
  172. }
  173. private void startWeigh_Click(object sender, EventArgs e)
  174. {
  175. thread = new Thread(getSerialData);
  176. try
  177. {
  178. GetData = true;
  179. serialPort1.PortName = this.ComList.Text;
  180. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  181. serialPort1.Open();
  182. thread.Start();
  183. }
  184. catch (Exception mes)
  185. {
  186. if (BaudRate.Text == "" || ComList.Text == "")
  187. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  188. else
  189. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  190. }
  191. }
  192. //停止称重
  193. private void stopWeigh_Click(object sender, EventArgs e)
  194. {
  195. GetData = false;
  196. SystemInf.OpenPort.Remove(serialPort1.PortName);
  197. serialPort1.Close();
  198. }
  199. private void getSerialData()
  200. {
  201. if (serialPort1.IsOpen)
  202. {
  203. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  204. {
  205. SystemInf.OpenPort.Add(serialPort1.PortName);
  206. try
  207. {
  208. while (GetData)
  209. {
  210. try
  211. {
  212. weight.Text = Regex.Replace(serialPort1.ReadLine(), "\\D+", "");
  213. }
  214. catch (Exception)
  215. {
  216. GetData = false;
  217. }
  218. }
  219. }
  220. catch (IOException ex) { MessageBox.Show(ex.Message); }
  221. }
  222. else
  223. MessageBox.Show("端口已被占用,请关闭其他窗口");
  224. }
  225. }
  226. private void Make_PalletWeigh_FormClosing(object sender, FormClosingEventArgs e)
  227. {
  228. lbl.Quit();
  229. if (serialPort1.IsOpen)
  230. {
  231. GetData = false;
  232. serialPort1.Close();
  233. SystemInf.OpenPort.Remove(serialPort1.PortName);
  234. thread.Interrupt();
  235. }
  236. }
  237. private void recordResult(string palletcode, string weigh, string time)
  238. {
  239. //if (showResult.Items.Count >= 50)
  240. //{
  241. // //如果有10行的话,则删除最前一行
  242. // showResult.Items.RemoveAt(0);
  243. //}
  244. //创建一个item
  245. ListViewItem lvi = new ListViewItem();
  246. //分条赋值
  247. lvi.SubItems.Add(palletcode);
  248. lvi.SubItems.Add(weigh);
  249. lvi.SubItems.Add(time);
  250. //添加结果的信息进去
  251. showResult.Items.Add(lvi);
  252. }
  253. }
  254. }