Packing_BigBoxWeight.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 pa_weight,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. string pa_weight = dt.Rows[0]["pa_weight"].ToString();
  117. if (pa_weight != "")
  118. {
  119. OperateResult.AppendText(">>箱号" + pa_outboxcode.Text + "已经称重\n", Color.Red);
  120. return;
  121. }
  122. //赋值重量单位
  123. Weight = double.Parse(_weight == "" ? "0" : _weight);
  124. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  125. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  126. if (Weight - MinWeight == MaxWeight - Weight)
  127. pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight);
  128. else
  129. pr_colorboxgw.Text = MinWeight + "-" + MaxWeight;
  130. if ((ActualWeight >= MinWeight) && (ActualWeight <= MaxWeight))
  131. {
  132. OperateResult.AppendText(">>大箱" + pa_outboxcode.Text + "重量检测检测合格\n", Color.Green);
  133. }
  134. else
  135. {
  136. OperateResult.AppendText(">>大箱" + pa_outboxcode.Text + "重量检测未通过\n", Color.Red);
  137. return;
  138. }
  139. //更新大箱重量
  140. sql.Clear();
  141. sql.Append("update package set pa_weight='" + ActualWeight + "' where pa_outboxcode= '" + pa_outboxcode.Text + "'");
  142. dh.ExecuteSql(sql.GetString(), "update");
  143. OperateResult.AppendText("<<<箱号:" + pa_outboxcode.Text + ",重量:" + weight.Text + "\n", Color.Green);
  144. LogicHandler.InsertMakeProcess(pa_outboxcode.Text, pa_makecode.Text, User.UserSourceCode, "大箱称量", "大箱称量合格", User.UserCode);
  145. OperateResult.AppendText("<<<更新成功\n", Color.Green);
  146. //刷新grid的历史称重信息;
  147. recordResult(pa_outboxcode.Text, weight.Text, System.DateTime.Now.ToString());
  148. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, pa_makecode.Text, User.UserLineCode, User.UserSourceCode, "大箱" + pa_outboxcode.Text + "称重:" + weight.Text, "称重成功", pa_outboxcode.Text, "");
  149. LogicHandler.RecordProdWeight(pa_outboxcode.Text, "BIGBOX", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pa_prodcode.Text, User.UserSourceCode, User.UserName);
  150. if (autoprint.Checked == true)
  151. {
  152. try
  153. {
  154. //doc = lbl.Documents.Open(ftpOperater.DownLoadTo + PrintLabel.Text);
  155. string ErrorMessage = "";
  156. 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))
  157. {
  158. OperateResult.AppendText(ErrorMessage + "\n", Color.Red);
  159. }
  160. dh.ExecuteSql("update package set pa_printcount= pa_printcount+1 where pa_outboxcode='" + pa_outboxcode.Text + "'", "update");
  161. printcount++;
  162. weightsum.Text = printcount + "";
  163. //清空输入框中的值,扫描框置空定焦
  164. pa_outboxcode.Text = "";
  165. pa_outboxcode.Focus();
  166. }
  167. catch (Exception eb)
  168. {
  169. if (PrintLabel.SelectedValue == null)
  170. {
  171. OperateResult.AppendText(">>产品:" + pa_prodcode.Text + "未维护大箱标签模板\n", Color.Red, pa_outboxcode);
  172. pa_outboxcode.Focus();
  173. }
  174. else OperateResult.AppendText("<<<打印失败:" + eb.Message + "\n", Color.Red);
  175. }
  176. }
  177. }
  178. else
  179. {
  180. OperateResult.AppendText("<<<大箱重量应大于0\n", Color.Red);
  181. }
  182. }
  183. }
  184. private void recordResult(string pa_outboxcode, string weight, string dateTime)
  185. {
  186. //创建一个item
  187. ListViewItem lvi = new ListViewItem();
  188. //分条赋值
  189. lvi.SubItems.Add(pa_outboxcode);
  190. lvi.SubItems.Add(weight);
  191. lvi.SubItems.Add(dateTime);
  192. //添加结果的信息进去
  193. showResult.Items.Add(lvi);
  194. }
  195. private void pa_prodcode_TextChanged(object sender, EventArgs e)
  196. {
  197. 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");
  198. PrintLabel.DataSource = _dt;
  199. PrintLabel.DisplayMember = "la_url";
  200. PrintLabel.ValueMember = "la_id";
  201. }
  202. private void StartWeight_Click(object sender, EventArgs e)
  203. {
  204. thread = new Thread(getSerialData);
  205. try
  206. {
  207. GetData = true;
  208. serialPort1.PortName = Comlist;
  209. serialPort1.BaudRate = int.Parse(Baurate);
  210. serialPort1.Open();
  211. thread.Start();
  212. }
  213. catch (Exception mes)
  214. {
  215. if (Baurate == "" || Comlist == "")
  216. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  217. else
  218. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  219. }
  220. }
  221. Regex re = new Regex("\\d+.\\w+");
  222. private void getSerialData()
  223. {
  224. if (serialPort1.IsOpen)
  225. {
  226. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  227. {
  228. SystemInf.OpenPort.Add(serialPort1.PortName);
  229. try
  230. {
  231. while (GetData)
  232. {
  233. try
  234. {
  235. weight.Text = re.Match(serialPort1.ReadLine().Trim()).Groups[0].Value;
  236. }
  237. catch (Exception)
  238. {
  239. GetData = false;
  240. }
  241. }
  242. }
  243. catch (IOException ex) { MessageBox.Show(ex.Message); }
  244. }
  245. else
  246. MessageBox.Show("端口已被占用,请关闭其他窗口");
  247. }
  248. }
  249. private void StopWeight_Click(object sender, EventArgs e)
  250. {
  251. if (serialPort1.IsOpen)
  252. {
  253. GetData = false;
  254. serialPort1.Close();
  255. SystemInf.OpenPort.Remove(serialPort1.PortName);
  256. thread.Abort();
  257. }
  258. }
  259. private void Make_BigBoxWeight_FormClosing(object sender, FormClosingEventArgs e)
  260. {
  261. BaseUtil.ClosePrint(lbl);
  262. StopWeight.PerformClick();
  263. InitPrint.Abort();
  264. if (serialPort1.IsOpen)
  265. {
  266. GetData = false;
  267. serialPort1.Close();
  268. SystemInf.OpenPort.Remove(serialPort1.PortName);
  269. thread.Interrupt();
  270. }
  271. //thread.Abort();
  272. }
  273. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  274. {
  275. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  276. {
  277. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  278. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  279. }
  280. }
  281. Double Weight;
  282. //最大重量
  283. Double MaxWeight;
  284. //最小重量
  285. Double MinWeight;
  286. private void RefreshWeigh_Click(object sender, EventArgs e)
  287. {
  288. sql.Clear();
  289. sql.Append("select pr_colorboxgw,pr_bigboxmaxw,pr_bigboxminw,pr_colorboxunit from product where pr_code='" + pa_prodcode.Text + "'");
  290. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  291. if (dt.Rows.Count > 0)
  292. {
  293. //重量的临时变量
  294. string _weight = dt.Rows[0]["pr_colorboxgw"].ToString();
  295. string _maxweight = dt.Rows[0]["pr_bigboxmaxw"].ToString();
  296. string _minweight = dt.Rows[0]["pr_bigboxminw"].ToString();
  297. //赋值重量单位
  298. Weight = double.Parse(_weight == "" ? "0" : _weight);
  299. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  300. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  301. if (Weight - MinWeight == MaxWeight - Weight)
  302. pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_colorboxunit"].ToString();
  303. else
  304. pr_colorboxgw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_colorboxunit"].ToString();
  305. }
  306. }
  307. }
  308. }