Make_ColorBoxWeigh.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Data;
  4. using System.IO;
  5. using System.IO.Ports;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading;
  9. using System.Windows.Forms;
  10. using UAS_MES.DataOperate;
  11. using UAS_MES.Entity;
  12. using UAS_MES.PublicMethod;
  13. using System.Drawing;
  14. using LabelManager2;
  15. using UAS_MES.PublicForm;
  16. namespace UAS_MES.Make
  17. {
  18. public partial class Make_ColorBoxWeigh : Form
  19. {
  20. AutoSizeFormClass asc = new AutoSizeFormClass();
  21. ApplicationClass lbl;
  22. Document doc;
  23. DataHelper dh;
  24. DataTable dt;
  25. LogStringBuilder sql = new LogStringBuilder();
  26. //启用线程进行称重数据读取
  27. Thread thread;
  28. SerialPort serialPort1 = new SerialPort();
  29. //称量的标准重量
  30. Double Weight;
  31. //最大重量
  32. Double MaxWeight;
  33. //最小重量
  34. Double MinWeight;
  35. //是否通过串口获取数据
  36. bool GetData = true;
  37. string LastSncode;
  38. bool WeightChange = false;
  39. string ErrorMessage;
  40. Thread InitPrint;
  41. System.DateTime[] indate;
  42. private bool AutoCut;
  43. public Make_ColorBoxWeigh()
  44. {
  45. InitializeComponent();
  46. CheckForIllegalCrossThreadCalls = false;
  47. }
  48. private void Make_ColorBoxWeigh_Load(object sender, EventArgs e)
  49. {
  50. asc.controllInitializeSize(this);
  51. ComList.Text = BaseUtil.GetCacheData("PortName").ToString();
  52. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  53. InitPrint = new Thread(InPrint);
  54. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  55. BaseUtil.SetFormCenter(stw);
  56. stw.ShowDialog();
  57. StartWeight.PerformClick();
  58. dh = SystemInf.dh;
  59. StepCount.StepCode = User.CurrentStepCode;
  60. StepCount.Source = User.UserSourceCode;
  61. StepCount.LineCode = User.UserLineCode;
  62. StepCount.Dh = dh;
  63. StepCount.Start();
  64. }
  65. private void InPrint()
  66. {
  67. try
  68. {
  69. lbl = new ApplicationClass();
  70. BaseUtil.WriteLbl(lbl);
  71. }
  72. catch (Exception ex)
  73. {
  74. OperateResult.AppendText("未正确安装CodeSoft软件\n", Color.Red);
  75. }
  76. }
  77. private void sncode_KeyDown(object sender, KeyEventArgs e)
  78. {
  79. if (e.KeyCode == Keys.Enter)
  80. {
  81. if (LogicHandler.CheckStepAttribute(Tag.ToString(), User.UserSourceCode, out ErrorMessage))
  82. {
  83. sql.Clear();
  84. sql.Append("select ms_makecode,ma_qty,ma_salecode,pr_detail,pr_colorboxunit,pr_code,pr_colorboxgw,");
  85. sql.Append("pr_colorboxunit,pr_colorboxmaxw,pr_colorboxminw,nvl(PR_CHECKCOLORBOXW,'0') PR_CHECKCOLORBOXW,nvl(pr_sendchecktype,'LineCode')");
  86. sql.Append("pr_sendchecktype from makeserial left join make on ms_makecode=ma_code left join product on ");
  87. sql.Append("ms_prodcode=pr_code where ms_sncode='" + sncode.Text + "' order by ms_id desc");
  88. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  89. if (dt.Rows.Count > 0)
  90. {
  91. //重量的临时变量
  92. string _weight = dt.Rows[0]["pr_colorboxgw"].ToString();
  93. string _maxweight = dt.Rows[0]["pr_colorboxmaxw"].ToString();
  94. string _minweight = dt.Rows[0]["pr_colorboxminw"].ToString();
  95. string pr_colorunit = dt.Rows[0]["pr_colorboxunit"].ToString();
  96. string PR_CHECKCOLORBOXW = dt.Rows[0]["PR_CHECKCOLORBOXW"].ToString();
  97. //赋值重量单位
  98. Weight = double.Parse(_weight == "" ? "0" : _weight);
  99. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  100. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  101. BaseUtil.SetFormValue(this.Controls, dt);
  102. if (Weight - MinWeight == MaxWeight - Weight)
  103. pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_colorboxunit"].ToString();
  104. else
  105. pr_colorboxgw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_colorboxunit"].ToString();
  106. string oMakeCode = "";
  107. string oMsID = "";
  108. if (LogicHandler.CheckStepSNAndMacode(ms_makecode.Text, User.UserSourceCode, sncode.Text, User.UserCode, out oMakeCode, out oMsID, out ErrorMessage))
  109. {
  110. string nextstepcode = dh.getFieldDataByCondition("makeserial", "ms_nextstepcode", "ms_id='" + oMsID + "'").ToString();
  111. if (nextstepcode != User.CurrentStepCode)
  112. {
  113. OperateResult.AppendText("<<序列号:" + sncode.Text + "下一工序是" + nextstepcode + ",不是当前岗位的工序\n", Color.Red, sncode);
  114. return;
  115. }
  116. //如果未打开串口设置为0
  117. //if (pr_colorboxunit.Text == "kg" && !WeightChange)
  118. //{
  119. // WeightChange = true;
  120. //}
  121. //重量信息若需要检测则必须符合检测要求才更新
  122. double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text.Replace("kg", "").Trim());
  123. if (PR_CHECKCOLORBOXW != "0")
  124. {
  125. if ((ActualWeight >= MinWeight) && (ActualWeight <= MaxWeight))
  126. {
  127. OperateResult.AppendText(">>彩盒" + sncode.Text + "重量检测检测合格\n", Color.Green);
  128. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ms_makecode.Text, User.UserLineCode, User.UserSourceCode, "彩盒称重:" + weight.Text, "称量合格", sncode.Text, "");
  129. LogicHandler.RecordProdWeight(sncode.Text, "SN", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName);
  130. dh.ExecuteSql("update makeserial set ms_grossw='" + ActualWeight + "' where ms_id='" + oMsID + "'", "update");
  131. }
  132. else
  133. {
  134. OperateResult.AppendText(">>彩盒" + sncode.Text + "重量检测未通过\n", Color.Red);
  135. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ms_makecode.Text, User.UserLineCode, User.UserSourceCode, "彩盒称重:" + weight.Text, "称量不合格", sncode.Text, "");
  136. LogicHandler.RecordProdWeight(sncode.Text, "SN", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName);
  137. return;
  138. }
  139. }
  140. else
  141. {
  142. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ms_makecode.Text, User.UserLineCode, User.UserSourceCode, "彩盒称重:" + weight.Text, "称量完成", sncode.Text, "");
  143. LogicHandler.RecordProdWeight(sncode.Text, "SN", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName);
  144. dh.ExecuteSql("update makeserial set ms_grossw='" + ActualWeight + "' where ms_id='" + oMsID + "'", "update");
  145. }
  146. ListViewItem lsi = new ListViewItem();
  147. lsi.SubItems.Add(sncode.Text);
  148. lsi.SubItems.Add(ActualWeight.ToString());
  149. lsi.SubItems.Add(System.DateTime.Now.ToString());
  150. WeighRecord.Items.Add(lsi);
  151. OperateResult.AppendText(">>彩盒" + sncode.Text + "称重完成\n", Color.Green);
  152. //显示最近的三个称量记录
  153. if (LogicHandler.SetStepResult(ms_makecode.Text, User.UserSourceCode, sncode.Text, "彩盒称量", "称量合格", User.UserCode, out ErrorMessage))
  154. {
  155. //提示正确返回时传递的信息
  156. if (ErrorMessage.Contains("AFTERSUCCESS"))
  157. OperateResult.AppendText(">>" + ErrorMessage + "\n");
  158. LoadCheckQTY();
  159. //记录送检前的最后一个序列号
  160. LastSncode = sncode.Text;
  161. if (AutoPrint.Checked)
  162. {
  163. OperateResult.AppendText(">>打印条码号" + sncode.Text + "\n", Color.Green);
  164. Confirm.PerformClick();
  165. }
  166. }
  167. else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red);
  168. LogicHandler.CollectRemarkInf(sncode.Text, "", "", oMakeCode, pr_code.Text, Remark.Text);
  169. LoadCollectedNum();
  170. sncode.Clear();
  171. }
  172. else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, sncode);
  173. }
  174. else OperateResult.AppendText(">>序列号不存在\n", Color.Red, sncode);
  175. }
  176. else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, sncode);
  177. }
  178. }
  179. private void getSerialData()
  180. {
  181. if (serialPort1.IsOpen)
  182. {
  183. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  184. {
  185. SystemInf.OpenPort.Add(serialPort1.PortName);
  186. try
  187. {
  188. while (GetData)
  189. {
  190. try
  191. {
  192. weight.Text = serialPort1.ReadLine();
  193. //if (pr_colorboxunit.Text == "kg")
  194. //{
  195. // weight.Text = (float.Parse(weight.Text) / 1000).ToString();
  196. // WeightChange = true;
  197. //}
  198. //else
  199. //{
  200. // WeightChange = false;
  201. //}
  202. }
  203. catch (Exception)
  204. {
  205. GetData = false;
  206. }
  207. }
  208. }
  209. catch (IOException ex) { MessageBox.Show(ex.Message); }
  210. }
  211. else OperateResult.AppendText(">>端口已被占用,请关闭其他窗口\n", Color.Red);
  212. }
  213. }
  214. private void 彩盒称重_SizeChanged(object sender, EventArgs e)
  215. {
  216. weight.Location = new Point(weight_label.Location.X + weight_label.Size.Width - 10, weight_label.Location.Y - 20);
  217. asc.controlAutoSize(this);
  218. }
  219. protected override void OnVisibleChanged(EventArgs e)
  220. {
  221. base.OnVisibleChanged(e);
  222. if (!IsHandleCreated)
  223. {
  224. this.Close();
  225. }
  226. }
  227. private void Clean_Click(object sender, EventArgs e)
  228. {
  229. OperateResult.Clear();
  230. }
  231. //关闭窗口的时候停止进程读取串口数据
  232. private void 彩盒称重_FormClosing(object sender, FormClosingEventArgs e)
  233. {
  234. BaseUtil.ClosePrint(lbl);
  235. StopWeight.PerformClick();
  236. InitPrint.Abort();
  237. if (serialPort1.IsOpen)
  238. {
  239. GetData = false;
  240. serialPort1.Close();
  241. SystemInf.OpenPort.Remove(serialPort1.PortName);
  242. thread.Interrupt();
  243. }
  244. //thread.Abort();
  245. }
  246. private void Confirm_Click(object sender, EventArgs e)
  247. {
  248. if (PrintLabel.Items.Count != 0)
  249. {
  250. doc = lbl.Documents.Open(PrintLabel.Text);
  251. string oErrorMessage;
  252. if (!Print.CodeSoft(Tag.ToString(), doc, PrintLabel.Text, PrintLabel.SelectedValue.ToString(), PrinterList.Text, sncode.Text, int.Parse(PrintNum.Text), ms_makecode.Text, pr_code.Text, "彩盒标", "0", out oErrorMessage))
  253. {
  254. OperateResult.AppendText(oErrorMessage + "\n", Color.Red);
  255. }
  256. }
  257. else
  258. {
  259. OperateResult.AppendText(">>产品" + pr_code.Text + "未维护打印标签\n", Color.Red);
  260. }
  261. }
  262. private void StartWeight_Click(object sender, EventArgs e)
  263. {
  264. thread = new Thread(getSerialData);
  265. try
  266. {
  267. serialPort1.PortName = ComList.Text;
  268. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  269. serialPort1.Open();
  270. GetData = true;
  271. thread.Start();
  272. }
  273. catch (Exception mes)
  274. {
  275. if (BaudRate.Text == "" || ComList.Text == "")
  276. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  277. else
  278. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  279. }
  280. }
  281. private void StopWeight_Click(object sender, EventArgs e)
  282. {
  283. if (serialPort1.IsOpen)
  284. {
  285. GetData = false;
  286. serialPort1.Close();
  287. SystemInf.OpenPort.Remove(serialPort1.PortName);
  288. thread.Abort();
  289. }
  290. }
  291. private void pr_code_TextChanged(object sender, EventArgs e)
  292. {
  293. LoadCheckQTY();
  294. DataTable _dt = (DataTable)dh.ExecuteSql("select la_id,la_url,la_isdefault from label where la_prodcode='" + pr_code.Text + "' and la_templatetype='彩盒标' and la_statuscode='AUDITED' order by la_isdefault", "select");
  295. PrintLabel.DataSource = _dt;
  296. PrintLabel.DisplayMember = "la_url";
  297. PrintLabel.ValueMember = "la_id";
  298. }
  299. private void SendCheck_Click(object sender, EventArgs e)
  300. {
  301. sql.Clear();
  302. sql.Append("update oqcbatch set ob_status='UNCHECK' where ob_checkno ='" + ob_checkno.Text + "'");
  303. dh.ExecuteSql(sql.GetString(), "select");
  304. ob_nowcheckqty.Text = "";
  305. ob_batchqty.Text = "";
  306. ob_nowcheckqty.ForeColor = Color.Black;
  307. SendCheck.Enabled = false;
  308. OperateResult.AppendText(">>批次" + ob_checkno.Text + "送检成功\n", Color.Green);
  309. LogicHandler.InsertMakeProcess(LastSncode, ms_makecode.Text, User.UserSourceCode, "手动送检", "手动送检成功", User.UserCode);
  310. //记录操作日志
  311. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ms_makecode.Text, User.UserLineCode, User.UserSourceCode, "手动送检", "手动送检成功", "", ob_checkno.Text);
  312. ob_checkno.Text = "";
  313. }
  314. private void LoadCheckQTY()
  315. {
  316. sql.Clear();
  317. string condition = "";
  318. int nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text);
  319. int batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text);
  320. if (nowcheckqty + 1 == batchqty && AutoCut)
  321. {
  322. condition = "and ob_status='UNCHECK' and ob_checkno='" + ob_checkno.Text + "'";
  323. }
  324. else
  325. {
  326. condition = "and ob_status='ENTERING' ";
  327. }
  328. if (pr_sendchecktype.Text == "SaleCode")
  329. {
  330. condition += " and ob_salecode='" + ma_salecode.Text + "'";
  331. }
  332. sql.Append("select ob_batchqty,ob_nowcheckqty,ob_checkno from oqcbatch where ");
  333. sql.Append("ob_linecode='" + User.UserLineCode + "' and ob_prodcode='" + pr_code.Text + "' and ");
  334. sql.Append("ob_stepcode='" + User.CurrentStepCode + "' " + condition);
  335. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  336. if (dt.Rows.Count > 0)
  337. {
  338. ob_batchqty.Text = dt.Rows[0]["ob_batchqty"].ToString();
  339. ob_nowcheckqty.Text = dt.Rows[0]["ob_nowcheckqty"].ToString();
  340. ob_checkno.Text = dt.Rows[0]["ob_checkno"].ToString();
  341. nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text);
  342. batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text);
  343. if (nowcheckqty == batchqty)
  344. {
  345. ob_nowcheckqty.ForeColor = Color.Red;
  346. OperateResult.AppendText(">>当前采集数量已达到送检数量\n", Color.Red);
  347. if (AutoCut)
  348. {
  349. OperateResult.AppendText(">>批次" + ob_checkno.Text + "自动断批\n", Color.Green);
  350. }
  351. }
  352. }
  353. else
  354. {
  355. ob_batchqty.Text = "";
  356. ob_nowcheckqty.Text = "";
  357. ob_checkno.Text = "";
  358. SendCheck.Enabled = false;
  359. }
  360. if (ob_batchqty.Text != "")
  361. {
  362. SendCheck.Enabled = true;
  363. }
  364. else
  365. {
  366. SendCheck.Enabled = false;
  367. }
  368. }
  369. private void ob_checkno_TextChanged(object sender, EventArgs e)
  370. {
  371. if (ob_checkno.Text != "")
  372. {
  373. string Cut = dh.getFieldDataByCondition("product left join oqcbatch on ob_prodcode=pr_code", "pr_ifautocutcheckno", "ob_checkno='" + ob_checkno.Text + "'").ToString();
  374. if (Cut == "" || Cut == "0")
  375. AutoCut = false;
  376. else
  377. AutoCut = true;
  378. SendCheck.Enabled = true;
  379. }
  380. }
  381. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  382. {
  383. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  384. {
  385. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  386. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  387. }
  388. }
  389. private void RefreshWeigh_Click(object sender, EventArgs e)
  390. {
  391. sql.Clear();
  392. sql.Append("select pr_colorboxgw,pr_colorboxmaxw,pr_colorboxminw,pr_colorboxunit from product where pr_code='" + pr_code.Text + "'");
  393. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  394. if (dt.Rows.Count > 0)
  395. {
  396. //重量的临时变量
  397. string _weight = dt.Rows[0]["pr_colorboxgw"].ToString();
  398. string _maxweight = dt.Rows[0]["pr_colorboxmaxw"].ToString();
  399. string _minweight = dt.Rows[0]["pr_colorboxminw"].ToString();
  400. //赋值重量单位
  401. Weight = double.Parse(_weight == "" ? "0" : _weight);
  402. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  403. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  404. if (Weight - MinWeight == MaxWeight - Weight)
  405. pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_colorboxunit"].ToString();
  406. else
  407. pr_colorboxgw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_colorboxunit"].ToString();
  408. }
  409. }
  410. private void LoadCollectedNum()
  411. {
  412. dt = (DataTable)dh.ExecuteSql("select mcd_inqty,ma_qty-mcd_inqty mcd_remainqty from make left join makecraftdetail on mcd_macode=ma_code where ma_code='" + ms_makecode.Text + "' and mcd_stepcode='" + User.CurrentStepCode + "'", "select");
  413. BaseUtil.SetFormValue(Controls, dt);
  414. }
  415. }
  416. }