Make_PalletWeigh.cs 12 KB

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