Packing_PalletWeigh.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using LabelManager2;
  2. using System;
  3. using System.Data;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.IO.Ports;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9. using UAS_MES_NEW.DataOperate;
  10. using UAS_MES_NEW.Entity;
  11. using UAS_MES_NEW.PublicForm;
  12. using UAS_MES_NEW.PublicMethod;
  13. namespace UAS_MES_NEW.Packing
  14. {
  15. public partial class Packing_PalletWeigh : Form
  16. {
  17. AutoSizeFormClass asc = new AutoSizeFormClass();
  18. LogStringBuilder sql = new LogStringBuilder();
  19. ApplicationClass lbl;
  20. Document doc;
  21. Thread InitPrint;
  22. Thread thread;
  23. DataHelper dh;
  24. DataTable dt;
  25. DataTable listc;
  26. DataTable info;
  27. //创建串口实例
  28. SerialPort serialPort1 = new SerialPort();
  29. //true的时候表示从串口读取数据
  30. bool GetData = true;
  31. public Packing_PalletWeigh()
  32. {
  33. InitializeComponent();
  34. }
  35. private void Make_PalletWeigh_Load(object sender, EventArgs e)
  36. {
  37. CheckForIllegalCrossThreadCalls = false;
  38. asc.controllInitializeSize(this);
  39. OperateResult.AppendText(">>请输入栈板号\n", Color.Black);
  40. //栈板号输入框聚焦
  41. ComList.Text = BaseUtil.GetCacheData("PortName").ToString();
  42. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  43. InitPrint = new Thread(InPrint);
  44. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  45. BaseUtil.SetFormCenter(stw);
  46. stw.ShowDialog();
  47. startWeigh.PerformClick();
  48. dh = SystemInf.dh;
  49. palletcode.Focus();
  50. }
  51. protected override void OnVisibleChanged(EventArgs e)
  52. {
  53. base.OnVisibleChanged(e);
  54. if (!IsHandleCreated)
  55. {
  56. this.Close();
  57. }
  58. }
  59. private void InPrint()
  60. {
  61. try
  62. {
  63. lbl = new ApplicationClass();
  64. BaseUtil.WriteLbl();
  65. }
  66. catch (Exception ex)
  67. {
  68. OperateResult.AppendText("未正确安装CodeSoft软件\n", Color.Red);
  69. }
  70. }
  71. private void Make_PalletWeigh_SizeChanged(object sender, EventArgs e)
  72. {
  73. asc.controlAutoSize(this);
  74. }
  75. private void palletcode_KeyDown(object sender, KeyEventArgs e)
  76. {
  77. //按下了enter键
  78. if (e.KeyCode == Keys.Enter)
  79. {
  80. if (palletcode.Text == "")
  81. {
  82. OperateResult.AppendText("<<输入不能为空\n", Color.Red);
  83. return;
  84. }
  85. if (weight.Text == "")
  86. {
  87. OperateResult.AppendText("<<未读取到重量信息\n", Color.Red);
  88. return;
  89. }
  90. //校验栈板号
  91. sql.Clear();
  92. sql.Append("select pa_id,pa_prodcode,pr_detail,pa_makecode,pa_salecode,pa_totalqty,");
  93. sql.Append("pa_packageqty from package left join product on pr_code=pa_prodcode where ");
  94. sql.Append("pa_outboxcode=:code and pa_type='3'");
  95. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select", palletcode.Text);
  96. if (dt.Rows.Count == 0)
  97. {
  98. OperateResult.AppendText("<<无该栈板号\n", Color.Red, palletcode);
  99. }
  100. else
  101. {
  102. //给页面赋值
  103. BaseUtil.SetFormValue(this.Controls, dt);
  104. //刷新grid的历史称重信息;
  105. if (double.Parse(weight.Text.Replace("kg", "").Trim()) == 0)
  106. {
  107. OperateResult.AppendText("<<重量不能等于0\n", Color.Red, palletcode);
  108. return;
  109. }
  110. // 更新大箱重量
  111. sql.Clear();
  112. sql.Append("update package set pa_weight=:weight where pa_id=:pa_id ");
  113. dh.ExecuteSql(sql.GetString(), "update", weight.Text.Replace("kg", "").Trim(), dt.Rows[0]["pa_id"].ToString());
  114. //记录makeprocess
  115. //查询maid
  116. info = (DataTable)dh.ExecuteSql("select ma_id,ma_wccode from make where ma_code='" + pa_makecode.Text + "'", "select");
  117. if (info.Rows.Count > 0)
  118. {
  119. //有值
  120. //查询
  121. sql.Clear();
  122. sql.Append("insert into MakeProcess(mp_id,mp_makecode,mp_maid,mp_stepcode,mp_stepname ,");
  123. sql.Append("mp_kind,mp_result,mp_indate,mp_inman,mp_wccode,mp_linecode,mp_sourcecode,");
  124. sql.Append("mp_snoutboxcode) values ( makeprocess_seq.nextval,:makecode,:ma_id,:craftcode,:craftname,'栈板称重'");
  125. sql.Append(",:result,sysdate,:inman,:workcenter,:linecode,:source,:palletcode) ");
  126. dh.ExecuteSql(sql.GetString(), "insert", pa_makecode.Text, info.Rows[0]["ma_id"].ToString(), User.CurrentStepCode, User.CurrentStepName, "称重成功,栈板号:" + palletcode.Text + ",重量:" + weight.Text.Trim(), User.UserName, info.Rows[0]["ma_wccode"].ToString(), User.UserLineCode, User.UserSourceCode, palletcode.Text);
  127. //提示信息栏显示:<< 栈板号:XXXX,重量:xxxx 克,
  128. OperateResult.AppendText("<<栈板号:" + palletcode.Text + ",重量:" + weight.Text.Trim() + "\n", Color.Green);
  129. //记录日志
  130. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, pa_makecode.Text, User.UserLineCode, User.UserSourceCode, "栈板称重", "重量:" + weight.Text.Trim() + "\n", palletcode.Text, "");
  131. //刷新称重计数+1
  132. weightedNum.Text = double.Parse(weightedNum.Text) + 1 + "";
  133. recordResult(palletcode.Text, weight.Text.Trim(), System.DateTime.Now.ToString());
  134. //插入称重日志表
  135. //sql.Clear();
  136. //sql.Append("insert into weightlog(wl_id,wl_type,wl_scancode,wl_weight,wl_unit,wl_linecode,");
  137. //sql.Append("wl_prodcode,wl_sccode,wl_indate,wl_inman) values (weightlog_seq.nextval,'PALLET',:palletcode,");
  138. //sql.Append(":weight,'kg',:linecode,:prodcode,:sccode,sysdate,:inman)");
  139. //dh.ExecuteSql(sql.GetString(), "insert", palletcode.Text, weight.Text.Replace("kg", "").Trim(), User.UserLineCode, pa_prodcode.Text, User.UserSourceCode, User.UserName);
  140. LogicHandler.RecordProdWeight(palletcode.Text, "PALLET", float.Parse(weight.Text.Replace("kg", "").Trim()), "kg", User.UserLineCode, pa_prodcode.Text, User.UserSourceCode, User.UserName);
  141. //如果用户勾选了自动打印,调用打印, 打印成功,消息提示栏显示“打印成功”,“请输入栈板号”
  142. if (autoPrint.Checked == true)
  143. {
  144. try
  145. {
  146. if (PrintLabel.SelectedValue == null)
  147. {
  148. OperateResult.AppendText(">>产品:" + pa_prodcode.Text + "未维护栈板标签模板\n", Color.Red, palletcode);
  149. return;
  150. }
  151. //doc = lbl.Documents.Open(PrintLabel.Text);
  152. string ErrorMessage = "";
  153. if (Print.CodeSoft(Tag.ToString(), ref lbl, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, palletcode.Text, int.Parse(PrintNum.Text), pa_makecode.Text, pa_prodcode.Text, "栈板标", "0", out ErrorMessage))
  154. {
  155. //打印成功,请输入栈板号
  156. OperateResult.AppendText("<<打印成功\n", Color.Green);
  157. }
  158. else
  159. {
  160. OperateResult.AppendText(ErrorMessage + "\n", Color.Red);
  161. }
  162. }
  163. catch (Exception ex)
  164. {
  165. OperateResult.AppendText("<<打印失败\n", Color.Red);
  166. OperateResult.AppendText(">>请输入栈板号\n", Color.Black, palletcode);
  167. return;
  168. }
  169. OperateResult.AppendText(">>请输入栈板号\n", Color.Black);
  170. //更新栈板打印次数
  171. sql.Clear();
  172. sql.Append("update package set pa_printcount= pa_printcount+1 where pa_id=:pa_id");
  173. dh.ExecuteSql(sql.GetString(), "update", dt.Rows[0]["pa_id"].ToString());
  174. //清空输入框中的值,扫描框置空定焦
  175. palletcode.Text = "";
  176. }
  177. else
  178. {
  179. OperateResult.AppendText("<<需打印请勾选自动打印\n", Color.Red);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. private void pa_prodcode_TextChanged(object sender, EventArgs e)
  186. {
  187. listc = (DataTable)dh.ExecuteSql("select la_id,substr(la_url,instr(la_url,'\\',-1)+1)la_name,la_url,la_isdefault from label where la_prodcode='" + pa_prodcode.Text + "' and la_templatetype='栈板标' and la_statuscode='AUDITED' order by la_isdefault", "select");
  188. if (listc.Rows.Count == 0)
  189. {
  190. OperateResult.AppendText(">>产品:" + pa_prodcode.Text + "未维护栈板标签模板\n", Color.Red);
  191. }
  192. PrintLabel.DataSource = listc;
  193. PrintLabel.DisplayMember = "la_url";
  194. PrintLabel.ValueMember = "la_id";
  195. }
  196. private void startWeigh_Click(object sender, EventArgs e)
  197. {
  198. thread = new Thread(getSerialData);
  199. try
  200. {
  201. GetData = true;
  202. serialPort1.PortName = this.ComList.Text;
  203. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  204. serialPort1.Open();
  205. thread.Start();
  206. }
  207. catch (Exception mes)
  208. {
  209. if (BaudRate.Text == "" || ComList.Text == "")
  210. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  211. else
  212. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  213. }
  214. }
  215. //停止称重
  216. private void stopWeigh_Click(object sender, EventArgs e)
  217. {
  218. if (serialPort1.IsOpen)
  219. {
  220. GetData = false;
  221. serialPort1.Close();
  222. SystemInf.OpenPort.Remove(serialPort1.PortName);
  223. thread.Abort();
  224. }
  225. }
  226. private void getSerialData()
  227. {
  228. if (serialPort1.IsOpen)
  229. {
  230. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  231. {
  232. SystemInf.OpenPort.Add(serialPort1.PortName);
  233. try
  234. {
  235. while (GetData)
  236. {
  237. try
  238. {
  239. //weight.Text = Regex.Replace(serialPort1.ReadLine(), "\\D+", "");
  240. weight.Text = serialPort1.ReadLine();
  241. }
  242. catch (Exception)
  243. {
  244. GetData = false;
  245. }
  246. }
  247. }
  248. catch (IOException ex) { MessageBox.Show(ex.Message); }
  249. }
  250. else
  251. MessageBox.Show("端口已被占用,请关闭其他窗口");
  252. }
  253. }
  254. private void Make_PalletWeigh_FormClosing(object sender, FormClosingEventArgs e)
  255. {
  256. BaseUtil.ClosePrint(lbl);
  257. stopWeigh.PerformClick();
  258. InitPrint.Abort();
  259. if (serialPort1.IsOpen)
  260. {
  261. GetData = false;
  262. serialPort1.Close();
  263. SystemInf.OpenPort.Remove(serialPort1.PortName);
  264. thread.Interrupt();
  265. }
  266. //thread.Abort();
  267. }
  268. private void recordResult(string palletcode, string weigh, string time)
  269. {
  270. //if (showResult.Items.Count >= 50)
  271. //{
  272. // //如果有10行的话,则删除最前一行
  273. // showResult.Items.RemoveAt(0);
  274. //}
  275. //创建一个item
  276. ListViewItem lvi = new ListViewItem();
  277. //分条赋值
  278. lvi.SubItems.Add(palletcode);
  279. lvi.SubItems.Add(weigh);
  280. lvi.SubItems.Add(time);
  281. //添加结果的信息进去
  282. showResult.Items.Add(lvi);
  283. }
  284. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  285. {
  286. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  287. {
  288. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  289. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  290. }
  291. }
  292. }
  293. }