Make_ColorBoxWeigh.cs 17 KB

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