Make_ColorBoxWeigh.cs 16 KB

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