Packing_BigBoxWeight.cs 13 KB

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