Make_CartonBoxWeigh.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using LabelManager2;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.IO.Ports;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12. using UAS_MES.DataOperate;
  13. using UAS_MES.Entity;
  14. using UAS_MES.PublicForm;
  15. using UAS_MES.PublicMethod;
  16. namespace UAS_MES.Make
  17. {
  18. public partial class Make_CartonBoxWeigh : Form
  19. {
  20. AutoSizeFormClass asc = new AutoSizeFormClass();
  21. DataHelper dh;
  22. DataTable dt;
  23. LogStringBuilder sql = new LogStringBuilder();
  24. ApplicationClass lbl;
  25. Document doc;
  26. Thread thread;
  27. //创建串口实例
  28. SerialPort serialPort1 = new SerialPort();
  29. //true的时候表示从串口读取数据
  30. bool GetData = true;
  31. //箱内总数
  32. int pa_totalqty;
  33. //称量的标准重量
  34. Double Weight;
  35. //最大重量
  36. Double MaxWeight;
  37. //最小重量
  38. Double MinWeight;
  39. DataTable ListC = new DataTable();
  40. Thread InitPrint;
  41. public Make_CartonBoxWeigh()
  42. {
  43. InitializeComponent();
  44. }
  45. protected override void OnVisibleChanged(EventArgs e)
  46. {
  47. base.OnVisibleChanged(e);
  48. if (!IsHandleCreated)
  49. {
  50. this.Close();
  51. }
  52. }
  53. private void 卡通箱称重_Load(object sender, EventArgs e)
  54. {
  55. CheckForIllegalCrossThreadCalls = false;
  56. asc.controllInitializeSize(this);
  57. ComList.Text = BaseUtil.GetCacheData("PortName").ToString();
  58. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  59. InitPrint = new Thread(InPrint);
  60. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  61. BaseUtil.SetFormCenter(stw);
  62. stw.ShowDialog();
  63. StartWeight.PerformClick();
  64. dh = new DataHelper();
  65. }
  66. private void InPrint()
  67. {
  68. try
  69. {
  70. lbl = new ApplicationClass();
  71. BaseUtil.WriteLbl(lbl);
  72. }
  73. catch (Exception)
  74. {
  75. OperateResult.AppendText("未正确安装CodeSoft软件\n", Color.Red);
  76. }
  77. }
  78. private void 卡通箱称重_SizeChanged(object sender, EventArgs e)
  79. {
  80. asc.controlAutoSize(this);
  81. }
  82. private void sncode_KeyDown(object sender, KeyEventArgs e)
  83. {
  84. if (e.KeyCode == Keys.Enter)
  85. {
  86. //获取序列号的箱号
  87. string ms_outboxcode = dh.getFieldDataByCondition("makeserial", "ms_outboxcode", "ms_sncode='" + outboxcode.Text + "' order by ms_id desc").ToString();
  88. if (ms_outboxcode != "")
  89. {
  90. outboxcode.Text = ms_outboxcode;
  91. }
  92. //根据箱号查询表单数据
  93. sql.Clear();
  94. sql.Append("select pa_totalqty,ma_code,mcd_inqty,ma_qty,ma_qty-mcd_inqty as mcd_waitqty,ma_salecode,pr_detail,pr_cartonunit,pr_code,pr_cartongw,");
  95. sql.Append("pr_cartonmaxw,pr_cartonminw,nvl(PR_CHECKCARTONW,'0') PR_CHECKCARTONW from package left join product on pr_code=pa_prodcode left join make on ma_prodcode=pr_code ");
  96. sql.Append("left join makecraftdetail on mcd_macode=ma_code where pa_outboxcode='" + outboxcode.Text + "' and mcd_stepcode='" + User.CurrentStepCode + "'");
  97. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  98. //填充Form的值
  99. BaseUtil.SetFormValue(this.Controls, dt);
  100. //填充打印文件选项的DataGridView
  101. if (dt.Rows.Count > 0)
  102. {
  103. string ErrorMessage;
  104. //重量的临时变量
  105. string PR_CHECKCARTONW = dt.Rows[0]["PR_CHECKCARTONW"].ToString();
  106. string _weight = dt.Rows[0]["pr_cartongw"].ToString();
  107. string _maxweight = dt.Rows[0]["pr_cartonmaxw"].ToString();
  108. string _minweight = dt.Rows[0]["pr_cartonminw"].ToString();
  109. string pa_pr_cartonunit = dt.Rows[0]["pr_cartonunit"].ToString();
  110. Weight = double.Parse(_weight == "" ? "0" : _weight);
  111. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  112. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  113. pa_totalqty = int.Parse(dt.Rows[0]["pa_totalqty"].ToString());
  114. if (Weight - MinWeight == MaxWeight - Weight)
  115. pr_cartongw.Text = Weight + "±" + (MaxWeight - Weight);
  116. else
  117. pr_cartongw.Text = MinWeight + "-" + MaxWeight;
  118. //if (pr_cartonunit.Text == "kg")
  119. //{
  120. // weight.Text = (float.Parse(weight.Text) / 1000).ToString();
  121. //}
  122. double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text.Replace("kg", "").Trim());
  123. //需要检查称重重量
  124. if (PR_CHECKCARTONW != "0")
  125. {
  126. //称量合格或不合格都记录重量
  127. if (ActualWeight >= MinWeight && ActualWeight <= MaxWeight)
  128. {
  129. LogicHandler.DoCommandLog(Tag.ToString(), User.UserSourceCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "卡通箱称重", "称量合格", outboxcode.Text, "");
  130. OperateResult.AppendText(">>箱号" + outboxcode.Text + "检测合格\n", Color.Green);
  131. }
  132. else
  133. {
  134. LogicHandler.DoCommandLog(Tag.ToString(), User.UserSourceCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "卡通箱称重", "称量不合格", outboxcode.Text, "");
  135. OperateResult.AppendText(">>箱号" + outboxcode.Text + "检测未通过\n", Color.Red);
  136. return;
  137. }
  138. }
  139. if (AutoPrint.Checked)
  140. {
  141. if (PrintLabel.Items.Count > 0)
  142. {
  143. doc = lbl.Documents.Open(ftpOperater.DownLoadTo + PrintLabel.Text);
  144. Print.CodeSoft(Tag.ToString(), doc, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), PrinterList.Text, outboxcode.Text, int.Parse(PrintNum.Text));
  145. }
  146. else
  147. {
  148. OperateResult.AppendText(">>产品" + pr_code.Text + "未维护卡通标签\n", Color.Green);
  149. }
  150. }
  151. ListViewItem lsi = new ListViewItem();
  152. lsi.SubItems.Add(outboxcode.Text);
  153. lsi.SubItems.Add(ActualWeight.ToString() + pr_cartonunit.Text);
  154. lsi.SubItems.Add(System.DateTime.Now.ToString());
  155. WeighRecord.Items.Add(lsi);
  156. OperateResult.AppendText(">>箱号" + outboxcode.Text + "称重完成\n", Color.Green);
  157. dh.ExecuteSql("update package set pa_weight='" + weight.Text + "', pa_printcount= nvl(pa_printcount,0)+1 where pa_outboxcode='" + outboxcode.Text + "'", "update");
  158. outboxcode.Clear();
  159. }
  160. else OperateResult.AppendText(">>箱号" + outboxcode.Text + "不存在\n", Color.Red, outboxcode);
  161. }
  162. }
  163. private void getSerialData()
  164. {
  165. if (serialPort1.IsOpen)
  166. {
  167. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  168. {
  169. SystemInf.OpenPort.Add(serialPort1.PortName);
  170. try
  171. {
  172. while (GetData)
  173. {
  174. try
  175. {
  176. weight.Text = serialPort1.ReadLine();
  177. //weight.Text = Regex.Replace(serialPort1.ReadLine(), "\\D+", "");
  178. }
  179. catch (Exception)
  180. {
  181. GetData = false;
  182. }
  183. }
  184. }
  185. catch (IOException ex) { OperateResult.AppendText(">>" + ex.Message + "\n", Color.Red, outboxcode); }
  186. }
  187. else OperateResult.AppendText(">>端口已被占用,请关闭其他窗口\n", Color.Red, outboxcode);
  188. }
  189. }
  190. //确认打印
  191. private void Confirm_Click(object sender, EventArgs e)
  192. {
  193. doc = lbl.Documents.Open(ftpOperater.DownLoadTo + PrintLabel.Text);
  194. Print.CodeSoft(Tag.ToString(), doc, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), PrinterList.Text, outboxcode.Text, int.Parse(PrintNum.Text));
  195. }
  196. //停止进程,关闭串口
  197. private void StopWeight_Click(object sender, EventArgs e)
  198. {
  199. GetData = false;
  200. SystemInf.OpenPort.Remove(serialPort1.PortName);
  201. serialPort1.Close();
  202. }
  203. //关闭窗口的时候停止进程,不再读取串口数据
  204. private void 卡通箱称重_FormClosing(object sender, FormClosingEventArgs e)
  205. {
  206. BaseUtil.ClosePrint(lbl);
  207. StopWeight.PerformClick();
  208. if (serialPort1.IsOpen)
  209. {
  210. GetData = false;
  211. serialPort1.Close();
  212. SystemInf.OpenPort.Remove(serialPort1.PortName);
  213. thread.Interrupt();
  214. }
  215. }
  216. private void StartWeight_Click(object sender, EventArgs e)
  217. {
  218. thread = new Thread(getSerialData);
  219. try
  220. {
  221. GetData = true;
  222. serialPort1.PortName = this.ComList.Text;
  223. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  224. serialPort1.Open();
  225. thread.Start();
  226. }
  227. catch (Exception mes)
  228. {
  229. if (BaudRate.Text == "" || ComList.Text == "")
  230. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  231. else
  232. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  233. }
  234. }
  235. private void pr_code_TextChanged(object sender, EventArgs e)
  236. {
  237. 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='" + pr_code.Text + "' and la_templatetype='卡通箱标' and la_statuscode='AUDITED' order by la_isdefault", "select");
  238. PrintLabel.DataSource = dt;
  239. PrintLabel.DisplayMember = "la_name";
  240. PrintLabel.ValueMember = "la_id";
  241. ftpOperater ftp = new ftpOperater();
  242. for (int i = 0; i < dt.Rows.Count; i++)
  243. {
  244. BaseUtil.GetPrintLabel(dt.Rows[i]["la_name"].ToString(), dt.Rows[i]["la_url"].ToString());
  245. }
  246. }
  247. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  248. {
  249. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  250. {
  251. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  252. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  253. }
  254. }
  255. }
  256. }