Make_AirTightnessTest.cs 12 KB

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