Packing_PalletWeigh.cs 14 KB

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