Make_ColorBoxWeigh.cs 18 KB

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