Packing_BigBoxWeight.cs 14 KB

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