Packing_ProdWeightSetMidBox.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.IO.Ports;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading;
  12. using System.Windows.Forms;
  13. using UAS_MES_NEW.DataOperate;
  14. using UAS_MES_NEW.Entity;
  15. using UAS_MES_NEW.PublicMethod;
  16. namespace UAS_MES_NEW.Packing
  17. {
  18. public partial class Packing_ProdWeightSetMidBox : Form
  19. {
  20. AutoSizeFormClass asc = new AutoSizeFormClass();
  21. LogStringBuilder sql = new LogStringBuilder();
  22. DataTable Dbfind;//存储工单号查询出来的信息
  23. //创建串口实例
  24. SerialPort serialPort1 = new SerialPort();
  25. Regex re = new Regex("\\d+.\\d+\\w+");
  26. DataHelper dh;
  27. Thread thread;
  28. //true的时候表示从串口读取数据
  29. bool GetData = true;
  30. int samplesCount = 0;
  31. //string maxValue = "";//存储最大值
  32. //string minValue = "";//存储最小值
  33. List<string> sncodes = new List<string>();//存序列号
  34. List<string> indates = new List<string>();//存时间
  35. List<string> weights = new List<string>();//存重量
  36. string recordSW = ""; //存标准重量
  37. string recordEV = "";//存误差值
  38. public Packing_ProdWeightSetMidBox()
  39. {
  40. InitializeComponent();
  41. }
  42. private void Packing_ProdWeightSet_Load(object sender, EventArgs e)
  43. {
  44. asc.controllInitializeSize(this);
  45. ComList.Text = BaseUtil.GetCacheData("PortName").ToString();
  46. BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
  47. //工单号放大镜配置
  48. ma_code.TableName = "make left join product on ma_prodcode=pr_code";
  49. ma_code.SelectField = "ma_code # 工单号,pr_code # 产品编号,pr_detail # 产品名称,pr_spec # 规格,ma_qty # 工单数量";
  50. ma_code.FormName = Name;
  51. ma_code.SetValueField = new string[] { "ma_code", "pr_code", "pr_detail", "pr_spec" };
  52. ma_code.DbChange += Ma_code_DbChange;
  53. dh = SystemInf.dh;
  54. //开始称重
  55. startWeigh.PerformClick();
  56. }
  57. private void Ma_code_DbChange(object sender, EventArgs e)
  58. {
  59. Dbfind = ma_code.ReturnData;
  60. BaseUtil.SetFormValue(this.Controls, Dbfind);
  61. //取误差值
  62. errorValue.Text = dh.GetConfig("prodWeightErrorValue", "MESSetting").ToString();
  63. }
  64. private void recordResult(int index, string palletcode, string weigh, DateTime time)
  65. {
  66. //创建一个item
  67. ListViewItem lvi = new ListViewItem();
  68. //分条赋值
  69. lvi.SubItems.Add(index + "");
  70. lvi.SubItems.Add(palletcode);
  71. lvi.SubItems.Add(weigh);
  72. lvi.SubItems.Add(time.ToString());
  73. //添加结果的信息进去
  74. showResult.Items.Add(lvi);
  75. //更新已称重量最大值最小值
  76. if (index > 1)
  77. {
  78. //maxValue = double.Parse(weigh) > double.Parse(maxValue) ? weigh : maxValue;
  79. //minValue = double.Parse(weigh) < double.Parse(minValue) ? weigh : minValue;
  80. }
  81. else
  82. {
  83. //maxValue = weigh;
  84. //minValue = weigh;
  85. //每次添加第一条数据的时候清空list的值
  86. sncodes.Clear();
  87. indates.Clear();
  88. weights.Clear();
  89. }
  90. //将重量信息加到集合中
  91. sncodes.Add(palletcode);
  92. indates.Add(time.ToString("yyyy-MM-dd HH:mm:ss"));
  93. weights.Add(weigh.Replace("kg", "").Trim());
  94. }
  95. private void startWeigh_Click(object sender, EventArgs e)
  96. {
  97. thread = new Thread(getSerialData);
  98. try
  99. {
  100. GetData = true;
  101. serialPort1.PortName = this.ComList.Text;
  102. serialPort1.BaudRate = int.Parse(BaudRate.Text);
  103. serialPort1.Open();
  104. thread.Start();
  105. }
  106. catch (Exception mes)
  107. {
  108. if (BaudRate.Text == "" || ComList.Text == "")
  109. OperateResult.AppendText(">>请先在电子秤调试界面维护波特率和串口\n", Color.Red);
  110. else
  111. OperateResult.AppendText(">>" + mes.Message + "\n", Color.Red);
  112. }
  113. }
  114. private void getSerialData()
  115. {
  116. if (serialPort1.IsOpen)
  117. {
  118. if (!SystemInf.OpenPort.Contains(serialPort1.PortName))
  119. {
  120. SystemInf.OpenPort.Add(serialPort1.PortName);
  121. try
  122. {
  123. while (GetData)
  124. {
  125. try
  126. {
  127. weight.Text = re.Match(serialPort1.ReadLine().Trim()).Groups[0].Value;
  128. //weight.Text = Regex.Replace(serialPort1.ReadLine(), "\\D+", "");
  129. }
  130. catch (Exception)
  131. {
  132. GetData = false;
  133. }
  134. }
  135. }
  136. catch (IOException ex) { MessageBox.Show(ex.Message); }
  137. }
  138. else
  139. MessageBox.Show("端口已被占用,请关闭其他窗口");
  140. }
  141. }
  142. //停止称重
  143. private void stopWeigh_Click(object sender, EventArgs e)
  144. {
  145. if (serialPort1.IsOpen)
  146. {
  147. GetData = false;
  148. serialPort1.Close();
  149. SystemInf.OpenPort.Remove(serialPort1.PortName);
  150. thread.Abort();
  151. }
  152. }
  153. private void confirm_Click(object sender, EventArgs e)
  154. {
  155. //按确认更新产品重量
  156. if (ma_code.Text == "")
  157. {
  158. OperateResult.AppendText("<<请先选择工单\n", Color.Red);
  159. return;
  160. }
  161. //判断是否达到已称数量
  162. if (showResult.Items.Count < samplesCount)
  163. {
  164. OperateResult.AppendText("<<采样个数不足\n", Color.Red);
  165. return;
  166. }
  167. //double sum = 0;
  168. ////点击取平均值
  169. //foreach (string s in weights)
  170. //{
  171. // sum += double.Parse(s);
  172. //}
  173. //sum = sum / weights.Count;
  174. //更新彩盒重量最大值最小值
  175. double totalweight = 0;
  176. foreach (string w in weights)
  177. {
  178. totalweight += double.Parse(w);
  179. }
  180. standardWeight.Text = (totalweight / weights.Count).ToString();
  181. dh.ExecuteSql("update product set pr_midboxmaxweight ='" + (double.Parse(standardWeight.Text) + double.Parse(errorValue.Text)) + "', pr_midboxminweight = '" + (double.Parse(standardWeight.Text) - double.Parse(errorValue.Text)) + "' where pr_code='" + pr_code.Text + "'", "update");
  182. //更新此产品需要检测彩盒重量
  183. //dh.ExecuteSql("update product set pr_checkcolorboxw = 1 where pr_code='" + pr_code.Text + "'", "update");
  184. OperateResult.AppendText("<<重量设置成功,最大值" + (double.Parse(standardWeight.Text) + double.Parse(errorValue.Text)) + unit.Text + ",最小值" + (double.Parse(standardWeight.Text) - double.Parse(errorValue.Text)) + unit.Text + "\n", Color.Green);
  185. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, "", User.UserLineCode, User.UserSourceCode, "产品重量设置", "产品编号" + pr_code.Text + ",最大" + (double.Parse(standardWeight.Text) + double.Parse(errorValue.Text)) + unit.Text + "最小" + (double.Parse(standardWeight.Text) - double.Parse(errorValue.Text)) + unit.Text, "", "");
  186. //更新采样记录表
  187. string serialNum = dh.GetSerialNumberByCaller("ProdWeightSample");
  188. sql.Clear();
  189. sql.Append("insert into PRODWEIGHTSAMPLE(PWS_CODE,PWS_SNCODE,PWS_MAKECODE,PWS_PRODCODE,PWS_INDATE,PWS_WEIGHT,PWS_UNIT) ");
  190. sql.Append("values ('" + serialNum + "',:sncode,'" + ma_code.Text + "','" + pr_code.Text + "',TO_Date(:indate,'YYYY-MM-dd HH24:mi:ss'),:weight,'" + unit.Text + "')");
  191. dh.BatchInsert(sql.GetString(), new string[] { "sncode", "indate", "weight" }, sncodes.ToArray(), indates.ToArray(), weights.ToArray());
  192. }
  193. private void sncode_KeyDown(object sender, KeyEventArgs e)
  194. {
  195. //按下了enter键
  196. if (e.KeyCode == Keys.Enter)
  197. {
  198. if (sncode.Text == "")
  199. {
  200. OperateResult.AppendText("<<输入不能为空\n", Color.Red);
  201. return;
  202. }
  203. if (weight.Text == "")
  204. {
  205. OperateResult.AppendText("<<未读取到重量信息\n", Color.Red);
  206. return;
  207. }
  208. if (double.Parse(weight.Text.Replace("kg", "").Trim()) == 0)
  209. {
  210. OperateResult.AppendText("<<重量不能等于0\n", Color.Red, sncode);
  211. return;
  212. }
  213. //验证序列号是否存在
  214. if (!dh.CheckExist("makeserial", "ms_sncode = '" + sncode.Text + "'"))
  215. {
  216. OperateResult.AppendText("<<序列号" + sncode.Text + "不存在\n", Color.Red, sncode);
  217. return;
  218. }
  219. //验证彩盒是否与产品对应
  220. if (!dh.CheckExist("makeserial", "ms_prodcode='" + pr_code.Text + "' and ms_sncode = '" + sncode.Text + "'"))
  221. {
  222. OperateResult.AppendText("<<序列号" + sncode.Text + "对应产品编号不是" + pr_code.Text + "\n", Color.Red, sncode);
  223. return;
  224. }
  225. //验证所称彩盒是否重复
  226. if (sncodes.Contains(sncode.Text))
  227. {
  228. OperateResult.AppendText("<<序列号" + sncode.Text + "已经称过\n", Color.Red, sncode);
  229. return;
  230. }
  231. //判断称重记录是否满足标准重量+-误差值
  232. double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text.Replace("kg", "").Trim());
  233. if (ActualWeight < (double.Parse(standardWeight.Text) - double.Parse(errorValue.Text)) || ActualWeight > (double.Parse(standardWeight.Text) + double.Parse(errorValue.Text)))
  234. {
  235. OperateResult.AppendText("<<序列号" + sncode.Text + "重量不在预设范围内\n", Color.Red, sncode);
  236. return;
  237. }
  238. //记录重量
  239. recordResult(showResult.Items.Count + 1, sncode.Text, weight.Text, System.DateTime.Now);
  240. sncode.Text = "";
  241. }
  242. }
  243. private void Packing_ProdWeightSet_AutoSizeChanged(object sender, EventArgs e)
  244. {
  245. }
  246. private void Packing_ProdWeightSet_SizeChanged(object sender, EventArgs e)
  247. {
  248. asc.controlAutoSize(this);
  249. }
  250. private void ma_code_UserControlTextChanged(object sender, EventArgs e)
  251. {
  252. //赋值
  253. sql.Clear();
  254. sql.Append("select ma_code,pr_code,pr_detail,pr_spec,ma_qty from make left join product on ma_prodcode=pr_code ");
  255. sql.Append("where ma_code='" + ma_code.Text + "'");
  256. Dbfind = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  257. if (Dbfind.Rows.Count == 0)
  258. {
  259. return;
  260. }
  261. BaseUtil.SetFormValue(this.Controls, Dbfind);
  262. //清空称量记录
  263. showResult.Items.Clear();
  264. indates.Clear();
  265. sncodes.Clear();
  266. weights.Clear();
  267. }
  268. private void ma_code_TextKeyDown(object sender, KeyEventArgs e)
  269. {
  270. }
  271. private void Packing_ProdWeightSet_FormClosing(object sender, FormClosingEventArgs e)
  272. {
  273. stopWeigh.PerformClick();
  274. if (serialPort1.IsOpen)
  275. {
  276. GetData = false;
  277. serialPort1.Close();
  278. SystemInf.OpenPort.Remove(serialPort1.PortName);
  279. thread.Interrupt();
  280. }
  281. //thread.Abort();
  282. }
  283. private void standardWeight_Leave(object sender, EventArgs e)
  284. {
  285. //在采样的时候不能更改
  286. if (showResult.Items.Count > 0)
  287. {
  288. if ((sender as TextBox).Name == "standardWeight" && standardWeight.Text == recordSW)
  289. {
  290. return;
  291. }
  292. else if ((sender as TextBox).Name == "errorValue" && errorValue.Text == recordEV)
  293. {
  294. return;
  295. }
  296. if (MessageBox.Show("是否修改标准重量或误差值重新采样", "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
  297. {
  298. //选择取消,数据回退
  299. standardWeight.Text = recordSW;
  300. errorValue.Text = recordEV;
  301. }
  302. else
  303. {
  304. //选择确认,重新采样
  305. showResult.Items.Clear();
  306. indates.Clear();
  307. sncodes.Clear();
  308. weights.Clear();
  309. }
  310. }
  311. }
  312. private void standardWeight_Enter(object sender, EventArgs e)
  313. {
  314. recordSW = standardWeight.Text;
  315. recordEV = errorValue.Text;
  316. }
  317. private void pr_code_TextChanged(object sender, EventArgs e)
  318. {
  319. //产品更换的时候
  320. DataTable d = (DataTable)dh.ExecuteSql("select pr_midboxmaxweight,pr_midboxminweight,PR_COLORBOXUNIT from product where pr_code='" + pr_code.Text + "'", "select");
  321. //给标准重量赋值
  322. if (d.Rows[0]["pr_midboxmaxweight"].ToString() == "" || d.Rows[0]["pr_midboxminweight"].ToString() == "")
  323. {
  324. standardWeight.Text = 0 + "";
  325. }
  326. else
  327. {
  328. //取二者平均值来
  329. standardWeight.Text = (double.Parse(d.Rows[0]["pr_midboxmaxweight"].ToString()) + double.Parse(d.Rows[0]["pr_midboxminweight"].ToString())) / 2 + "";
  330. }
  331. if (d.Rows[0]["PR_COLORBOXUNIT"].ToString() != "")
  332. {
  333. unit.Text = d.Rows[0]["PR_COLORBOXUNIT"].ToString();
  334. }
  335. else
  336. {
  337. unit.Text = "kg";
  338. }
  339. //取误差值
  340. errorValue.Text = dh.GetConfig("prodWeightErrorValue", "MESSetting").ToString();
  341. //查询重量设置采样个数
  342. try
  343. {
  344. samplesCount = int.Parse(dh.GetConfig("prodWeightSetting", "MESSetting").ToString());
  345. }
  346. catch (Exception ess)
  347. {
  348. //如果没维护的话默认是10
  349. samplesCount = 5;
  350. }
  351. OperateResult.AppendText("<<重量设置需采样个数为" + samplesCount + "\n", Color.Black);
  352. //清空称量记录
  353. showResult.Items.Clear();
  354. indates.Clear();
  355. sncodes.Clear();
  356. weights.Clear();
  357. }
  358. }
  359. }