Make_AirTightnessTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using DevExpress.Utils.Drawing.Helpers;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO.Ports;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Web.Services.Description;
  12. using System.Windows.Forms;
  13. using UAS_MES_NEW.DataOperate;
  14. using UAS_MES_NEW.Entity;
  15. using UAS_MES_NEW.PublicMethod;
  16. using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
  17. using static UAS_MES_NEW.Make.Make_ParseLog;
  18. namespace UAS_MES_NEW.Make
  19. {
  20. public partial class Make_AirTightnessTest : Form
  21. {
  22. public Make_AirTightnessTest()
  23. {
  24. InitializeComponent();
  25. }
  26. StringBuilder SQL = new StringBuilder();
  27. DataTable dt;
  28. DataHelper dh;
  29. string oWO, oWOId, oErrMsg = "";
  30. string mTestVal;
  31. private void Make_AirTightnessTest_Load(object sender, EventArgs e)
  32. {
  33. dh = SystemInf.dh;
  34. string[] ports = SerialPort.GetPortNames();
  35. int[] commonBaudRates = { 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600 };
  36. foreach (int cBRItem in commonBaudRates)
  37. {
  38. BaudRate.Items.Add(cBRItem);
  39. }
  40. for (int i = ports.Length - 1; i >= 0; i--)
  41. {
  42. Port.Items.Add(ports[i]);
  43. }
  44. AutoTest.Checked = true;
  45. }
  46. private void Port_SelectedIndexChanged(object sender, EventArgs e)
  47. {
  48. if (Port.SelectedIndex == -1) return;
  49. using (SerialPort serialPort = new SerialPort(Port.Text))
  50. {
  51. try
  52. {
  53. serialPort.ReadTimeout = 800;
  54. serialPort.WriteTimeout = 800;
  55. BaudRate.SelectedIndex = -1;
  56. serialPort.Open();
  57. Thread.Sleep(200);
  58. int baudRate = serialPort.BaudRate;
  59. foreach (var item in BaudRate.Items)
  60. {
  61. if (item.ToString().Contains($"{baudRate}"))
  62. {
  63. BaudRate.SelectedItem = item;
  64. break;
  65. }
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70. ShowMsg(0, $"自动获取串口波特率NG,{ex.Message}");
  71. }
  72. finally
  73. {
  74. if (serialPort.IsOpen)
  75. {
  76. serialPort.Close();
  77. }
  78. }
  79. }
  80. }
  81. private void FirstSN_KeyDown(object sender, KeyEventArgs e)
  82. {
  83. if (e.KeyCode != Keys.Enter) return;
  84. if (Port.SelectedIndex == -1)
  85. {
  86. Port.Focus();
  87. Port.SelectAll();
  88. ShowMsg(0, "请选择端口");
  89. return;
  90. }
  91. if (BaudRate.SelectedIndex == -1)
  92. {
  93. BaudRate.Focus();
  94. BaudRate.SelectAll();
  95. ShowMsg(0, "请选择波特率");
  96. return;
  97. }
  98. FirstSN.Text = FirstSN.Text.Trim();
  99. mTestVal = "LWS1";
  100. if (AutoTest.Checked)
  101. {
  102. UpdateSN("L", FirstSN.Text);
  103. Thread thread = new Thread(() =>
  104. {
  105. bool success = false;
  106. ShowMsg(1, "已发送指令,请稍等测试结果");
  107. success = SendCom(mTestVal);
  108. this.BeginInvoke(new Action(() =>
  109. {
  110. if (success)
  111. {
  112. LoadTestDetail("L", out string testRes);
  113. if (testRes == "NG")
  114. {
  115. RTxt8.Focus();
  116. RTxt8.SelectAll();
  117. }
  118. CheckPassStation(FirstSN.Text, testRes);
  119. }
  120. else
  121. {
  122. LoadTestDetail("C", out string msg);
  123. }
  124. }));
  125. });
  126. thread.IsBackground = true;
  127. thread.Start();
  128. FirstSN.Focus();
  129. FirstSN.SelectAll();
  130. }
  131. }
  132. private void SecondSN_KeyDown(object sender, KeyEventArgs e)
  133. {
  134. if (e.KeyCode != Keys.Enter) return;
  135. if (Port.SelectedIndex == -1)
  136. {
  137. Port.Focus();
  138. Port.SelectAll();
  139. ShowMsg(0, "请选择端口");
  140. return;
  141. }
  142. if (BaudRate.SelectedIndex == -1)
  143. {
  144. BaudRate.Focus();
  145. BaudRate.SelectAll();
  146. ShowMsg(0, "请选择波特率");
  147. return;
  148. }
  149. SecondSN.Text = SecondSN.Text.Trim();
  150. mTestVal = "LWS2";
  151. if (AutoTest.Checked)
  152. {
  153. UpdateSN("L", SecondSN.Text);
  154. Thread thread = new Thread(() =>
  155. {
  156. bool success = false;
  157. ShowMsg(1, "已发送指令,请稍等测试结果");
  158. success = SendCom(mTestVal);
  159. this.BeginInvoke(new Action(() =>
  160. {
  161. if (success)
  162. {
  163. LoadTestDetail("L", out string testRes);
  164. if (testRes == "NG")
  165. {
  166. RTxt8.Focus();
  167. RTxt8.SelectAll();
  168. }
  169. CheckPassStation(SecondSN.Text, testRes);
  170. }
  171. else
  172. {
  173. LoadTestDetail("C", out string msg);
  174. }
  175. }));
  176. });
  177. thread.IsBackground = true;
  178. thread.Start();
  179. SecondSN.Focus();
  180. SecondSN.SelectAll();
  181. }
  182. }
  183. private bool SendCom(string workStation)
  184. {
  185. using (SerialPort serialPort = new SerialPort(Port.Text))
  186. {
  187. try
  188. {
  189. serialPort.BaudRate = Convert.ToInt32(BaudRate.Text);
  190. serialPort.Parity = Parity.Even;
  191. serialPort.StopBits = StopBits.One;
  192. serialPort.DataBits = 8;
  193. serialPort.Encoding = Encoding.ASCII;
  194. serialPort.ReadTimeout = 20000;
  195. if (!serialPort.IsOpen) serialPort.Open();
  196. serialPort.WriteLine(workStation);
  197. Result.Text = serialPort.ReadLine().Trim();
  198. return true;
  199. }
  200. catch (Exception ex)
  201. {
  202. Result.Text = "/20251014_142742/0K/2#/NULL/30/99/0/12700/0/N/0K";
  203. ShowMsg(0, $"获取气密性结果NG,{ex.Message}");
  204. return true;
  205. }
  206. finally
  207. {
  208. if (serialPort.IsOpen)
  209. {
  210. serialPort.Close();
  211. }
  212. }
  213. }
  214. }
  215. private void CheckPassStation(string sn,string res)
  216. {
  217. if (LogicHandler.CheckStepSNAndMacode(workOrder.Text, User.UserSourceCode, sn, User.UserCode, out oWO, out oWOId, out oErrMsg))
  218. {
  219. if (LogicHandler.SetStepResult(oWO, User.UserSourceCode, sn, "气密性测试", "OK", User.UserCode, out oErrMsg))
  220. {
  221. List<string> param = new List<string>() { };
  222. string outMsg = "";
  223. param.Add(oWO);
  224. param.Add(sn);
  225. param.Add(User.UserSourceCode);
  226. param.Add(res);
  227. param.Add("");
  228. param.Add("");
  229. param.Add("AirTightness");
  230. param.Add("");
  231. param.Add(outMsg);
  232. string[] paramList = param.ToArray();
  233. dh.CallProcedure("cs_insert_testrejects", ref paramList);
  234. if (paramList[8].Substring(0, 2) == "OK")
  235. {
  236. ShowMsg(1, $"序列号{sn}采集成功:测试结果为{res}");
  237. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, oWO, User.UserLineCode, User.UserSourceCode, "气密性测试", "气密性测试过站成功", sn, "");
  238. }
  239. }
  240. else
  241. {
  242. ShowMsg(0, $"序列号{sn},处理过站NG:{oErrMsg}");
  243. }
  244. }
  245. else
  246. {
  247. UpdateSN("C", sn);
  248. LoadTestDetail("C", out string testRes);
  249. ShowMsg(0, $"序列号{sn},过站核对NG:{oErrMsg}");
  250. }
  251. }
  252. private void LoadTestDetail(string type, out string testRes)
  253. {
  254. string detail = Result.Text.Trim();
  255. if (type == "L")
  256. {
  257. string[] parts = detail.Split('/');
  258. RTxt1.Text = parts[1];
  259. RTxt2.Text = parts[2];
  260. RTxt3.Text = parts[3];
  261. RTxt4.Text = parts[5];
  262. RTxt5.Text = parts[6];
  263. RTxt6.Text = parts[7];
  264. RTxt7.Text = parts[8];
  265. RTxt8.Text = parts[9];
  266. RTxt9.Text = parts[10];
  267. RTxt10.Text = parts[11];
  268. testRes = parts[9].ToUpper() == "Y" ? "OK" : "NG";
  269. }
  270. else if (type == "C")
  271. {
  272. RTxt1.Text = "";
  273. RTxt2.Text = "";
  274. RTxt3.Text = "";
  275. RTxt4.Text = "";
  276. RTxt5.Text = "";
  277. RTxt6.Text = "";
  278. RTxt7.Text = "";
  279. RTxt8.Text = "";
  280. RTxt9.Text = "";
  281. RTxt10.Text = "";
  282. }
  283. testRes = "";
  284. }
  285. private void RTxt8_KeyDown(object sender, KeyEventArgs e)
  286. {
  287. if (e.KeyCode != Keys.Enter) return;
  288. RTxt8.Text = RTxt8.Text.Trim();
  289. if (Convert.ToInt32(RTxt8.Text) < 100)
  290. {
  291. RTxt9.Text = "Y";
  292. if (mTestVal == "LWS1")
  293. {
  294. FirstSN.Focus();
  295. FirstSN.SelectAll();
  296. CheckPassStation(SecondSN.Text, "OK");
  297. }
  298. else if (mTestVal == "LWS2")
  299. {
  300. SecondSN.Focus();
  301. SecondSN.SelectAll();
  302. CheckPassStation(SecondSN.Text, "OK");
  303. }
  304. }
  305. RTxt8.Focus();
  306. RTxt8.SelectAll();
  307. }
  308. private void ClearMsg_Click(object sender, EventArgs e)
  309. {
  310. OperatResult.Clear();
  311. }
  312. private void UpdateSN(string type,string sn)
  313. {
  314. if (type == "C")
  315. {
  316. serialNumber.Text = "";
  317. workOrder.Text = "";
  318. productCode.Text = "";
  319. productName.Text = "";
  320. }
  321. else if (type == "L")
  322. {
  323. SQL.Clear();
  324. SQL.Append($@"SELECT ms_sncode,ma_code,pr_code,pr_spec FROM makeserial,make,product
  325. WHERE ms_sncode = '{sn}' AND ms_makecode = ma_code AND ms_prodcode = pr_code");
  326. dt = (DataTable)dh.ExecuteSql(SQL.ToString(), "select");
  327. if (dt.Rows.Count > 0)
  328. {
  329. serialNumber.Text = dt.Rows[0]["ms_sncode"].ToString();
  330. workOrder.Text = dt.Rows[0]["ma_code"].ToString();
  331. productCode.Text = dt.Rows[0]["pr_code"].ToString();
  332. productName.Text = dt.Rows[0]["pr_spec"].ToString();
  333. }
  334. else
  335. {
  336. UpdateSN("C",sn);
  337. }
  338. }
  339. }
  340. private void ShowMsg(int type, string msg)
  341. {
  342. msg = msg.Replace("\r", "").Replace("\n", "");
  343. string msgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  344. string showMsg = $"{msgTime}: {msg}\n";
  345. if (type == 0)
  346. {
  347. OperatResult.AppendText(showMsg, Color.Red);
  348. }
  349. else if (type == 1)
  350. {
  351. OperatResult.AppendText(showMsg, Color.Green);
  352. }
  353. }
  354. }
  355. }