Make_ColorBoxWeigh.cs 17 KB

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