Packing_BigBoxWeight.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using DevExpress.Printing.Core.PdfExport.Metafile;
  2. using LabelManager2;
  3. using Seagull.BarTender.Print;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.IO.Ports;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Threading;
  15. using System.Windows.Forms;
  16. using UAS_MES_NEW.DataOperate;
  17. using UAS_MES_NEW.Entity;
  18. using UAS_MES_NEW.PublicForm;
  19. using UAS_MES_NEW.PublicMethod;
  20. using static DevExpress.XtraBars.Painters.PrimitivesPainterWindowsXP;
  21. using static System.Runtime.CompilerServices.RuntimeHelpers;
  22. namespace UAS_MES_NEW.Packing
  23. {
  24. public partial class Packing_BigBoxWeigh : Form
  25. {
  26. AutoSizeFormClass asc = new AutoSizeFormClass();
  27. DataHelper dh;
  28. Document doc;
  29. //true的时候表示从串口读取数据
  30. bool GetData = true;
  31. Engine engine;
  32. DataTable dt;
  33. LogStringBuilder sql = new LogStringBuilder();
  34. ApplicationClass lbl;
  35. Thread thread;
  36. Thread InitPrint;
  37. //打印计数
  38. int printcount;
  39. //创建串口实例
  40. SerialPort serialPort1 = new SerialPort();
  41. //存储当前大箱号
  42. string outboxcode = "";
  43. //记录称重串口号和波特率
  44. string Comlist = "";
  45. string Baurate = "";
  46. public Packing_BigBoxWeigh()
  47. {
  48. InitializeComponent();
  49. }
  50. protected override void OnVisibleChanged(EventArgs e)
  51. {
  52. base.OnVisibleChanged(e);
  53. if (!IsHandleCreated)
  54. {
  55. this.Close();
  56. }
  57. }
  58. private void Make_BigBoxWeight_Load(object sender, EventArgs e)
  59. {
  60. asc.controllInitializeSize(this);
  61. CheckForIllegalCrossThreadCalls = false;
  62. Comlist = BaseUtil.GetCacheData("PortName").ToString();
  63. Baurate = BaseUtil.GetCacheData("BaudRate").ToString();
  64. InitPrint = new Thread(InPrint);
  65. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  66. BaseUtil.SetFormCenter(stw);
  67. stw.ShowDialog();
  68. pa_outboxcode.Focus();
  69. OperateResult.AppendText(">>请输入大箱号\n");
  70. printcount = 0;
  71. weightsum.Text = printcount + "";
  72. StartWeight.PerformClick();
  73. dh = SystemInf.dh;
  74. }
  75. private void InPrint()
  76. {
  77. try
  78. {
  79. engine = new Engine(true);
  80. }
  81. catch
  82. {
  83. OperateResult.AppendText("未正确安装BarTender软件\n", Color.Red);
  84. }
  85. }
  86. private void Make_BigBoxWeight_SizeChanged(object sender, EventArgs e)
  87. {
  88. asc.controlAutoSize(this);
  89. }
  90. private void pa_outboxcode_KeyDown(object sender, KeyEventArgs e)
  91. {
  92. if (e.KeyCode == Keys.Enter)
  93. {
  94. sql.Clear();
  95. sql.Append("select pa_weight,pr_bigboxmaxw,pr_bigboxminw,pr_colorboxgw,pa_id,pa_prodcode,pr_spec,pr_detail,pa_makecode,pa_salecode,pa_totalqty,");
  96. sql.Append("pa_packageqty from package left join product on pr_code=pa_prodcode where ");
  97. sql.Append("pa_outboxcode='" + pa_outboxcode.Text + "' and pa_type=2");
  98. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  99. outboxcode = pa_outboxcode.Text;
  100. weightsum.Text = printcount + "";
  101. if (dt.Rows.Count > 0)
  102. {
  103. BaseUtil.SetFormValue(this.Controls, dt);
  104. pa_outboxcode.Text = outboxcode;
  105. }
  106. else
  107. {
  108. OperateResult.AppendText(">>箱号不存在\n", Color.Red);
  109. return;
  110. }
  111. double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text.Replace("kg", "").Trim());
  112. if (ActualWeight > 0)
  113. {
  114. string _weight = dt.Rows[0]["pr_colorboxgw"].ToString();
  115. string _maxweight = dt.Rows[0]["pr_bigboxmaxw"].ToString();
  116. string _minweight = dt.Rows[0]["pr_bigboxminw"].ToString();
  117. string pa_weight = dt.Rows[0]["pa_weight"].ToString();
  118. if (pa_weight != "")
  119. {
  120. OperateResult.AppendText(">>箱号" + pa_outboxcode.Text + "已经称重\n", Color.Red);
  121. return;
  122. }
  123. //赋值重量单位
  124. Weight = double.Parse(_weight == "" ? "0" : _weight);
  125. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  126. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  127. if (Weight - MinWeight == MaxWeight - Weight)
  128. pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight);
  129. else
  130. pr_colorboxgw.Text = MinWeight + "-" + MaxWeight;
  131. if ((ActualWeight >= MinWeight) && (ActualWeight <= MaxWeight))
  132. {
  133. OperateResult.AppendText(">>大箱" + pa_outboxcode.Text + "重量检测检测合格\n", Color.Green);
  134. }
  135. else
  136. {
  137. OperateResult.AppendText(">>大箱" + pa_outboxcode.Text + "重量检测未通过\n", Color.Red);
  138. return;
  139. }
  140. //更新大箱重量
  141. sql.Clear();
  142. sql.Append("update package set pa_weight='" + ActualWeight + "' where pa_outboxcode= '" + pa_outboxcode.Text + "'");
  143. dh.ExecuteSql(sql.GetString(), "update");
  144. OperateResult.AppendText("<<<箱号:" + pa_outboxcode.Text + ",重量:" + weight.Text + "\n", Color.Green);
  145. LogicHandler.InsertMakeProcess(pa_outboxcode.Text, pa_makecode.Text, User.UserSourceCode, "大箱称量", "大箱称量合格", User.UserCode);
  146. OperateResult.AppendText("<<<更新成功\n", Color.Green);
  147. //刷新grid的历史称重信息;
  148. recordResult(pa_outboxcode.Text, weight.Text, System.DateTime.Now.ToString());
  149. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, pa_makecode.Text, User.UserLineCode, User.UserSourceCode, "大箱" + pa_outboxcode.Text + "称重:" + weight.Text, "称重成功", pa_outboxcode.Text, "");
  150. LogicHandler.RecordProdWeight(pa_outboxcode.Text, "BIGBOX", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pa_prodcode.Text, User.UserSourceCode, User.UserName);
  151. if (autoprint.Checked == true)
  152. {
  153. try
  154. {
  155. //doc = lbl.Documents.Open(ftpOperater.DownLoadTo + PrintLabel.Text);
  156. string ErrorMessage = "";
  157. if (!Print.BarTender(Tag.ToString(), ref engine, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), Printer.Text, pa_outboxcode.Text, int.Parse(PrintNum.Text), pa_makecode.Text, pa_prodcode.Text, "大箱标", "0", out ErrorMessage))
  158. {
  159. OperateResult.AppendText(ErrorMessage + "\n", Color.Red);
  160. }
  161. dh.ExecuteSql("update package set pa_printcount= pa_printcount+1 where pa_outboxcode='" + pa_outboxcode.Text + "'", "update");
  162. printcount++;
  163. weightsum.Text = printcount + "";
  164. //清空输入框中的值,扫描框置空定焦
  165. pa_outboxcode.Text = "";
  166. pa_outboxcode.Focus();
  167. }
  168. catch (Exception eb)
  169. {
  170. if (PrintLabel.SelectedValue == null)
  171. {
  172. OperateResult.AppendText(">>产品:" + pa_prodcode.Text + "未维护大箱标签模板\n", Color.Red, pa_outboxcode);
  173. pa_outboxcode.Focus();
  174. }
  175. else OperateResult.AppendText("<<<打印失败:" + eb.Message + "\n", Color.Red);
  176. }
  177. }
  178. }
  179. else
  180. {
  181. OperateResult.AppendText("<<<大箱重量应大于0\n", Color.Red);
  182. }
  183. }
  184. }
  185. private void recordResult(string pa_outboxcode, string weight, string dateTime)
  186. {
  187. //创建一个item
  188. ListViewItem lvi = new ListViewItem();
  189. //分条赋值
  190. lvi.SubItems.Add(pa_outboxcode);
  191. lvi.SubItems.Add(weight);
  192. lvi.SubItems.Add(dateTime);
  193. //添加结果的信息进去
  194. showResult.Items.Add(lvi);
  195. }
  196. private void pa_prodcode_TextChanged(object sender, EventArgs e)
  197. {
  198. DataTable _dt = (DataTable)dh.ExecuteSql("select la_id,la_url,la_isdefault from label where la_prodcode='" + pa_prodcode.Text + "' and la_templatetype='大箱标' and la_statuscode='AUDITED' order by la_isdefault", "select");
  199. PrintLabel.DataSource = _dt;
  200. PrintLabel.DisplayMember = "la_url";
  201. PrintLabel.ValueMember = "la_id";
  202. }
  203. private void StartWeight_Click(object sender, EventArgs e)
  204. {
  205. thread = new Thread(getSerialData);
  206. try
  207. {
  208. GetData = true;
  209. serialPort1.PortName = Comlist;
  210. serialPort1.BaudRate = int.Parse(Baurate);
  211. serialPort1.Open();
  212. thread.Start();
  213. }
  214. catch (Exception mes)
  215. {
  216. if (Baurate == "" || Comlist == "")
  217. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  218. else
  219. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  220. }
  221. }
  222. Regex re = new Regex("\\d+.\\w+");
  223. private void getSerialData()
  224. {
  225. if (serialPort1.IsOpen)
  226. {
  227. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  228. {
  229. SystemInf.OpenPort.Add(serialPort1.PortName);
  230. try
  231. {
  232. while (GetData)
  233. {
  234. try
  235. {
  236. weight.Text = re.Match(serialPort1.ReadLine().Trim()).Groups[0].Value;
  237. }
  238. catch (Exception)
  239. {
  240. GetData = false;
  241. }
  242. }
  243. }
  244. catch (IOException ex) { MessageBox.Show(ex.Message); }
  245. }
  246. else
  247. MessageBox.Show("端口已被占用,请关闭其他窗口");
  248. }
  249. }
  250. private void StopWeight_Click(object sender, EventArgs e)
  251. {
  252. if (serialPort1.IsOpen)
  253. {
  254. GetData = false;
  255. serialPort1.Close();
  256. SystemInf.OpenPort.Remove(serialPort1.PortName);
  257. thread.Abort();
  258. }
  259. }
  260. private void Make_BigBoxWeight_FormClosing(object sender, FormClosingEventArgs e)
  261. {
  262. BaseUtil.ClosePrint(lbl);
  263. StopWeight.PerformClick();
  264. InitPrint.Abort();
  265. if (serialPort1.IsOpen)
  266. {
  267. GetData = false;
  268. serialPort1.Close();
  269. SystemInf.OpenPort.Remove(serialPort1.PortName);
  270. thread.Interrupt();
  271. }
  272. //thread.Abort();
  273. }
  274. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  275. {
  276. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  277. {
  278. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  279. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  280. }
  281. }
  282. Double Weight;
  283. //最大重量
  284. Double MaxWeight;
  285. //最小重量
  286. Double MinWeight;
  287. private void RefreshWeigh_Click(object sender, EventArgs e)
  288. {
  289. sql.Clear();
  290. sql.Append("select pr_colorboxgw,pr_bigboxmaxw,pr_bigboxminw,pr_colorboxunit from product where pr_code='" + pa_prodcode.Text + "'");
  291. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  292. if (dt.Rows.Count > 0)
  293. {
  294. //重量的临时变量
  295. string _weight = dt.Rows[0]["pr_colorboxgw"].ToString();
  296. string _maxweight = dt.Rows[0]["pr_bigboxmaxw"].ToString();
  297. string _minweight = dt.Rows[0]["pr_bigboxminw"].ToString();
  298. //赋值重量单位
  299. Weight = double.Parse(_weight == "" ? "0" : _weight);
  300. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  301. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  302. if (Weight - MinWeight == MaxWeight - Weight)
  303. pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_colorboxunit"].ToString();
  304. else
  305. pr_colorboxgw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_colorboxunit"].ToString();
  306. }
  307. }
  308. private void Packing_Click(object sender, EventArgs e)
  309. {
  310. if (pa_outboxcode.Text != "")
  311. {
  312. sql.Clear();
  313. sql.Append("select nvl(pa_status,0) pa_status from packagedetail left join package ");
  314. sql.Append("on pa_id=pd_paid where pd_outboxcode='" + pa_outboxcode.Text + "'");
  315. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  316. if (dt.Rows.Count > 0)
  317. {
  318. if (dt.Rows[0]["pa_status"].ToString() == "0")
  319. {
  320. string Seal = MessageBox.Show(this.ParentForm, "是否确认封箱?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  321. if (Seal == "Yes")
  322. {
  323. OperateResult.AppendText(">>大箱号" + pa_outboxcode.Text + "封大箱成功\n", Color.Green);
  324. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, pa_makecode.Text, User.UserLineCode, User.UserSourceCode, "大箱采集", "大箱封箱" + pa_outboxcode.Text + "成功", pa_outboxcode.Text, "");
  325. dh.ExecuteSql("update package set pa_status=1 where pa_outboxcode='" + pa_outboxcode.Text + "'", "update");
  326. }
  327. }
  328. else OperateResult.AppendText(">>大箱号" + pa_outboxcode.Text + "已封大箱\n", Color.Red);
  329. }
  330. else OperateResult.AppendText(">>大箱号" + pa_outboxcode.Text + "未采集内容\n", Color.Red);
  331. }
  332. else OperateResult.AppendText(">>大箱号不能为空\n", Color.Red);
  333. }
  334. }
  335. }