Packing_ProdWeightSet.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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.DataOperate;
  14. using UAS_MES.Entity;
  15. using UAS_MES.PublicMethod;
  16. namespace UAS_MES.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. DataHelper dh;
  26. Thread thread;
  27. //true的时候表示从串口读取数据
  28. bool GetData = true;
  29. int samplesCount = 0;
  30. string maxValue = "";//存储最大值
  31. string minValue = "";//存储最小值
  32. List<string> sncodes = new List<string>();//存序列号
  33. List<string> indates = new List<string>();//存时间
  34. List<string> weights = new List<string>();//存重量
  35. string recordSW = ""; //存标准重量
  36. string recordEV = "";//存误差值
  37. bool isKg = false;//记录单位是否KG
  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 = new DataHelper();
  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+pr_cartonunit.Text);
  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);
  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 = Regex.Replace(serialPort1.ReadLine(), "\\D+", "");
  128. }
  129. catch (Exception)
  130. {
  131. GetData = false;
  132. }
  133. }
  134. }
  135. catch (IOException ex) { MessageBox.Show(ex.Message); }
  136. }
  137. else
  138. MessageBox.Show("端口已被占用,请关闭其他窗口");
  139. }
  140. }
  141. //停止称重
  142. private void stopWeigh_Click(object sender, EventArgs e)
  143. {
  144. GetData = false;
  145. SystemInf.OpenPort.Remove(serialPort1.PortName);
  146. serialPort1.Close();
  147. }
  148. private void confirm_Click(object sender, EventArgs e)
  149. {
  150. //按确认更新产品重量
  151. if (ma_code.Text=="")
  152. {
  153. OperateResult.AppendText("<<请先选择工单\n", Color.Red);
  154. return;
  155. }
  156. //判断是否达到已称数量
  157. if (showResult.Items.Count<samplesCount)
  158. {
  159. OperateResult.AppendText("<<采样个数不足\n", Color.Red);
  160. return;
  161. }
  162. double sum = 0;
  163. //点击取平均值
  164. foreach (string s in weights)
  165. {
  166. sum += double.Parse(s);
  167. }
  168. sum = sum / weights.Count;
  169. //更新彩盒重量最大值最小值
  170. if (isKg)
  171. {
  172. dh.ExecuteSql("update product set PR_COLORBOXMAXW ='" + (sum + double.Parse(errorValue.Text))/1000 + "', PR_COLORBOXMINW = '" + (sum - double.Parse(errorValue.Text))/1000 + "',PR_COLORBOXUNIT='" + pr_cartonunit.Text + "' where pr_code='" + pr_code.Text + "'", "update");
  173. OperateResult.AppendText("<<重量设置成功,最大值" + (sum + double.Parse(errorValue.Text))/1000 + "kg,最小值" + (sum - double.Parse(errorValue.Text))/1000 + "kg\n", Color.Green);
  174. }
  175. else
  176. {
  177. dh.ExecuteSql("update product set PR_COLORBOXMAXW ='" + (sum + double.Parse(errorValue.Text)) + "', PR_COLORBOXMINW = '" + (sum - double.Parse(errorValue.Text)) + "',PR_COLORBOXUNIT='" + pr_cartonunit.Text + "' where pr_code='" + pr_code.Text + "'", "update");
  178. OperateResult.AppendText("<<重量设置成功,最大值"+ (sum + double.Parse(errorValue.Text)) + "g,最小值"+ (sum - double.Parse(errorValue.Text)) + "g\n", Color.Green);
  179. }
  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,'" + pr_cartonunit.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) == 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. if (double.Parse(weight.Text)<(double.Parse(standardWeight.Text)- double.Parse(errorValue.Text))|| double.Parse(weight.Text) >(double.Parse(standardWeight.Text) + double.Parse(errorValue.Text)))
  227. {
  228. OperateResult.AppendText("<<序列号" + sncode.Text + "重量不在预设范围内\n", Color.Red, sncode);
  229. return;
  230. }
  231. //记录重量
  232. recordResult(showResult.Items.Count+1,sncode.Text, weight.Text, System.DateTime.Now);
  233. sncode.Text = "";
  234. }
  235. }
  236. private void Packing_ProdWeightSet_AutoSizeChanged(object sender, EventArgs e)
  237. {
  238. }
  239. private void Packing_ProdWeightSet_SizeChanged(object sender, EventArgs e)
  240. {
  241. asc.controlAutoSize(this);
  242. }
  243. private void ma_code_UserControlTextChanged(object sender, EventArgs e)
  244. {
  245. //赋值
  246. sql.Clear();
  247. sql.Append("select ma_code,pr_code,pr_detail,pr_spec,ma_qty from make left join product on ma_prodcode=pr_code ");
  248. sql.Append("where ma_code='" + ma_code.Text + "'");
  249. Dbfind = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  250. if (Dbfind.Rows.Count == 0)
  251. {
  252. return;
  253. }
  254. BaseUtil.SetFormValue(this.Controls, Dbfind);
  255. //清空称量记录
  256. showResult.Items.Clear();
  257. indates.Clear();
  258. sncodes.Clear();
  259. weights.Clear();
  260. }
  261. private void ma_code_TextKeyDown(object sender, KeyEventArgs e)
  262. {
  263. }
  264. private void Packing_ProdWeightSet_FormClosing(object sender, FormClosingEventArgs e)
  265. {
  266. stopWeigh.PerformClick();
  267. if (serialPort1.IsOpen)
  268. {
  269. GetData = false;
  270. serialPort1.Close();
  271. SystemInf.OpenPort.Remove(serialPort1.PortName);
  272. thread.Interrupt();
  273. }
  274. }
  275. private void standardWeight_Leave(object sender, EventArgs e)
  276. {
  277. //在采样的时候不能更改
  278. if (showResult.Items.Count > 0)
  279. {
  280. if ((sender as TextBox).Name == "standardWeight"&& standardWeight.Text == recordSW)
  281. {
  282. return;
  283. }
  284. else if((sender as TextBox).Name == "errorValue" && errorValue.Text == recordEV)
  285. {
  286. return;
  287. }
  288. if (MessageBox.Show("是否修改标准重量或误差值重新采样", "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
  289. {
  290. //选择取消,数据回退
  291. standardWeight.Text = recordSW;
  292. errorValue.Text = recordEV;
  293. }
  294. else
  295. {
  296. //选择确认,重新采样
  297. showResult.Items.Clear();
  298. indates.Clear();
  299. sncodes.Clear();
  300. weights.Clear();
  301. }
  302. }
  303. }
  304. private void standardWeight_Enter(object sender, EventArgs e)
  305. {
  306. recordSW = standardWeight.Text;
  307. recordEV = errorValue.Text;
  308. }
  309. private void pr_code_TextChanged(object sender, EventArgs e)
  310. {
  311. //产品更换的时候
  312. DataTable d = (DataTable)dh.ExecuteSql("select PR_COLORBOXMAXW,PR_COLORBOXMINW,PR_COLORBOXUNIT from product where pr_code='" + pr_code.Text + "'", "select");
  313. isKg = false;
  314. //给标准重量赋值
  315. if (d.Rows[0]["PR_COLORBOXMAXW"].ToString() == "" || d.Rows[0]["PR_COLORBOXMINW"].ToString() == "")
  316. {
  317. standardWeight.Text = 0 + "";
  318. }
  319. else
  320. {
  321. //取二者平均值来
  322. standardWeight.Text = (double.Parse(d.Rows[0]["PR_COLORBOXMAXW"].ToString()) + double.Parse(d.Rows[0]["PR_COLORBOXMINW"].ToString())) / 2 + "";
  323. if (d.Rows[0]["PR_COLORBOXUNIT"].ToString()=="kg")
  324. {
  325. isKg = true;
  326. standardWeight.Text = double.Parse(standardWeight.Text) * 1000 + "";
  327. }
  328. }
  329. //取误差值
  330. errorValue.Text = dh.GetConfig("prodWeightErrorValue", "MESSetting").ToString();
  331. //查询重量设置采样个数
  332. try
  333. {
  334. samplesCount = int.Parse(dh.GetConfig("prodWeightSetting", "MESSetting").ToString());
  335. }
  336. catch (Exception ess)
  337. {
  338. //如果没维护的话默认是10
  339. samplesCount = 10;
  340. }
  341. OperateResult.AppendText("<<重量设置需采样个数为" + samplesCount + "\n", Color.Black);
  342. //清空称量记录
  343. showResult.Items.Clear();
  344. indates.Clear();
  345. sncodes.Clear();
  346. weights.Clear();
  347. }
  348. }
  349. }