Make_ColorBoxWeigh.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 = new DataHelper();
  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,mcd_inqty,ma_qty,ma_qty-mcd_inqty as mcd_waitqty,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')pr_sendchecktype from makeserial left join make on ms_makecode=ma_code left join product on ");
  86. sql.Append("ms_prodcode=pr_code left join makecraftdetail on ms_makecode=mcd_macode where ms_sncode='" + sncode.Text + "' order by ms_id desc");
  87. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  88. if (dt.Rows.Count > 0)
  89. {
  90. //重量的临时变量
  91. string _weight = dt.Rows[0]["pr_colorboxgw"].ToString();
  92. string _maxweight = dt.Rows[0]["pr_colorboxmaxw"].ToString();
  93. string _minweight = dt.Rows[0]["pr_colorboxminw"].ToString();
  94. string pr_colorunit = dt.Rows[0]["pr_colorboxunit"].ToString();
  95. string PR_CHECKCOLORBOXW = dt.Rows[0]["PR_CHECKCOLORBOXW"].ToString();
  96. //赋值重量单位
  97. Weight = double.Parse(_weight == "" ? "0" : _weight);
  98. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  99. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  100. BaseUtil.SetFormValue(this.Controls, dt);
  101. if (Weight - MinWeight == MaxWeight - Weight)
  102. pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_colorboxunit"].ToString();
  103. else
  104. pr_colorboxgw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_colorboxunit"].ToString();
  105. string oMakeCode = "";
  106. string oMsID = "";
  107. if (LogicHandler.CheckStepSNAndMacode(ms_makecode.Text, User.UserSourceCode, sncode.Text, User.UserCode, out oMakeCode, out oMsID, out ErrorMessage))
  108. {
  109. //如果未打开串口设置为0
  110. //if (pr_colorboxunit.Text == "kg" && !WeightChange)
  111. //{
  112. // WeightChange = true;
  113. //}
  114. double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text.Replace("kg", "").Trim());
  115. if (PR_CHECKCOLORBOXW != "0")
  116. {
  117. if ((ActualWeight >= MinWeight) && (ActualWeight <= MaxWeight))
  118. {
  119. OperateResult.AppendText(">>彩盒" + sncode.Text + "重量检测检测合格\n", Color.Green);
  120. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ms_makecode.Text, User.UserLineCode, User.UserSourceCode, "彩盒称重:" + weight.Text, "称量合格", sncode.Text, "");
  121. LogicHandler.RecordProdWeight(sncode.Text, "SN", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName);
  122. }
  123. else
  124. {
  125. OperateResult.AppendText(">>彩盒" + sncode.Text + "重量检测未通过\n", Color.Red);
  126. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ms_makecode.Text, User.UserLineCode, User.UserSourceCode, "彩盒称重:" + weight.Text, "称量不合格", sncode.Text, "");
  127. LogicHandler.RecordProdWeight(sncode.Text, "SN", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName);
  128. return;
  129. }
  130. }
  131. else
  132. {
  133. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ms_makecode.Text, User.UserLineCode, User.UserSourceCode, "彩盒称重:" + weight.Text, "称量完成", sncode.Text, "");
  134. LogicHandler.RecordProdWeight(sncode.Text, "SN", float.Parse(ActualWeight.ToString()), "kg", User.UserLineCode, pr_code.Text, User.UserSourceCode, User.UserName);
  135. }
  136. ListViewItem lsi = new ListViewItem();
  137. lsi.SubItems.Add(sncode.Text);
  138. lsi.SubItems.Add(ActualWeight.ToString());
  139. lsi.SubItems.Add(System.DateTime.Now.ToString());
  140. WeighRecord.Items.Add(lsi);
  141. OperateResult.AppendText(">>彩盒" + sncode.Text + "称重完成\n", Color.Green);
  142. //显示最近的三个称量记录
  143. if (LogicHandler.SetStepResult(ms_makecode.Text, User.UserSourceCode, sncode.Text, "彩盒称量", "称量合格", User.UserCode, out ErrorMessage))
  144. {
  145. //提示正确返回时传递的信息
  146. if (ErrorMessage.Contains("AFTERSUCCESS"))
  147. OperateResult.AppendText(">>" + ErrorMessage + "\n");
  148. LoadCheckQTY();
  149. //记录送检前的最后一个序列号
  150. LastSncode = sncode.Text;
  151. if (AutoPrint.Checked)
  152. {
  153. OperateResult.AppendText(">>打印条码号" + sncode.Text + "\n", Color.Green);
  154. Confirm.PerformClick();
  155. }
  156. }
  157. else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red);
  158. //称量是否通过都记录重量
  159. dh.ExecuteSql("update makeserial set ms_grossw='" + ActualWeight + "' where ms_id='" + oMsID + "'", "update");
  160. sncode.Clear();
  161. }
  162. else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, sncode);
  163. }
  164. else OperateResult.AppendText(">>序列号不存在\n", Color.Red, sncode);
  165. }
  166. else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, sncode);
  167. }
  168. }
  169. private void getSerialData()
  170. {
  171. if (serialPort1.IsOpen)
  172. {
  173. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  174. {
  175. SystemInf.OpenPort.Add(serialPort1.PortName);
  176. try
  177. {
  178. while (GetData)
  179. {
  180. try
  181. {
  182. weight.Text = serialPort1.ReadLine();
  183. //if (pr_colorboxunit.Text == "kg")
  184. //{
  185. // weight.Text = (float.Parse(weight.Text) / 1000).ToString();
  186. // WeightChange = true;
  187. //}
  188. //else
  189. //{
  190. // WeightChange = false;
  191. //}
  192. }
  193. catch (Exception)
  194. {
  195. GetData = false;
  196. }
  197. }
  198. }
  199. catch (IOException ex) { MessageBox.Show(ex.Message); }
  200. }
  201. else OperateResult.AppendText(">>端口已被占用,请关闭其他窗口\n", Color.Red);
  202. }
  203. }
  204. private void 彩盒称重_SizeChanged(object sender, EventArgs e)
  205. {
  206. weight.Location = new Point(weight_label.Location.X + weight_label.Size.Width - 10, weight_label.Location.Y - 20);
  207. asc.controlAutoSize(this);
  208. }
  209. protected override void OnVisibleChanged(EventArgs e)
  210. {
  211. base.OnVisibleChanged(e);
  212. if (!IsHandleCreated)
  213. {
  214. this.Close();
  215. }
  216. }
  217. private void Clean_Click(object sender, EventArgs e)
  218. {
  219. OperateResult.Clear();
  220. }
  221. //关闭窗口的时候停止进程读取串口数据
  222. private void 彩盒称重_FormClosing(object sender, FormClosingEventArgs e)
  223. {
  224. BaseUtil.ClosePrint(lbl);
  225. StopWeight.PerformClick();
  226. InitPrint.Abort();
  227. if (serialPort1.IsOpen)
  228. {
  229. GetData = false;
  230. serialPort1.Close();
  231. SystemInf.OpenPort.Remove(serialPort1.PortName);
  232. thread.Interrupt();
  233. }
  234. //thread.Abort();
  235. }
  236. private void Confirm_Click(object sender, EventArgs e)
  237. {
  238. if (PrintLabel.Items.Count != 0)
  239. {
  240. doc = lbl.Documents.Open(ftpOperater.DownLoadTo + PrintLabel.Text);
  241. string oErrorMessage;
  242. 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))
  243. {
  244. OperateResult.AppendText(oErrorMessage + "\n", Color.Red);
  245. }
  246. }
  247. else
  248. {
  249. OperateResult.AppendText(">>产品" + pr_code.Text + "未维护打印标签\n", Color.Red);
  250. }
  251. }
  252. private void StartWeight_Click(object sender, EventArgs e)
  253. {
  254. thread = new Thread(getSerialData);
  255. try
  256. {
  257. serialPort1.PortName = ComList.Text;
  258. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  259. serialPort1.Open();
  260. GetData = true;
  261. thread.Start();
  262. }
  263. catch (Exception mes)
  264. {
  265. if (BaudRate.Text == "" || ComList.Text == "")
  266. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  267. else
  268. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  269. }
  270. }
  271. private void StopWeight_Click(object sender, EventArgs e)
  272. {
  273. if (serialPort1.IsOpen)
  274. {
  275. GetData = false;
  276. serialPort1.Close();
  277. SystemInf.OpenPort.Remove(serialPort1.PortName);
  278. thread.Abort();
  279. }
  280. }
  281. private void pr_code_TextChanged(object sender, EventArgs e)
  282. {
  283. LoadCheckQTY();
  284. 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='" + pr_code.Text + "' and la_templatetype='彩盒标' and la_statuscode='AUDITED' order by la_isdefault", "select");
  285. PrintLabel.DataSource = _dt;
  286. PrintLabel.DisplayMember = "la_name";
  287. PrintLabel.ValueMember = "la_id";
  288. ftpOperater ftp = new ftpOperater();
  289. indate = new System.DateTime[_dt.Rows.Count];
  290. for (int i = 0; i < _dt.Rows.Count; i++)
  291. {
  292. BaseUtil.GetPrintLabel(_dt.Rows[i]["la_name"].ToString(), _dt.Rows[i]["la_url"].ToString());
  293. }
  294. }
  295. private void LoadCollectNum()
  296. {
  297. DataTable dt = (DataTable)dh.ExecuteSql("select mcd_inqty,ma_qty-mcd_inqty mcd_waitqty from make left join makecraftdetail on mcd_macode=ma_code where ma_code='" + ms_makecode.Text + "' and mcd_stepcode='" + User.CurrentStepCode + "'", "select");
  298. BaseUtil.SetFormValue(Controls, dt);
  299. }
  300. private void SendCheck_Click(object sender, EventArgs e)
  301. {
  302. sql.Clear();
  303. sql.Append("update oqcbatch set ob_status='UNCHECK' where ob_checkno ='" + ob_checkno.Text + "'");
  304. dh.ExecuteSql(sql.GetString(), "select");
  305. ob_nowcheckqty.Text = "";
  306. ob_batchqty.Text = "";
  307. ob_nowcheckqty.ForeColor = Color.Black;
  308. SendCheck.Enabled = false;
  309. OperateResult.AppendText(">>批次" + ob_checkno.Text + "送检成功\n", Color.Green);
  310. LogicHandler.InsertMakeProcess(LastSncode, ms_makecode.Text, User.UserSourceCode, "手动送检", "手动送检成功", User.UserCode);
  311. //记录操作日志
  312. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ms_makecode.Text, User.UserLineCode, User.UserSourceCode, "手动送检", "手动送检成功", "", ob_checkno.Text);
  313. ob_checkno.Text = "";
  314. }
  315. private void LoadCheckQTY()
  316. {
  317. sql.Clear();
  318. string condition = "";
  319. int nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text);
  320. int batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text);
  321. if (nowcheckqty + 1 == batchqty && AutoCut)
  322. {
  323. condition = "and ob_status='UNCHECK' and ob_checkno='" + ob_checkno.Text + "'";
  324. }
  325. else
  326. {
  327. condition = "and ob_status='ENTERING' ";
  328. }
  329. if (pr_sendchecktype.Text == "SaleCode")
  330. {
  331. condition += " and ob_salecode='" + ma_salecode.Text + "'";
  332. }
  333. sql.Append("select ob_batchqty,ob_nowcheckqty,ob_checkno from oqcbatch where ");
  334. sql.Append("ob_linecode='" + User.UserLineCode + "' and ob_prodcode='" + pr_code.Text + "' and ");
  335. sql.Append("ob_stepcode='" + User.CurrentStepCode + "' " + condition);
  336. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  337. if (dt.Rows.Count > 0)
  338. {
  339. ob_batchqty.Text = dt.Rows[0]["ob_batchqty"].ToString();
  340. ob_nowcheckqty.Text = dt.Rows[0]["ob_nowcheckqty"].ToString();
  341. ob_checkno.Text = dt.Rows[0]["ob_checkno"].ToString();
  342. nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text);
  343. batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text);
  344. if (nowcheckqty == batchqty)
  345. {
  346. ob_nowcheckqty.ForeColor = Color.Red;
  347. OperateResult.AppendText(">>当前采集数量已达到送检数量\n", Color.Red);
  348. if (AutoCut)
  349. {
  350. OperateResult.AppendText(">>批次" + ob_checkno.Text + "自动断批\n", Color.Green);
  351. }
  352. }
  353. }
  354. else
  355. {
  356. ob_batchqty.Text = "";
  357. ob_nowcheckqty.Text = "";
  358. ob_checkno.Text = "";
  359. SendCheck.Enabled = false;
  360. }
  361. if (ob_batchqty.Text != "")
  362. {
  363. SendCheck.Enabled = true;
  364. }
  365. else
  366. {
  367. SendCheck.Enabled = false;
  368. }
  369. }
  370. private void ob_checkno_TextChanged(object sender, EventArgs e)
  371. {
  372. if (ob_checkno.Text != "")
  373. {
  374. string Cut = dh.getFieldDataByCondition("product left join oqcbatch on ob_prodcode=pr_code", "pr_ifautocutcheckno", "ob_checkno='" + ob_checkno.Text + "'").ToString();
  375. if (Cut == "" || Cut == "0")
  376. AutoCut = false;
  377. else
  378. AutoCut = true;
  379. SendCheck.Enabled = true;
  380. }
  381. }
  382. private void PrintLabel_SelectedValueChanged(object sender, EventArgs e)
  383. {
  384. if (PrintLabel.SelectedValue != null && PrintLabel.SelectedValue.ToString() != "System.Data.DataRowView")
  385. {
  386. string PrintNums = dh.getFieldDataByCondition("label", "la_printnos", "la_id='" + PrintLabel.SelectedValue.ToString() + "'").ToString();
  387. PrintNum.Text = (PrintNums == "" ? "1" : PrintNums);
  388. }
  389. }
  390. private void RefreshWeigh_Click(object sender, EventArgs e)
  391. {
  392. sql.Clear();
  393. sql.Append("select pr_colorboxgw,pr_colorboxmaxw,pr_colorboxminw,pr_colorboxunit from product where pr_code='" + pr_code.Text + "'");
  394. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  395. if (dt.Rows.Count > 0)
  396. {
  397. //重量的临时变量
  398. string _weight = dt.Rows[0]["pr_colorboxgw"].ToString();
  399. string _maxweight = dt.Rows[0]["pr_colorboxmaxw"].ToString();
  400. string _minweight = dt.Rows[0]["pr_colorboxminw"].ToString();
  401. //赋值重量单位
  402. Weight = double.Parse(_weight == "" ? "0" : _weight);
  403. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  404. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  405. if (Weight - MinWeight == MaxWeight - Weight)
  406. pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight) + dt.Rows[0]["pr_colorboxunit"].ToString();
  407. else
  408. pr_colorboxgw.Text = MinWeight + "-" + MaxWeight + dt.Rows[0]["pr_colorboxunit"].ToString();
  409. }
  410. }
  411. }
  412. }