Packing_ProdWeightSet.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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_ProdWeightSet : 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_ProdWeightSet()
  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. dh.ExecuteSql("update product set PR_COLORBOXMAXW ='" + (double.Parse(standardWeight.Text) + double.Parse(errorValue.Text)) + "', PR_COLORBOXMINW = '" + (double.Parse(standardWeight.Text) - double.Parse(errorValue.Text)) + "' where pr_code='" + pr_code.Text + "'", "update");
  176. //更新此产品需要检测彩盒重量
  177. dh.ExecuteSql("update product set pr_checkcolorboxw = 1 where pr_code='" + pr_code.Text + "'", "update");
  178. 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);
  179. 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,"","");
  180. //更新采样记录表
  181. string serialNum = dh.GetSerialNumberByCaller("ProdWeightSample");
  182. sql.Clear();
  183. sql.Append("insert into PRODWEIGHTSAMPLE(PWS_CODE,PWS_SNCODE,PWS_MAKECODE,PWS_PRODCODE,PWS_INDATE,PWS_WEIGHT,PWS_UNIT) ");
  184. sql.Append("values ('"+serialNum+"',:sncode,'"+ma_code.Text+"','"+pr_code.Text+ "',TO_Date(:indate,'YYYY-MM-dd HH24:mi:ss'),:weight,'" + unit.Text+"')");
  185. dh.BatchInsert(sql.GetString(), new string[] { "sncode", "indate" , "weight" },sncodes.ToArray(),indates.ToArray(),weights.ToArray());
  186. }
  187. private void sncode_KeyDown(object sender, KeyEventArgs e)
  188. {
  189. //按下了enter键
  190. if (e.KeyCode == Keys.Enter)
  191. {
  192. if (sncode.Text == "")
  193. {
  194. OperateResult.AppendText("<<输入不能为空\n", Color.Red);
  195. return;
  196. }
  197. if (weight.Text == "")
  198. {
  199. OperateResult.AppendText("<<未读取到重量信息\n", Color.Red);
  200. return;
  201. }
  202. if (double.Parse(weight.Text.Replace("kg", "").Trim()) == 0)
  203. {
  204. OperateResult.AppendText("<<重量不能等于0\n", Color.Red, sncode);
  205. return;
  206. }
  207. //验证序列号是否存在
  208. if (!dh.CheckExist("makeserial", "ms_sncode = '" + sncode.Text + "'"))
  209. {
  210. OperateResult.AppendText("<<序列号" + sncode.Text + "不存在\n", Color.Red, sncode);
  211. return;
  212. }
  213. //验证彩盒是否与产品对应
  214. if (!dh.CheckExist("makeserial","ms_prodcode='"+pr_code.Text+"' and ms_sncode = '"+sncode.Text+"'"))
  215. {
  216. OperateResult.AppendText("<<序列号"+sncode.Text+"对应产品编号不是"+pr_code.Text+"\n", Color.Red, sncode);
  217. return;
  218. }
  219. //验证所称彩盒是否重复
  220. if (sncodes.Contains(sncode.Text))
  221. {
  222. OperateResult.AppendText("<<序列号" + sncode.Text + "已经称过\n", Color.Red, sncode);
  223. return;
  224. }
  225. //判断称重记录是否满足标准重量+-误差值
  226. double ActualWeight = double.Parse(weight.Text == "" ? "0" : weight.Text.Replace("kg", "").Trim());
  227. if (ActualWeight < (double.Parse(standardWeight.Text)- double.Parse(errorValue.Text))|| ActualWeight > (double.Parse(standardWeight.Text) + double.Parse(errorValue.Text)))
  228. {
  229. OperateResult.AppendText("<<序列号" + sncode.Text + "重量不在预设范围内\n", Color.Red, sncode);
  230. return;
  231. }
  232. //记录重量
  233. recordResult(showResult.Items.Count+1,sncode.Text, weight.Text, System.DateTime.Now);
  234. sncode.Text = "";
  235. }
  236. }
  237. private void Packing_ProdWeightSet_AutoSizeChanged(object sender, EventArgs e)
  238. {
  239. }
  240. private void Packing_ProdWeightSet_SizeChanged(object sender, EventArgs e)
  241. {
  242. asc.controlAutoSize(this);
  243. }
  244. private void ma_code_UserControlTextChanged(object sender, EventArgs e)
  245. {
  246. //赋值
  247. sql.Clear();
  248. sql.Append("select ma_code,pr_code,pr_detail,pr_spec,ma_qty from make left join product on ma_prodcode=pr_code ");
  249. sql.Append("where ma_code='" + ma_code.Text + "'");
  250. Dbfind = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  251. if (Dbfind.Rows.Count == 0)
  252. {
  253. return;
  254. }
  255. BaseUtil.SetFormValue(this.Controls, Dbfind);
  256. //清空称量记录
  257. showResult.Items.Clear();
  258. indates.Clear();
  259. sncodes.Clear();
  260. weights.Clear();
  261. }
  262. private void ma_code_TextKeyDown(object sender, KeyEventArgs e)
  263. {
  264. }
  265. private void Packing_ProdWeightSet_FormClosing(object sender, FormClosingEventArgs e)
  266. {
  267. stopWeigh.PerformClick();
  268. if (serialPort1.IsOpen)
  269. {
  270. GetData = false;
  271. serialPort1.Close();
  272. SystemInf.OpenPort.Remove(serialPort1.PortName);
  273. thread.Interrupt();
  274. }
  275. //thread.Abort();
  276. }
  277. private void standardWeight_Leave(object sender, EventArgs e)
  278. {
  279. //在采样的时候不能更改
  280. if (showResult.Items.Count > 0)
  281. {
  282. if ((sender as TextBox).Name == "standardWeight"&& standardWeight.Text == recordSW)
  283. {
  284. return;
  285. }
  286. else if((sender as TextBox).Name == "errorValue" && errorValue.Text == recordEV)
  287. {
  288. return;
  289. }
  290. if (MessageBox.Show("是否修改标准重量或误差值重新采样", "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
  291. {
  292. //选择取消,数据回退
  293. standardWeight.Text = recordSW;
  294. errorValue.Text = recordEV;
  295. }
  296. else
  297. {
  298. //选择确认,重新采样
  299. showResult.Items.Clear();
  300. indates.Clear();
  301. sncodes.Clear();
  302. weights.Clear();
  303. }
  304. }
  305. }
  306. private void standardWeight_Enter(object sender, EventArgs e)
  307. {
  308. recordSW = standardWeight.Text;
  309. recordEV = errorValue.Text;
  310. }
  311. private void pr_code_TextChanged(object sender, EventArgs e)
  312. {
  313. //产品更换的时候
  314. DataTable d = (DataTable)dh.ExecuteSql("select PR_COLORBOXMAXW,PR_COLORBOXMINW,PR_COLORBOXUNIT from product where pr_code='" + pr_code.Text + "'", "select");
  315. //给标准重量赋值
  316. if (d.Rows[0]["PR_COLORBOXMAXW"].ToString() == "" || d.Rows[0]["PR_COLORBOXMINW"].ToString() == "")
  317. {
  318. standardWeight.Text = 0 + "";
  319. }
  320. else
  321. {
  322. //取二者平均值来
  323. standardWeight.Text = (double.Parse(d.Rows[0]["PR_COLORBOXMAXW"].ToString()) + double.Parse(d.Rows[0]["PR_COLORBOXMINW"].ToString())) / 2 + "";
  324. }
  325. if (d.Rows[0]["PR_COLORBOXUNIT"].ToString() != "")
  326. {
  327. unit.Text = d.Rows[0]["PR_COLORBOXUNIT"].ToString();
  328. }
  329. else
  330. {
  331. unit.Text = "kg";
  332. }
  333. //取误差值
  334. errorValue.Text = dh.GetConfig("prodWeightErrorValue", "MESSetting").ToString();
  335. //查询重量设置采样个数
  336. try
  337. {
  338. samplesCount = int.Parse(dh.GetConfig("prodWeightSetting", "MESSetting").ToString());
  339. }
  340. catch (Exception ess)
  341. {
  342. //如果没维护的话默认是10
  343. samplesCount = 10;
  344. }
  345. OperateResult.AppendText("<<重量设置需采样个数为" + samplesCount + "\n", Color.Black);
  346. //清空称量记录
  347. showResult.Items.Clear();
  348. indates.Clear();
  349. sncodes.Clear();
  350. weights.Clear();
  351. }
  352. }
  353. }