Make_PartClean.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. using UAS_MES_NEW.DataOperate;
  12. using UAS_MES_NEW.Entity;
  13. using UAS_MES_NEW.PublicMethod;
  14. namespace UAS_MES_NEW.Make
  15. {
  16. public partial class Make_PartClean : Form
  17. {
  18. AutoSizeFormClass asc = new AutoSizeFormClass();
  19. DataHelper dh;
  20. DataTable dt;
  21. // 工序类型:清洗页面固定写CLEAN,写入SB_STEPCODE用于和零件烘烤区分
  22. const string StepCode = "CLEAN";
  23. // 当前批次号:结束清洗后置空,下一个录入的SN会生成新的批次号
  24. string currentBatch = "";
  25. // ============ TcpServer数据接收相关 ============
  26. // 监听器保持长监听,客户端连接后各起一个读取线程,收到数据切回UI线程填入SN录入框
  27. TcpListener srvListener;
  28. readonly List<TcpClient> srvClients = new List<TcpClient>();
  29. readonly object srvLock = new object();
  30. bool srvRunning = false;
  31. public Make_PartClean()
  32. {
  33. InitializeComponent();
  34. }
  35. private void Make_PartClean_Load(object sender, EventArgs e)
  36. {
  37. asc.controllInitializeSize(this);
  38. dh = SystemInf.dh;
  39. // 恢复未结束的批次(比如中途退出页面的情况)
  40. object b = dh.getFieldDataByCondition("SINGLEBOARD", "max(SB_BATCHCODE)", "SB_STEPCODE='" + StepCode + "' and SB_ENDTIME is null");
  41. currentBatch = b == null ? "" : b.ToString();
  42. if (currentBatch == "")
  43. {
  44. lblBatchCode.Text = "(暂无,录入SN时自动生成)";
  45. }
  46. else
  47. {
  48. lblBatchCode.Text = currentBatch;
  49. LoadBatchData(currentBatch);
  50. }
  51. SetStatus("就绪,请录入SN");
  52. // TcpServer默认本机IP、端口5000,进入界面自动启动
  53. txtSrvIp.Text = GetLocalIP();
  54. srvPort.Value = 5000;
  55. StartServer(true);
  56. sncode.Focus();
  57. }
  58. /// <summary>
  59. /// 录入框回车:插入SN到SINGLEBOARD,本批次共用同一个批次号
  60. /// </summary>
  61. private void sncode_KeyDown(object sender, KeyEventArgs e)
  62. {
  63. if (e.KeyCode == Keys.Enter)
  64. {
  65. InsertSN();
  66. e.Handled = true;
  67. e.SuppressKeyPress = true;
  68. }
  69. }
  70. private void InsertSN()
  71. {
  72. string sn = sncode.Text.Trim();
  73. if (sn == "")
  74. {
  75. SetStatus("SN不能为空!");
  76. return;
  77. }
  78. try
  79. {
  80. // 没有进行中的批次时生成新批次号:PC+年月日时分秒
  81. if (currentBatch == "")
  82. {
  83. currentBatch = GenBatchCode();
  84. lblBatchCode.Text = currentBatch;
  85. }
  86. // 同一SN在整个SINGLEBOARD表中不允许重复录入
  87. dt = (DataTable)dh.ExecuteSql("select SB_ID from SINGLEBOARD where SB_SNCODE='" + sn + "'", "select");
  88. if (dt.Rows.Count > 0)
  89. {
  90. SetStatus("SN【" + sn + "】已录入过,请勿重复!");
  91. sncode.Clear();
  92. sncode.Focus();
  93. return;
  94. }
  95. dh.ExecuteSql("insert into SINGLEBOARD(SB_ID,SB_SNCODE,SB_INDATE,SB_STATUS,SB_STEPCODE,SB_BATCHCODE,SB_INMAN)values(SINGLEBOARD_SEQ.nextval,'" + sn + "',sysdate,'在录入','" + StepCode + "','" + currentBatch + "','" + User.UserName + "')", "insert");
  96. SetStatus("SN【" + sn + "】录入成功,批次号" + currentBatch);
  97. sncode.Clear();
  98. sncode.Focus();
  99. LoadBatchData(currentBatch);
  100. }
  101. catch (Exception ex)
  102. {
  103. SetStatus("录入失败:" + ex.Message);
  104. }
  105. }
  106. /// <summary>
  107. /// 生成批次号:清洗PC、烘烤PD + 年月日时分秒,长度16在SB_BATCHCODE(20)范围内
  108. /// </summary>
  109. private string GenBatchCode()
  110. {
  111. return (StepCode == "CLEAN" ? "PC" : "PD") + DateTime.Now.ToString("yyyyMMddHHmmss");
  112. }
  113. /// <summary>
  114. /// 开始清洗:给当前批次所有未开始的记录写开始时间
  115. /// </summary>
  116. private void btnStart_Click(object sender, EventArgs e)
  117. {
  118. if (currentBatch == "")
  119. {
  120. MessageBox.Show("当前没有进行中的批次,请先录入SN!");
  121. return;
  122. }
  123. try
  124. {
  125. dh.ExecuteSql("update SINGLEBOARD set SB_STARTTIME='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',SB_STATUS='清洗中' where SB_BATCHCODE='" + currentBatch + "' and SB_STEPCODE='" + StepCode + "' and SB_STARTTIME is null", "update");
  126. SetStatus("批次【" + currentBatch + "】清洗已开始");
  127. LoadBatchData(currentBatch);
  128. }
  129. catch (Exception ex)
  130. {
  131. SetStatus("开始清洗失败:" + ex.Message);
  132. }
  133. }
  134. /// <summary>
  135. /// 结束清洗:给当前批次所有未结束的记录写结束时间,之后录入的SN生成新批次
  136. /// </summary>
  137. private void btnEnd_Click(object sender, EventArgs e)
  138. {
  139. if (currentBatch == "")
  140. {
  141. MessageBox.Show("当前没有进行中的批次!");
  142. return;
  143. }
  144. try
  145. {
  146. dh.ExecuteSql("update SINGLEBOARD set SB_ENDTIME='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',SB_STATUS='已完成' where SB_BATCHCODE='" + currentBatch + "' and SB_STEPCODE='" + StepCode + "' and SB_ENDTIME is null", "update");
  147. SetStatus("批次【" + currentBatch + "】已结束,下一批录入SN时生成新批次号");
  148. // 保留显示刚结束批次的数据,批次号置空
  149. LoadBatchData(currentBatch);
  150. currentBatch = "";
  151. lblBatchCode.Text = "(暂无,录入SN时自动生成)";
  152. }
  153. catch (Exception ex)
  154. {
  155. SetStatus("结束清洗失败:" + ex.Message);
  156. }
  157. }
  158. /// <summary>
  159. /// 按批次号查询
  160. /// </summary>
  161. private void btnQuery_Click(object sender, EventArgs e)
  162. {
  163. string b = txtQueryBatch.Text.Trim();
  164. if (b == "")
  165. {
  166. MessageBox.Show("请输入要查询的批次号!");
  167. return;
  168. }
  169. LoadBatchData(b);
  170. }
  171. /// <summary>
  172. /// 加载指定批次的数据显示在表格里
  173. /// </summary>
  174. private void LoadBatchData(string batch)
  175. {
  176. dt = (DataTable)dh.ExecuteSql("select SB_SNCODE \"SN\",SB_INDATE \"录入时间\",SB_STARTTIME \"开始清洗时间\",SB_INMAN \"录入人\",SB_STATUS \"状态\" from SINGLEBOARD where SB_BATCHCODE='" + batch + "' and SB_STEPCODE='" + StepCode + "' order by SB_ID", "select");
  177. DGV.DataSource = dt;
  178. }
  179. private void SetStatus(string text)
  180. {
  181. lblStatus.Text = "状态:" + text;
  182. }
  183. private void Make_PartClean_SizeChanged(object sender, EventArgs e)
  184. {
  185. asc.controlAutoSize(this);
  186. }
  187. #region TcpServer数据接收
  188. /// <summary>
  189. /// 启动TcpServer监听,silent=true时不弹窗(界面自动启动时用)
  190. /// </summary>
  191. private void StartServer(bool silent)
  192. {
  193. if (srvRunning) return;
  194. try
  195. {
  196. string sip = txtSrvIp.Text.Trim();
  197. IPAddress bindIp = (sip == "" || sip == "0.0.0.0") ? IPAddress.Any : IPAddress.Parse(sip);
  198. srvListener = new TcpListener(bindIp, (int)srvPort.Value);
  199. srvListener.Start();
  200. srvRunning = true;
  201. srvListener.BeginAcceptTcpClient(SrvAcceptCallback, null);
  202. lblSrvStatus.Text = "监听中 " + sip + ":" + srvPort.Value;
  203. SetStatus("TcpServer已启动,监听 " + sip + ":" + srvPort.Value);
  204. }
  205. catch (Exception ex)
  206. {
  207. CloseServer();
  208. lblSrvStatus.Text = "启动失败";
  209. SetStatus("TcpServer启动失败:" + ex.Message);
  210. if (!silent) MessageBox.Show("TcpServer启动失败:" + ex.Message);
  211. }
  212. }
  213. private void btnSrvOpen_Click(object sender, EventArgs e)
  214. {
  215. StartServer(false);
  216. }
  217. private void btnSrvClose_Click(object sender, EventArgs e)
  218. {
  219. CloseServer();
  220. lblSrvStatus.Text = "已停止";
  221. SetStatus("TcpServer已停止");
  222. }
  223. /// <summary>
  224. /// 停止监听并断开所有已连接的客户端
  225. /// </summary>
  226. private void CloseServer()
  227. {
  228. srvRunning = false;
  229. try
  230. {
  231. if (srvListener != null)
  232. {
  233. srvListener.Stop();
  234. }
  235. }
  236. catch { }
  237. srvListener = null;
  238. lock (srvLock)
  239. {
  240. foreach (TcpClient c in srvClients)
  241. {
  242. try { c.Close(); } catch { }
  243. }
  244. srvClients.Clear();
  245. }
  246. }
  247. /// <summary>
  248. /// 异步接受客户端连接,每个客户端起一个读取线程
  249. /// </summary>
  250. private void SrvAcceptCallback(IAsyncResult ar)
  251. {
  252. TcpClient client = null;
  253. try
  254. {
  255. if (!srvRunning || srvListener == null) return;
  256. client = srvListener.EndAcceptTcpClient(ar);
  257. lock (srvLock) srvClients.Add(client);
  258. ThreadPool.QueueUserWorkItem(SrvReadWorker, client);
  259. SafeSrvLog("客户端已连接 " + client.Client.RemoteEndPoint);
  260. }
  261. catch { }
  262. finally
  263. {
  264. try
  265. {
  266. if (srvRunning && srvListener != null)
  267. {
  268. srvListener.BeginAcceptTcpClient(SrvAcceptCallback, null);
  269. }
  270. }
  271. catch { }
  272. }
  273. }
  274. /// <summary>
  275. /// 循环读取客户端数据,按行拆分后填入SN录入框
  276. /// </summary>
  277. private void SrvReadWorker(object state)
  278. {
  279. TcpClient client = (TcpClient)state;
  280. string remote = "";
  281. try { remote = client.Client.RemoteEndPoint.ToString(); } catch { }
  282. try
  283. {
  284. NetworkStream ns = client.GetStream();
  285. byte[] buf = new byte[4096];
  286. while (srvRunning)
  287. {
  288. int n = ns.Read(buf, 0, buf.Length);
  289. if (n <= 0) break;
  290. string text = Encoding.Default.GetString(buf, 0, n);
  291. // 设备一般以回车换行结尾,按行拆分,每行一个SN
  292. string[] lines = text.Replace("\r\n", "\n").Split('\n');
  293. foreach (string line in lines)
  294. {
  295. string sn = line.Trim();
  296. if (sn != "")
  297. {
  298. OnServerDataReceived(sn);
  299. }
  300. }
  301. }
  302. }
  303. catch { }
  304. finally
  305. {
  306. try { client.Close(); } catch { }
  307. lock (srvLock) srvClients.Remove(client);
  308. SafeSrvLog("客户端断开 " + remote);
  309. }
  310. }
  311. /// <summary>
  312. /// 收到的数据填入SN录入框并直接触发录入(后台线程切回UI线程)
  313. /// </summary>
  314. private void OnServerDataReceived(string sn)
  315. {
  316. try
  317. {
  318. if (!IsHandleCreated) return;
  319. BeginInvoke((MethodInvoker)delegate
  320. {
  321. sncode.Text = sn;
  322. SafeSrvLog("收到数据【" + sn + "】→ 自动录入");
  323. InsertSN();
  324. });
  325. }
  326. catch { }
  327. }
  328. /// <summary>
  329. /// TcpServer日志:安全切回UI线程写状态栏
  330. /// </summary>
  331. private void SafeSrvLog(string text)
  332. {
  333. try
  334. {
  335. if (!IsHandleCreated) return;
  336. BeginInvoke((MethodInvoker)delegate
  337. {
  338. SetStatus("TcpServer:" + text);
  339. });
  340. }
  341. catch { }
  342. }
  343. /// <summary>
  344. /// 取本机IPv4地址作为监听默认IP
  345. /// </summary>
  346. private string GetLocalIP()
  347. {
  348. try
  349. {
  350. IPHostEntry entry = Dns.GetHostEntry(Dns.GetHostName());
  351. foreach (IPAddress a in entry.AddressList)
  352. {
  353. if (a.AddressFamily == AddressFamily.InterNetwork)
  354. {
  355. return a.ToString();
  356. }
  357. }
  358. }
  359. catch { }
  360. return "127.0.0.1";
  361. }
  362. #endregion
  363. private void Make_PartClean_FormClosing(object sender, FormClosingEventArgs e)
  364. {
  365. CloseServer();
  366. }
  367. }
  368. }