Make_ColorBoxWeigh.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. DataHelper dh;
  23. DataTable dt;
  24. LogStringBuilder sql = new LogStringBuilder();
  25. //启用线程进行称重数据读取
  26. Thread thread;
  27. SerialPort serialPort1 = new SerialPort();
  28. //称量的标准重量
  29. Double Weight;
  30. //最大重量
  31. Double MaxWeight;
  32. //最小重量
  33. Double MinWeight;
  34. //是否通过串口获取数据
  35. bool GetData = true;
  36. string LastSncode;
  37. string ErrorMessage;
  38. Thread InitPrint;
  39. System.DateTime[] indate;
  40. private bool AutoCut;
  41. public Make_ColorBoxWeigh()
  42. {
  43. InitializeComponent();
  44. CheckForIllegalCrossThreadCalls = false;
  45. }
  46. private void Make_ColorBoxWeigh_Load(object sender, EventArgs e)
  47. {
  48. asc.controllInitializeSize(this);
  49. ComList.Text = BaseUtil.GetCacheData("PortName").ToString();
  50. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  51. InitPrint = new Thread(InPrint);
  52. SetLoadingWindow stw = new SetLoadingWindow(InitPrint, "初始化打印程序");
  53. BaseUtil.SetFormCenter(stw);
  54. stw.ShowDialog();
  55. StartWeight.PerformClick();
  56. dh = new DataHelper();
  57. }
  58. private void InPrint()
  59. {
  60. try
  61. {
  62. lbl = new ApplicationClass();
  63. BaseUtil.WriteLbl(lbl);
  64. }
  65. catch (Exception ex)
  66. {
  67. OperateResult.AppendText("未正确安装CodeSoft软件\n", Color.Red);
  68. }
  69. }
  70. private void sncode_KeyDown(object sender, KeyEventArgs e)
  71. {
  72. if (e.KeyCode == Keys.Enter)
  73. {
  74. if (LogicHandler.CheckStepAttribute(Tag.ToString(), User.UserSourceCode, out ErrorMessage))
  75. {
  76. sql.Clear();
  77. 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,");
  78. sql.Append("pr_colorboxunit,pr_colorboxmaxw,pr_colorboxminw from makeserial left join make on ms_makecode=ma_code left join product on ");
  79. sql.Append("ms_prodcode=pr_code left join makecraftdetail on ms_makecode=mcd_macode where ms_sncode='" + sncode.Text + "' order by ms_id desc");
  80. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  81. if (dt.Rows.Count > 0)
  82. {
  83. //重量的临时变量
  84. string _weight = dt.Rows[0]["pr_colorboxgw"].ToString();
  85. string _maxweight = dt.Rows[0]["pr_colorboxmaxw"].ToString();
  86. string _minweight = dt.Rows[0]["pr_colorboxminw"].ToString();
  87. string pr_colorunit = dt.Rows[0]["pr_colorboxunit"].ToString();
  88. //赋值重量单位
  89. Weight = double.Parse(_weight == "" ? "0" : _weight);
  90. MaxWeight = double.Parse(_maxweight == "" ? "0" : _maxweight);
  91. MinWeight = double.Parse(_minweight == "" ? "0" : _minweight);
  92. BaseUtil.SetFormValue(this.Controls, dt);
  93. if (Weight - MinWeight == MaxWeight - Weight)
  94. pr_colorboxgw.Text = Weight + "±" + (MaxWeight - Weight);
  95. else
  96. pr_colorboxgw.Text = MinWeight + "-" + MaxWeight;
  97. string oMakeCode = "";
  98. string oMsID = "";
  99. if (LogicHandler.CheckStepSNAndMacode(ms_makecode.Text, User.UserSourceCode, sncode.Text, User.UserCode, out oMakeCode, out oMsID, out ErrorMessage))
  100. {
  101. string YN = dh.GetConfig("BatchNumber", "MESSetting").ToString();
  102. //表示需要验证判断md_qty 是否大于等于md_baseqty
  103. if (YN != "0")
  104. {
  105. sql.Clear();
  106. sql.Append("select wm_concat(md_prodcode) ,count(1) cn from makeprepare left join makepreparedetail ");
  107. sql.Append("on mp_id=md_mpid where mp_makecode='" + ms_makecode.Text + "' and md_qty<md_baseqty");
  108. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  109. if (dt.Rows[0]["CN"].ToString() != "0")
  110. {
  111. OperateResult.AppendText(">>物料:" + pr_code.Text + " ,岗位用料不足\n", Color.Red, sncode);
  112. return;
  113. }
  114. }
  115. //如果未打开串口设置为0
  116. double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text);
  117. if ((ActualWeight >= MinWeight) && (ActualWeight <= MaxWeight))
  118. {
  119. ListViewItem lsi = new ListViewItem();
  120. lsi.SubItems.Add(sncode.Text);
  121. lsi.SubItems.Add(ActualWeight.ToString());
  122. lsi.SubItems.Add(System.DateTime.Now.ToString());
  123. WeighRecord.Items.Add(lsi);
  124. //显示最近的三个称量记录
  125. if (LogicHandler.SetStepResult(ms_makecode.Text, User.UserSourceCode, sncode.Text, "彩盒称量", "称量合格", User.UserCode, out ErrorMessage))
  126. {
  127. LoadCheckQTY();
  128. OperateResult.AppendText(">>" + sncode.Text + "重量检测检测合格\n", Color.Green);
  129. //记录送检前的最后一个序列号
  130. LastSncode = sncode.Text;
  131. if (AutoPrint.Checked)
  132. {
  133. OperateResult.AppendText(">>打印条码号" + sncode.Text + "\n", Color.Green);
  134. Confirm.PerformClick();
  135. }
  136. sncode.Text = "";
  137. }
  138. else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, sncode);
  139. }
  140. else OperateResult.AppendText(">>" + sncode.Text + "重量检测未通过\n", Color.Red);
  141. //称量是否通过都记录重量
  142. dh.ExecuteSql("update makeserial set ms_grossw='" + ActualWeight + "' where ms_sncode='" + sncode.Text + "' and ms_makecode='" + ms_makecode.Text + "'", "update");
  143. sncode.Clear();
  144. }
  145. else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, sncode);
  146. }
  147. else OperateResult.AppendText(">>序列号不存在\n", Color.Red, sncode);
  148. }
  149. else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red, sncode);
  150. }
  151. }
  152. private void getSerialData()
  153. {
  154. if (serialPort1.IsOpen)
  155. {
  156. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  157. {
  158. SystemInf.OpenPort.Add(serialPort1.PortName);
  159. try
  160. {
  161. while (GetData)
  162. {
  163. try
  164. {
  165. weight.Text = Regex.Replace(serialPort1.ReadLine(), "\\D+", "");
  166. if (pr_colorboxunit.Text == "kg")
  167. weight.Text = (float.Parse(weight.Text) / 1000).ToString();
  168. }
  169. catch (Exception)
  170. {
  171. GetData = false;
  172. }
  173. }
  174. }
  175. catch (IOException ex) { MessageBox.Show(ex.Message); }
  176. }
  177. else OperateResult.AppendText(">>端口已被占用,请关闭其他窗口\n", Color.Red);
  178. }
  179. }
  180. private void 彩盒称重_SizeChanged(object sender, EventArgs e)
  181. {
  182. asc.controlAutoSize(this);
  183. }
  184. protected override void OnVisibleChanged(EventArgs e)
  185. {
  186. base.OnVisibleChanged(e);
  187. if (!IsHandleCreated)
  188. {
  189. this.Close();
  190. }
  191. }
  192. private void Clean_Click(object sender, EventArgs e)
  193. {
  194. OperateResult.Clear();
  195. }
  196. //关闭窗口的时候停止进程读取串口数据
  197. private void 彩盒称重_FormClosing(object sender, FormClosingEventArgs e)
  198. {
  199. BaseUtil.ClosePrint(lbl);
  200. StopWeight.PerformClick();
  201. if (serialPort1.IsOpen)
  202. {
  203. GetData = false;
  204. serialPort1.Close();
  205. SystemInf.OpenPort.Remove(serialPort1.PortName);
  206. thread.Interrupt();
  207. thread.Join();
  208. }
  209. }
  210. private void Confirm_Click(object sender, EventArgs e)
  211. {
  212. if (PrintLabel.Items.Count != 0)
  213. {
  214. Print.CodeSoft(Tag.ToString(), lbl, PrintLabel.Text.Split(':')[1], PrintLabel.SelectedValue.ToString(), PrinterList.Text, sncode.Text, int.Parse(PrintNum.Text), indate[PrintLabel.SelectedIndex]);
  215. }
  216. else
  217. {
  218. OperateResult.AppendText(">>产品" + pr_code.Text + "未维护打印标签\n", Color.Red);
  219. }
  220. }
  221. private void StartWeight_Click(object sender, EventArgs e)
  222. {
  223. thread = new Thread(getSerialData);
  224. try
  225. {
  226. serialPort1.PortName = ComList.Text;
  227. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  228. serialPort1.Open();
  229. GetData = true;
  230. thread.Start();
  231. }
  232. catch (Exception mes)
  233. {
  234. if (BaudRate.Text == "" || ComList.Text == "")
  235. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  236. else
  237. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  238. }
  239. }
  240. private void StopWeight_Click(object sender, EventArgs e)
  241. {
  242. GetData = false;
  243. SystemInf.OpenPort.Remove(serialPort1.PortName);
  244. serialPort1.Close();
  245. }
  246. private void pr_code_TextChanged(object sender, EventArgs e)
  247. {
  248. LoadCheckQTY();
  249. dt = (DataTable)dh.ExecuteSql("select pl_labelcode||':'||pl_labelname pl_laname,pl_labelname,pl_labelcode,pl_labelurl,pl_indate from productlabel where pl_prodcode='" + pr_code.Text + "' order by pl_isdefault desc ", "select");
  250. PrintLabel.DataSource = dt;
  251. PrintLabel.DisplayMember = "pl_laname";
  252. PrintLabel.ValueMember = "pl_labelcode";
  253. ftpOperater ftp = new ftpOperater();
  254. indate = new System.DateTime[dt.Rows.Count];
  255. for (int i = 0; i < dt.Rows.Count; i++)
  256. {
  257. BaseUtil.GetPrintLabel(dt.Rows[i]["pl_labelname"].ToString(), dt.Rows[i]["pl_labelurl"].ToString(), dt.Rows[i]["pl_indate"].ToString());
  258. indate[i] = Convert.ToDateTime(dt.Rows[i]["pl_indate"].ToString());
  259. }
  260. }
  261. private void LoadCollectNum()
  262. {
  263. 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");
  264. BaseUtil.SetFormValue(Controls, dt);
  265. }
  266. private void SendCheck_Click(object sender, EventArgs e)
  267. {
  268. sql.Clear();
  269. sql.Append("update oqcbatch set ob_status='UNCHECK' where ob_checkno ='" + ob_checkno.Text + "'");
  270. dh.ExecuteSql(sql.GetString(), "select");
  271. ob_nowcheckqty.Text = "";
  272. ob_batchqty.Text = "";
  273. ob_nowcheckqty.ForeColor = Color.Black;
  274. SendCheck.Enabled = false;
  275. OperateResult.AppendText(">>批次" + ob_checkno.Text + "送检成功\n", Color.Green);
  276. LogicHandler.InsertMakeProcess(LastSncode, ms_makecode.Text, User.UserSourceCode, "手动送检", "手动送检成功", User.UserCode);
  277. //记录操作日志
  278. LogicHandler.DoCommandLog(Tag.ToString(),User.UserCode, ms_makecode.Text, User.UserLineCode, User.UserSourceCode, "手动送检", "手动送检成功", "", ob_checkno.Text);
  279. ob_checkno.Text = "";
  280. }
  281. private void LoadCheckQTY()
  282. {
  283. sql.Clear();
  284. string condition = "";
  285. int nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text);
  286. int batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text);
  287. if (nowcheckqty + 1 == batchqty && AutoCut)
  288. {
  289. condition = "and ob_status='UNCHECK' and ob_checkno='" + ob_checkno.Text + "'";
  290. }
  291. else
  292. {
  293. condition = "and ob_status='ENTERING' ";
  294. }
  295. sql.Append("select ob_batchqty,ob_nowcheckqty,ob_checkno from oqcbatch where ");
  296. sql.Append("ob_linecode='" + User.UserLineCode + "' and ob_prodcode='" + pr_code.Text + "' and ");
  297. sql.Append("ob_stepcode='" + User.CurrentStepCode + "' " + condition);
  298. DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  299. if (dt.Rows.Count > 0)
  300. {
  301. ob_batchqty.Text = dt.Rows[0]["ob_batchqty"].ToString();
  302. ob_nowcheckqty.Text = dt.Rows[0]["ob_nowcheckqty"].ToString();
  303. ob_checkno.Text = dt.Rows[0]["ob_checkno"].ToString();
  304. nowcheckqty = int.Parse(ob_nowcheckqty.Text == "" ? "0" : ob_nowcheckqty.Text);
  305. batchqty = int.Parse(ob_batchqty.Text == "" ? "0" : ob_batchqty.Text);
  306. if (nowcheckqty == batchqty)
  307. {
  308. ob_nowcheckqty.ForeColor = Color.Red;
  309. OperateResult.AppendText(">>当前采集数量已达到送检数量\n", Color.Red);
  310. }
  311. }
  312. else
  313. {
  314. ob_batchqty.Text = "";
  315. ob_nowcheckqty.Text = "";
  316. ob_checkno.Text = "";
  317. SendCheck.Enabled = false;
  318. }
  319. if (ob_batchqty.Text != "")
  320. {
  321. SendCheck.Enabled = true;
  322. }
  323. else
  324. {
  325. SendCheck.Enabled = false;
  326. }
  327. }
  328. private void ob_checkno_TextChanged(object sender, EventArgs e)
  329. {
  330. if (ob_checkno.Text != "")
  331. {
  332. string Cut = dh.getFieldDataByCondition("product left join oqcbatch on ob_prodcode=pr_code", "pr_ifautocutcheckno", "ob_checkno='" + ob_checkno.Text + "'").ToString();
  333. if (Cut == "" || Cut == "0")
  334. AutoCut = false;
  335. else
  336. AutoCut = true;
  337. SendCheck.Enabled = true;
  338. }
  339. }
  340. }
  341. }