Make_ColorBoxWeigh.cs 23 KB

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