From 233737bc31dcd1a3e1843e2d607a1ade3152e815 Mon Sep 17 00:00:00 2001 From: gf zhou Date: Wed, 16 Apr 2025 11:31:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8E=A5=E5=8F=A3null?= =?UTF-8?q?=E5=80=BC=E6=A3=80=E6=9F=A5=20=E6=89=93=E5=BC=80=E8=9E=BA?= =?UTF-8?q?=E4=B8=9D=E5=BC=95=E5=AF=BC=E9=A1=B5=E9=9D=A2=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=20=E7=A8=8B=E5=BA=8F=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E6=89=93=E5=BC=80=E5=91=BD=E5=90=8D=E4=BF=AE=E6=94=B9=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BF=83=E8=B7=B3=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WindowsFormsApp1/MainForm.cs | 122 ++++++++++++++-------- WindowsFormsApp1/Program.cs | 1 + WindowsFormsApp1/form/ImageDisplayForm.cs | 5 + WindowsFormsApp1/server/ApiClient.cs | 114 +++++++++++++++++--- 4 files changed, 186 insertions(+), 56 deletions(-) diff --git a/WindowsFormsApp1/MainForm.cs b/WindowsFormsApp1/MainForm.cs index a6815db..3e8c970 100644 --- a/WindowsFormsApp1/MainForm.cs +++ b/WindowsFormsApp1/MainForm.cs @@ -77,7 +77,7 @@ namespace WindowsFormsApp1 { bool createdNew; // 创建一个全局命名的互斥体 - _singleInstanceMutex = new Mutex(true, "WindowsFormsApp1_MES_PLC_Control_SingleInstance", out createdNew); + _singleInstanceMutex = new Mutex(true, "WindowsFormsApp1_MES_PLC_Control_SingleInstance" + Init.t_Line.linename, out createdNew); return createdNew; } @@ -182,7 +182,8 @@ namespace WindowsFormsApp1 continue; } - //写入心跳 + /* + * //写入心跳 在主线程取消,单独开线程 写plc心跳 PlcData.Write_Heartbeat = PlcData.Write_Heartbeat == 0 ? (short)1 : (short)0; var haertresult = siemensHelp.WriteAddress(plcaddress, PlcData); if (!haertresult.Success) @@ -191,7 +192,7 @@ namespace WindowsFormsApp1 //PLC链接状态.BackColor = Color.Red; LogExecute.logNet.WriteError($"{Plc.plcname}:{Plc.plcip}写入心跳错误:处理停止"); continue; - } + }*/ if (Init.t_Lineplcs.Count >= 2 && Plc.id == Init.t_Lineplcs.First().plcid) @@ -231,6 +232,7 @@ namespace WindowsFormsApp1 材料值.Text = PlcData.Read_MaterialCode2; Init.ImageLocation = PlcData.Read_Other4; Init.ImageName = PlcData.Read_Other3.ToString(); + Init.ScrewBegin = PlcData.Read_Other2; #region 螺丝引导相关 // 螺丝 OK/NG @@ -268,34 +270,10 @@ namespace WindowsFormsApp1 { if (!imageFormIsOpened) { - ThreadHelp threadHelp = new ThreadHelp(); - if (!threadHelp.isrunning1) - { - threadHelp.cancellationTokenSource1 = new CancellationTokenSource(); - threadHelp.Thread = new Thread(() => 螺丝引导ToolStripMenuItem_Click(this, EventArgs.Empty)); - threadHelp.Thread.Name = $"imagedisplay_Thread"; // 显式设置线程名称 - threadHelp.Thread.Start(); - threadHelp.isrunning1 = true; - } - Init.threadHelps.Add(threadHelp); - } - } - // 结束打螺丝 0 - else - { - if (imageFormIsOpened) - { - var list = Init.threadHelps.Where(x => x.Thread.Name == "imagedisplay_Thread" && x.Thread.IsAlive).ToList(); - foreach (var thread in list) - { - if (thread.Thread != null) - { - thread.cancellationTokenSource1?.Cancel(); - thread.Thread.Abort(); - thread.isrunning1 = false; - } - Init.threadHelps.Remove(thread); - } + this.Invoke(new Action(() => { + ImageDisplayForm imageDisplayForm = new ImageDisplayForm(); + imageDisplayForm.Show(); + })); } } #endregion 螺丝引导相关 @@ -471,6 +449,7 @@ namespace WindowsFormsApp1 if (nowflow.dzscan) { //扫码 + ScanForm scanForm = new ScanForm(); DialogResult scan_result = scanForm.ShowDialog(); if (scan_result == DialogResult.OK) @@ -743,11 +722,7 @@ namespace WindowsFormsApp1 { ChangeBarcode(); MES_MsgfORM(nowinterface.code, result, result.Requesttime); - //出站清空sn - if(nowinterface.code == "SN_Checkout") - { - PlcData.Write_stSN = ""; - } + var writeresult = siemensHelp.WriteAddress(plcaddress, PlcData); if (!writeresult.Success) { @@ -756,15 +731,13 @@ namespace WindowsFormsApp1 } else { - LogExecute.logNet.WriteDebug($"上位机写入触发字:{PlcData.Write_Status}"); - if (result.Mesresult != mesresult.PASS) { - if (resdresult.Msg.Contains("机台上料数量不够扣料")) + if (result.Msg.Contains("机台上料数量不够扣料")) { PlcData.Write_Status = 1003; } - else if (resdresult.Msg.Contains("此载具为空")) + else if (result.Msg.Contains("此载具为空")) { PlcData.Write_Status = 1004; } @@ -773,8 +746,18 @@ namespace WindowsFormsApp1 PlcData.Write_Status = 1001; } siemensHelp.WriteAddress(plcaddress, PlcData); + LogExecute.logNet.WriteDebug($"上位机写入触发字:{PlcData.Write_Status}"); continue; } + else + { + //出站清空sn + if (nowinterface.code == "SN_Checkout") + { + PlcData.Write_stSN = ""; + } + } + } } else @@ -879,7 +862,8 @@ namespace WindowsFormsApp1 foreach (var item in Init.t_Lineplcs) { - OldPlcTrigger.Add(item.plcid, 0); + if(!OldPlcTrigger.ContainsKey(item.plcid)) + OldPlcTrigger.Add(item.plcid, 0); } #region 初始化全局变量 @@ -968,6 +952,17 @@ namespace WindowsFormsApp1 threadHelp.isrunning1 = true; } Init.threadHelps.Add(threadHelp); + //心跳线程 + ThreadHelp threadHelp1 = new ThreadHelp(); + if (!threadHelp1.isrunning1) + { + threadHelp1.cancellationTokenSource1 = new CancellationTokenSource(); + threadHelp1.Thread = new Thread(() => plcheart(plc, threadHelp1.cancellationTokenSource1.Token)); + threadHelp1.Thread.Name = $"PLC_{plc.id}_heartThread"; // 显式设置线程名称 + threadHelp1.Thread.Start(); + threadHelp1.isrunning1 = true; + } + Init.threadHelps.Add(threadHelp1); } if(Init.t_User.username.ToLower() != "admin") @@ -1006,6 +1001,7 @@ namespace WindowsFormsApp1 private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { + 开始处理.Enabled = false; // 设置窗体正在关闭标志 _isFormClosing = true; @@ -1063,7 +1059,11 @@ namespace WindowsFormsApp1 if (thread.Thread != null && thread.Thread.IsAlive) { thread.cancellationTokenSource1?.Cancel(); - thread.Thread.Join(); // 等待线程完成 + var threadEnded = thread.Thread.Join(3000); // 等待线程完成 + if (!threadEnded) + { + LogExecute.logNet.WriteWarn($"线程 {thread.Thread.Name} 未能在规定时间内结束,将强制继续"); + } } thread.isrunning1 = false; } @@ -1825,5 +1825,43 @@ namespace WindowsFormsApp1 } } } + + //plc心跳 + private void plcheart(t_plc Plc, CancellationToken token) + { + while (!token.IsCancellationRequested) + { + try + { + SiemensHelp siemensHelp = Plc.plchelp; + List plcaddress = Init.t_Plcaddresses.FindAll(a => a.plcid == Plc.id); + + plcdata PlcData = new plcdata(); + + var resdresult = siemensHelp.ReadAddress(plcaddress, PlcData); + if (!resdresult.Success) + { + LogExecute.logNet.WriteError($"{Plc.plcname}:{Plc.plcip}读取全部地址处理错误!"); + } + + //写入心跳 + PlcData.Write_Heartbeat = PlcData.Write_Heartbeat == 0 ? (short)1 : (short)0; + //LogExecute.logNet.WriteDebug($"{Plc.plcname}:{Plc.plcip}写入心跳{PlcData.Write_Heartbeat}!"); + var haertresult = siemensHelp.WriteAddress(plcaddress, PlcData); + if (!haertresult.Success) + { + LogExecute.logNet.WriteError($"{Plc.plcname}:{Plc.plcip}写入心跳错误!"); + } + } + catch (Exception) + { + + } + finally + { + Thread.Sleep(1000); + } + } + } } } diff --git a/WindowsFormsApp1/Program.cs b/WindowsFormsApp1/Program.cs index a8185b3..fd07bec 100644 --- a/WindowsFormsApp1/Program.cs +++ b/WindowsFormsApp1/Program.cs @@ -75,6 +75,7 @@ namespace WindowsFormsApp1 public static string ImageName { get; set; } public static int ImageLocation { get; set; } + public static short ScrewBegin { get; set; } public static int PlcTrigger { get; set; } diff --git a/WindowsFormsApp1/form/ImageDisplayForm.cs b/WindowsFormsApp1/form/ImageDisplayForm.cs index 7db0948..1371d41 100644 --- a/WindowsFormsApp1/form/ImageDisplayForm.cs +++ b/WindowsFormsApp1/form/ImageDisplayForm.cs @@ -252,6 +252,11 @@ namespace WindowsFormsApp1 // 自动更新定时器事件 private void AutoUpdateTimer_Tick(object sender, EventArgs e) { + if(Init.ScrewBegin != 1) + { + this.Close(); + return; + } // 获取当前位置变量 //string newPosition = GetCurrentPosition(); string newPosition = Init.ImageLocation.ToString(); diff --git a/WindowsFormsApp1/server/ApiClient.cs b/WindowsFormsApp1/server/ApiClient.cs index ad9aaa3..23402a6 100644 --- a/WindowsFormsApp1/server/ApiClient.cs +++ b/WindowsFormsApp1/server/ApiClient.cs @@ -225,6 +225,8 @@ namespace WindowsFormsApp1.server } else { + string errorContent = response.Content.ReadAsStringAsync().Result; + LogExecute.logNet.WriteError($"请求失败,请求内容:{jsonPayload},状态码:{response.StatusCode},错误内容:{errorContent}"); throw new Exception($"请求失败,状态码:{response.StatusCode}"); } @@ -249,9 +251,14 @@ namespace WindowsFormsApp1.server // 换盘 老化上线 SN需要调用扫码的值 Init.Sn var sn = ""; var stationid = Init.t_Line.lineremark2.ToLower(); - if ("loadaging,exchange2,exchange1".Contains(stationid)) + + if ("loadaging".Contains(stationid)) { - sn = PlcData.Read_MaterialCode0 ?? OtherData.JS_SN; + sn = OtherData.JS_SN; + } + else if ("exchange2,exchange1".Contains(stationid)) + { + sn = PlcData.Read_MaterialCode0; } else { @@ -267,7 +274,7 @@ namespace WindowsFormsApp1.server OPID = _opID, Token = _token, FixSN = _fixsn, - SN = sn, + SN = sn ?? "", CarrierID = "", SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") }; @@ -293,7 +300,7 @@ namespace WindowsFormsApp1.server OPID = _opID, Token = _token, FixSN = _fixsn, - CarrierID = PlcData.Read_MaterialCode1, + CarrierID = PlcData.Read_MaterialCode1 ?? "", SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") }; @@ -337,7 +344,7 @@ namespace WindowsFormsApp1.server OPID = _opID, Token = _token, FixSN = _fixsn, - SN = OtherData.JS_SN ?? PlcData.Read_MaterialCode2 ?? Init.DZString,//扫码 + SN = OtherData.JS_SN ?? PlcData.Read_MaterialCode2 ?? Init.DZString ?? "",//扫码 SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") }; @@ -389,11 +396,11 @@ namespace WindowsFormsApp1.server new CompListItem() { // 安装主板 端子 - CompID = OtherData.JS_SN ?? Init.DZString, + CompID = OtherData.JS_SN ?? Init.DZString ?? "", Qty = "1" } }, - SN = PlcData.Write_stSN, + SN = PlcData.Write_stSN ?? "", SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") }; @@ -419,7 +426,7 @@ namespace WindowsFormsApp1.server OPID = _opID, Token = _token, FixSN = _fixsn, - SN = PlcData.Write_stSN, + SN = PlcData.Write_stSN ?? "", //CarrierID = PlcData.Read_MaterialCode1, SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") }; @@ -461,9 +468,9 @@ namespace WindowsFormsApp1.server StationID = _stationID, MachineID = _machineID, OPID = OtherData.opindex, - Token = locationindex.ToString(),//判断站,胶水不用传 + Token = locationindex.ToString() ?? "",//判断站,胶水不用传 FixSN = fixsn_1,//判断站,胶水不用传 - SN = OtherData.JS_SN, + SN = OtherData.JS_SN ?? "", SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") }; @@ -777,6 +784,7 @@ namespace WindowsFormsApp1.server var result = PlcData.Read_Result == 1 ? "PASS" : "FAIL"; var logresult = PlcData.Read_Result == 1 ? "OK" : "NG"; + // 基础请求对象 var request = new CheckOutObject() { @@ -789,14 +797,14 @@ namespace WindowsFormsApp1.server { new SNInfoItem() { - SN = PlcData.Write_stSN, + SN = PlcData.Write_stSN ?? "", Result = result, DC_INFO = new List(), CompList = new List() } }, Qty = "", - CarrierID = PlcData.Read_MaterialCode1, + CarrierID = PlcData.Read_MaterialCode1 ?? "", Mold = _mold, Token = _token, FixSN = _fixsn, @@ -1092,6 +1100,14 @@ namespace WindowsFormsApp1.server public ApiFormResult OEEDataCollection(string url, plcdata PlcData, otherdata OtherData) { + //模穴 真空站两个穴位 + var model = 1; + + if (_stationID.ToLower() == "vacuum") + { + model = OtherData.plcid; + } + var request = new { EventID = "OEEDataCollection", @@ -1099,8 +1115,47 @@ namespace WindowsFormsApp1.server StationID = _stationID, MachineID = _machineID, OPID = _opID, + SN = PlcData.Write_stSN, + CarrierID = PlcData.Read_MaterialCode1, + DC_INFO = new List(), + CompList = new List(), SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") }; + request.DC_INFO.Add(new DataDCInfoItem() + { + Item = "Eq_Active_Rate", + Value = "0" + }); + + // 工位最长ct + request.DC_INFO.Add(new DataDCInfoItem() + { + Item = "Cycle_Time", + Value = "0" //跟PLC定义好值从哪里获取 + }); + + // 机台ct + request.DC_INFO.Add(new DataDCInfoItem() + { + Item = "Takt_Time", + Value = "0" //跟PLC定义好值从哪里获取 + }); + + var tossing = DataListManager.DataList.Any(x => x.Name == _stationID); + if (tossing) + { + var materialList = DataListManager.DataList.First(x => x.Name == _stationID).Components; + foreach(var material in materialList) + { + request.DC_INFO.Add( + new DataDCInfoItem() + { + Item = $"Tossing_{material}", + Value = "1" //跟PLC定义好值从哪里获取 + } + ); + } + } return CallApi(url, request, "OEEDataCollection", PlcData, PLC_Status.OEEDataCollection); } @@ -1248,9 +1303,40 @@ namespace WindowsFormsApp1.server public const int Fail = 1001; } + #region oeedata + public class ComponentItem + { + public string Name { get; set; } + public List Components { get; set; } + + public ComponentItem(string name, List components) + { + Name = name; + Components = components; + } + } + + public class DataListManager + { + // 直接声明并赋值 + public static List DataList = new List + { + new ComponentItem("Assy1", new List { "EMC_PCBA" }), + new ComponentItem("ScrewE", new List { "M3*10" }), + new ComponentItem("Assy2", new List { "M4*14", "ST_4*12" }), + new ComponentItem("Assy3", new List { "M3*10" }), + new ComponentItem("ScrewS", new List { "M4*12", "M6*10" }), + new ComponentItem("Assy4", new List { "MAIN_PCBA" }), + new ComponentItem("ScrewM", new List { "M4*12" }), + new ComponentItem("Assy6", new List { "Top_Cover" }), + new ComponentItem("ScrewTC", new List { "M4*12" }) + }; + } + #endregion + #region checkout class - // 根对象类 - public class CheckOutObject + // 根对象类 + public class CheckOutObject { public List SNInfo { get; set; } public string Qty { get; set; }