| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- using UAS_MES_NEW.DataOperate;
- using UAS_MES_NEW.Entity;
- using UAS_MES_NEW.PublicMethod;
- namespace UAS_MES_NEW.Make
- {
- public partial class Make_PartClean : Form
- {
- AutoSizeFormClass asc = new AutoSizeFormClass();
- DataHelper dh;
- DataTable dt;
- // 工序类型:清洗页面固定写CLEAN,写入SB_STEPCODE用于和零件烘烤区分
- const string StepCode = "CLEAN";
- // 当前批次号:结束清洗后置空,下一个录入的SN会生成新的批次号
- string currentBatch = "";
- // ============ TcpServer数据接收相关 ============
- // 监听器保持长监听,客户端连接后各起一个读取线程,收到数据切回UI线程填入SN录入框
- TcpListener srvListener;
- readonly List<TcpClient> srvClients = new List<TcpClient>();
- readonly object srvLock = new object();
- bool srvRunning = false;
- public Make_PartClean()
- {
- InitializeComponent();
- }
- private void Make_PartClean_Load(object sender, EventArgs e)
- {
- asc.controllInitializeSize(this);
- dh = SystemInf.dh;
- // 恢复未结束的批次(比如中途退出页面的情况)
- object b = dh.getFieldDataByCondition("SINGLEBOARD", "max(SB_BATCHCODE)", "SB_STEPCODE='" + StepCode + "' and SB_ENDTIME is null");
- currentBatch = b == null ? "" : b.ToString();
- if (currentBatch == "")
- {
- lblBatchCode.Text = "(暂无,录入SN时自动生成)";
- }
- else
- {
- lblBatchCode.Text = currentBatch;
- LoadBatchData(currentBatch);
- }
- SetStatus("就绪,请录入SN");
- // TcpServer默认本机IP、端口5000,进入界面自动启动
- txtSrvIp.Text = GetLocalIP();
- srvPort.Value = 5000;
- StartServer(true);
- sncode.Focus();
- }
- /// <summary>
- /// 录入框回车:插入SN到SINGLEBOARD,本批次共用同一个批次号
- /// </summary>
- private void sncode_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter)
- {
- InsertSN();
- e.Handled = true;
- e.SuppressKeyPress = true;
- }
- }
- private void InsertSN()
- {
- string sn = sncode.Text.Trim();
- if (sn == "")
- {
- SetStatus("SN不能为空!");
- return;
- }
- try
- {
- // 没有进行中的批次时生成新批次号:PC+年月日时分秒
- if (currentBatch == "")
- {
- currentBatch = GenBatchCode();
- lblBatchCode.Text = currentBatch;
- }
- // 同一SN在同一工序(SB_STEPCODE)内不允许重复录入,跨工序(清洗/烘烤)可以各录一次
- dt = (DataTable)dh.ExecuteSql("select SB_ID from SINGLEBOARD where SB_SNCODE='" + sn + "' and SB_STEPCODE='" + StepCode + "'", "select");
- if (dt.Rows.Count > 0)
- {
- SetStatus("SN【" + sn + "】已录入过,请勿重复!");
- sncode.Clear();
- sncode.Focus();
- return;
- }
- 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");
- SetStatus("SN【" + sn + "】录入成功,批次号" + currentBatch);
- sncode.Clear();
- sncode.Focus();
- LoadBatchData(currentBatch);
- }
- catch (Exception ex)
- {
- SetStatus("录入失败:" + ex.Message);
- }
- }
- /// <summary>
- /// 生成批次号:清洗PC、烘烤PD + 年月日时分秒,长度16在SB_BATCHCODE(20)范围内
- /// </summary>
- private string GenBatchCode()
- {
- return (StepCode == "CLEAN" ? "PC" : "PD") + DateTime.Now.ToString("yyyyMMddHHmmss");
- }
- /// <summary>
- /// 开始清洗:给当前批次所有未开始的记录写开始时间
- /// </summary>
- private void btnStart_Click(object sender, EventArgs e)
- {
- if (currentBatch == "")
- {
- MessageBox.Show("当前没有进行中的批次,请先录入SN!");
- return;
- }
- try
- {
- 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");
- SetStatus("批次【" + currentBatch + "】清洗已开始");
- LoadBatchData(currentBatch);
- }
- catch (Exception ex)
- {
- SetStatus("开始清洗失败:" + ex.Message);
- }
- }
- /// <summary>
- /// 结束清洗:给当前批次所有未结束的记录写结束时间,之后录入的SN生成新批次
- /// </summary>
- private void btnEnd_Click(object sender, EventArgs e)
- {
- if (currentBatch == "")
- {
- MessageBox.Show("当前没有进行中的批次!");
- return;
- }
- try
- {
- 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");
- SetStatus("批次【" + currentBatch + "】已结束,下一批录入SN时生成新批次号");
- // 保留显示刚结束批次的数据,批次号置空
- LoadBatchData(currentBatch);
- currentBatch = "";
- lblBatchCode.Text = "(暂无,录入SN时自动生成)";
- }
- catch (Exception ex)
- {
- SetStatus("结束清洗失败:" + ex.Message);
- }
- }
- /// <summary>
- /// 按批次号查询
- /// </summary>
- private void btnQuery_Click(object sender, EventArgs e)
- {
- string b = txtQueryBatch.Text.Trim();
- if (b == "")
- {
- MessageBox.Show("请输入要查询的批次号!");
- return;
- }
- LoadBatchData(b);
- }
- /// <summary>
- /// 加载指定批次的数据显示在表格里
- /// </summary>
- private void LoadBatchData(string batch)
- {
- 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");
- DGV.DataSource = dt;
- }
- private void SetStatus(string text)
- {
- lblStatus.Text = "状态:" + text;
- }
- private void Make_PartClean_SizeChanged(object sender, EventArgs e)
- {
- asc.controlAutoSize(this);
- }
- #region TcpServer数据接收
- /// <summary>
- /// 启动TcpServer监听,silent=true时不弹窗(界面自动启动时用)
- /// </summary>
- private void StartServer(bool silent)
- {
- if (srvRunning) return;
- try
- {
- string sip = txtSrvIp.Text.Trim();
- IPAddress bindIp = (sip == "" || sip == "0.0.0.0") ? IPAddress.Any : IPAddress.Parse(sip);
- srvListener = new TcpListener(bindIp, (int)srvPort.Value);
- srvListener.Start();
- srvRunning = true;
- srvListener.BeginAcceptTcpClient(SrvAcceptCallback, null);
- lblSrvStatus.Text = "监听中 " + sip + ":" + srvPort.Value;
- SetStatus("TcpServer已启动,监听 " + sip + ":" + srvPort.Value);
- }
- catch (Exception ex)
- {
- CloseServer();
- lblSrvStatus.Text = "启动失败";
- SetStatus("TcpServer启动失败:" + ex.Message);
- if (!silent) MessageBox.Show("TcpServer启动失败:" + ex.Message);
- }
- }
- private void btnSrvOpen_Click(object sender, EventArgs e)
- {
- StartServer(false);
- }
- private void btnSrvClose_Click(object sender, EventArgs e)
- {
- CloseServer();
- lblSrvStatus.Text = "已停止";
- SetStatus("TcpServer已停止");
- }
- /// <summary>
- /// 停止监听并断开所有已连接的客户端
- /// </summary>
- private void CloseServer()
- {
- srvRunning = false;
- try
- {
- if (srvListener != null)
- {
- srvListener.Stop();
- }
- }
- catch { }
- srvListener = null;
- lock (srvLock)
- {
- foreach (TcpClient c in srvClients)
- {
- try { c.Close(); } catch { }
- }
- srvClients.Clear();
- }
- }
- /// <summary>
- /// 异步接受客户端连接,每个客户端起一个读取线程
- /// </summary>
- private void SrvAcceptCallback(IAsyncResult ar)
- {
- TcpClient client = null;
- try
- {
- if (!srvRunning || srvListener == null) return;
- client = srvListener.EndAcceptTcpClient(ar);
- lock (srvLock) srvClients.Add(client);
- ThreadPool.QueueUserWorkItem(SrvReadWorker, client);
- SafeSrvLog("客户端已连接 " + client.Client.RemoteEndPoint);
- }
- catch { }
- finally
- {
- try
- {
- if (srvRunning && srvListener != null)
- {
- srvListener.BeginAcceptTcpClient(SrvAcceptCallback, null);
- }
- }
- catch { }
- }
- }
- /// <summary>
- /// 循环读取客户端数据,按行拆分后填入SN录入框
- /// </summary>
- private void SrvReadWorker(object state)
- {
- TcpClient client = (TcpClient)state;
- string remote = "";
- try { remote = client.Client.RemoteEndPoint.ToString(); } catch { }
- try
- {
- NetworkStream ns = client.GetStream();
- byte[] buf = new byte[4096];
- while (srvRunning)
- {
- int n = ns.Read(buf, 0, buf.Length);
- if (n <= 0) break;
- string text = Encoding.Default.GetString(buf, 0, n);
- // 设备一般以回车换行结尾,按行拆分,每行一个SN
- string[] lines = text.Replace("\r\n", "\n").Split('\n');
- foreach (string line in lines)
- {
- string sn = line.Trim();
- if (sn != "")
- {
- OnServerDataReceived(sn);
- }
- }
- }
- }
- catch { }
- finally
- {
- try { client.Close(); } catch { }
- lock (srvLock) srvClients.Remove(client);
- SafeSrvLog("客户端断开 " + remote);
- }
- }
- /// <summary>
- /// 收到的数据填入SN录入框并直接触发录入(后台线程切回UI线程)
- /// </summary>
- private void OnServerDataReceived(string sn)
- {
- try
- {
- if (!IsHandleCreated) return;
- BeginInvoke((MethodInvoker)delegate
- {
- sncode.Text = sn;
- SafeSrvLog("收到数据【" + sn + "】→ 自动录入");
- InsertSN();
- });
- }
- catch { }
- }
- /// <summary>
- /// TcpServer日志:安全切回UI线程写状态栏
- /// </summary>
- private void SafeSrvLog(string text)
- {
- try
- {
- if (!IsHandleCreated) return;
- BeginInvoke((MethodInvoker)delegate
- {
- SetStatus("TcpServer:" + text);
- });
- }
- catch { }
- }
- /// <summary>
- /// 取本机IPv4地址作为监听默认IP
- /// </summary>
- private string GetLocalIP()
- {
- try
- {
- IPHostEntry entry = Dns.GetHostEntry(Dns.GetHostName());
- foreach (IPAddress a in entry.AddressList)
- {
- if (a.AddressFamily == AddressFamily.InterNetwork)
- {
- return a.ToString();
- }
- }
- }
- catch { }
- return "127.0.0.1";
- }
- #endregion
- private void Make_PartClean_FormClosing(object sender, FormClosingEventArgs e)
- {
- CloseServer();
- }
- }
- }
|