添加接口null值检查

打开螺丝引导页面逻辑修改
程序重复打开命名修改
添加心跳线程
This commit is contained in:
gf zhou committed 2025-04-16 11:31:16 +08:00
1 parent 1a9f2a5bca
commit 233737bc31
4 files changed
+186 -56

No files matched your search

+80 -42
View File
@@ -77,7 +77,7 @@ namespace WindowsFormsApp1
{ {
bool createdNew; 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; return createdNew;
} }
@@ -182,7 +182,8 @@ namespace WindowsFormsApp1
continue; continue;
} }
//写入心跳 /*
* //写入心跳 在主线程取消,单独开线程 写plc心跳
PlcData.Write_Heartbeat = PlcData.Write_Heartbeat == 0 ? (short)1 : (short)0; PlcData.Write_Heartbeat = PlcData.Write_Heartbeat == 0 ? (short)1 : (short)0;
var haertresult = siemensHelp.WriteAddress(plcaddress, PlcData); var haertresult = siemensHelp.WriteAddress(plcaddress, PlcData);
if (!haertresult.Success) if (!haertresult.Success)
@@ -191,7 +192,7 @@ namespace WindowsFormsApp1
//PLC链接状态.BackColor = Color.Red; //PLC链接状态.BackColor = Color.Red;
LogExecute.logNet.WriteError($"{Plc.plcname}:{Plc.plcip}写入心跳错误:处理停止"); LogExecute.logNet.WriteError($"{Plc.plcname}:{Plc.plcip}写入心跳错误:处理停止");
continue; continue;
} }*/
if (Init.t_Lineplcs.Count >= 2 && Plc.id == Init.t_Lineplcs.First().plcid) if (Init.t_Lineplcs.Count >= 2 && Plc.id == Init.t_Lineplcs.First().plcid)
@@ -231,6 +232,7 @@ namespace WindowsFormsApp1
材料值.Text = PlcData.Read_MaterialCode2; 材料值.Text = PlcData.Read_MaterialCode2;
Init.ImageLocation = PlcData.Read_Other4; Init.ImageLocation = PlcData.Read_Other4;
Init.ImageName = PlcData.Read_Other3.ToString(); Init.ImageName = PlcData.Read_Other3.ToString();
Init.ScrewBegin = PlcData.Read_Other2;
#region 螺丝引导相关 #region 螺丝引导相关
// 螺丝 OK/NG // 螺丝 OK/NG
@@ -268,34 +270,10 @@ namespace WindowsFormsApp1
{ {
if (!imageFormIsOpened) if (!imageFormIsOpened)
{ {
ThreadHelp threadHelp = new ThreadHelp(); this.Invoke(new Action(() => {
if (!threadHelp.isrunning1) ImageDisplayForm imageDisplayForm = new ImageDisplayForm();
{ imageDisplayForm.Show();
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);
}
} }
} }
#endregion 螺丝引导相关 #endregion 螺丝引导相关
@@ -471,6 +449,7 @@ namespace WindowsFormsApp1
if (nowflow.dzscan) if (nowflow.dzscan)
{ {
//扫码 //扫码
ScanForm scanForm = new ScanForm(); ScanForm scanForm = new ScanForm();
DialogResult scan_result = scanForm.ShowDialog(); DialogResult scan_result = scanForm.ShowDialog();
if (scan_result == DialogResult.OK) if (scan_result == DialogResult.OK)
@@ -743,11 +722,7 @@ namespace WindowsFormsApp1
{ {
ChangeBarcode(); ChangeBarcode();
MES_MsgfORM(nowinterface.code, result, result.Requesttime); MES_MsgfORM(nowinterface.code, result, result.Requesttime);
//出站清空sn
if(nowinterface.code == "SN_Checkout")
{
PlcData.Write_stSN = "";
}
var writeresult = siemensHelp.WriteAddress(plcaddress, PlcData); var writeresult = siemensHelp.WriteAddress(plcaddress, PlcData);
if (!writeresult.Success) if (!writeresult.Success)
{ {
@@ -756,15 +731,13 @@ namespace WindowsFormsApp1
} }
else else
{ {
LogExecute.logNet.WriteDebug($"上位机写入触发字:{PlcData.Write_Status}");
if (result.Mesresult != mesresult.PASS) if (result.Mesresult != mesresult.PASS)
{ {
if (resdresult.Msg.Contains("机台上料数量不够扣料")) if (result.Msg.Contains("机台上料数量不够扣料"))
{ {
PlcData.Write_Status = 1003; PlcData.Write_Status = 1003;
} }
else if (resdresult.Msg.Contains("此载具为空")) else if (result.Msg.Contains("此载具为空"))
{ {
PlcData.Write_Status = 1004; PlcData.Write_Status = 1004;
} }
@@ -773,8 +746,18 @@ namespace WindowsFormsApp1
PlcData.Write_Status = 1001; PlcData.Write_Status = 1001;
} }
siemensHelp.WriteAddress(plcaddress, PlcData); siemensHelp.WriteAddress(plcaddress, PlcData);
LogExecute.logNet.WriteDebug($"上位机写入触发字:{PlcData.Write_Status}");
continue; continue;
} }
else
{
//出站清空sn
if (nowinterface.code == "SN_Checkout")
{
PlcData.Write_stSN = "";
}
}
} }
} }
else else
@@ -879,7 +862,8 @@ namespace WindowsFormsApp1
foreach (var item in Init.t_Lineplcs) foreach (var item in Init.t_Lineplcs)
{ {
OldPlcTrigger.Add(item.plcid, 0); if(!OldPlcTrigger.ContainsKey(item.plcid))
OldPlcTrigger.Add(item.plcid, 0);
} }
#region 初始化全局变量 #region 初始化全局变量
@@ -968,6 +952,17 @@ namespace WindowsFormsApp1
threadHelp.isrunning1 = true; threadHelp.isrunning1 = true;
} }
Init.threadHelps.Add(threadHelp); 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") if(Init.t_User.username.ToLower() != "admin")
@@ -1006,6 +1001,7 @@ namespace WindowsFormsApp1
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{ {
开始处理.Enabled = false;
// 设置窗体正在关闭标志 // 设置窗体正在关闭标志
_isFormClosing = true; _isFormClosing = true;
@@ -1063,7 +1059,11 @@ namespace WindowsFormsApp1
if (thread.Thread != null && thread.Thread.IsAlive) if (thread.Thread != null && thread.Thread.IsAlive)
{ {
thread.cancellationTokenSource1?.Cancel(); thread.cancellationTokenSource1?.Cancel();
thread.Thread.Join(); // 等待线程完成 var threadEnded = thread.Thread.Join(3000); // 等待线程完成
if (!threadEnded)
{
LogExecute.logNet.WriteWarn($"线程 {thread.Thread.Name} 未能在规定时间内结束,将强制继续");
}
} }
thread.isrunning1 = false; 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<t_plcaddress> 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);
}
}
}
} }
} }
+1
View File
@@ -75,6 +75,7 @@ namespace WindowsFormsApp1
public static string ImageName { get; set; } public static string ImageName { get; set; }
public static int ImageLocation { get; set; } public static int ImageLocation { get; set; }
public static short ScrewBegin { get; set; }
public static int PlcTrigger { get; set; } public static int PlcTrigger { get; set; }
@@ -252,6 +252,11 @@ namespace WindowsFormsApp1
// 自动更新定时器事件 // 自动更新定时器事件
private void AutoUpdateTimer_Tick(object sender, EventArgs e) private void AutoUpdateTimer_Tick(object sender, EventArgs e)
{ {
if(Init.ScrewBegin != 1)
{
this.Close();
return;
}
// 获取当前位置变量 // 获取当前位置变量
//string newPosition = GetCurrentPosition(); //string newPosition = GetCurrentPosition();
string newPosition = Init.ImageLocation.ToString(); string newPosition = Init.ImageLocation.ToString();
+100 -14
View File
@@ -225,6 +225,8 @@ namespace WindowsFormsApp1.server
} }
else else
{ {
string errorContent = response.Content.ReadAsStringAsync().Result;
LogExecute.logNet.WriteError($"请求失败,请求内容:{jsonPayload},状态码:{response.StatusCode},错误内容:{errorContent}");
throw new Exception($"请求失败,状态码:{response.StatusCode}"); throw new Exception($"请求失败,状态码:{response.StatusCode}");
} }
@@ -249,9 +251,14 @@ namespace WindowsFormsApp1.server
// 换盘 老化上线 SN需要调用扫码的值 Init.Sn // 换盘 老化上线 SN需要调用扫码的值 Init.Sn
var sn = ""; var sn = "";
var stationid = Init.t_Line.lineremark2.ToLower(); 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 else
{ {
@@ -267,7 +274,7 @@ namespace WindowsFormsApp1.server
OPID = _opID, OPID = _opID,
Token = _token, Token = _token,
FixSN = _fixsn, FixSN = _fixsn,
SN = sn, SN = sn ?? "",
CarrierID = "", CarrierID = "",
SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
@@ -293,7 +300,7 @@ namespace WindowsFormsApp1.server
OPID = _opID, OPID = _opID,
Token = _token, Token = _token,
FixSN = _fixsn, FixSN = _fixsn,
CarrierID = PlcData.Read_MaterialCode1, CarrierID = PlcData.Read_MaterialCode1 ?? "",
SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
@@ -337,7 +344,7 @@ namespace WindowsFormsApp1.server
OPID = _opID, OPID = _opID,
Token = _token, Token = _token,
FixSN = _fixsn, 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") SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
@@ -389,11 +396,11 @@ namespace WindowsFormsApp1.server
new CompListItem() new CompListItem()
{ {
// 安装主板 端子 // 安装主板 端子
CompID = OtherData.JS_SN ?? Init.DZString, CompID = OtherData.JS_SN ?? Init.DZString ?? "",
Qty = "1" Qty = "1"
} }
}, },
SN = PlcData.Write_stSN, SN = PlcData.Write_stSN ?? "",
SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
@@ -419,7 +426,7 @@ namespace WindowsFormsApp1.server
OPID = _opID, OPID = _opID,
Token = _token, Token = _token,
FixSN = _fixsn, FixSN = _fixsn,
SN = PlcData.Write_stSN, SN = PlcData.Write_stSN ?? "",
//CarrierID = PlcData.Read_MaterialCode1, //CarrierID = PlcData.Read_MaterialCode1,
SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
}; };
@@ -461,9 +468,9 @@ namespace WindowsFormsApp1.server
StationID = _stationID, StationID = _stationID,
MachineID = _machineID, MachineID = _machineID,
OPID = OtherData.opindex, OPID = OtherData.opindex,
Token = locationindex.ToString(),//判断站,胶水不用传 Token = locationindex.ToString() ?? "",//判断站,胶水不用传
FixSN = fixsn_1,//判断站,胶水不用传 FixSN = fixsn_1,//判断站,胶水不用传
SN = OtherData.JS_SN, SN = OtherData.JS_SN ?? "",
SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") 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 result = PlcData.Read_Result == 1 ? "PASS" : "FAIL";
var logresult = PlcData.Read_Result == 1 ? "OK" : "NG"; var logresult = PlcData.Read_Result == 1 ? "OK" : "NG";
// 基础请求对象 // 基础请求对象
var request = new CheckOutObject() var request = new CheckOutObject()
{ {
@@ -789,14 +797,14 @@ namespace WindowsFormsApp1.server
{ {
new SNInfoItem() new SNInfoItem()
{ {
SN = PlcData.Write_stSN, SN = PlcData.Write_stSN ?? "",
Result = result, Result = result,
DC_INFO = new List<DCInfoItem>(), DC_INFO = new List<DCInfoItem>(),
CompList = new List<CompListItem>() CompList = new List<CompListItem>()
} }
}, },
Qty = "", Qty = "",
CarrierID = PlcData.Read_MaterialCode1, CarrierID = PlcData.Read_MaterialCode1 ?? "",
Mold = _mold, Mold = _mold,
Token = _token, Token = _token,
FixSN = _fixsn, FixSN = _fixsn,
@@ -1092,6 +1100,14 @@ namespace WindowsFormsApp1.server
public ApiFormResult OEEDataCollection(string url, plcdata PlcData, otherdata OtherData) public ApiFormResult OEEDataCollection(string url, plcdata PlcData, otherdata OtherData)
{ {
//模穴 真空站两个穴位
var model = 1;
if (_stationID.ToLower() == "vacuum")
{
model = OtherData.plcid;
}
var request = new var request = new
{ {
EventID = "OEEDataCollection", EventID = "OEEDataCollection",
@@ -1099,8 +1115,47 @@ namespace WindowsFormsApp1.server
StationID = _stationID, StationID = _stationID,
MachineID = _machineID, MachineID = _machineID,
OPID = _opID, OPID = _opID,
SN = PlcData.Write_stSN,
CarrierID = PlcData.Read_MaterialCode1,
DC_INFO = new List<DataDCInfoItem>(),
CompList = new List<CompListItem>(),
SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") 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); return CallApi(url, request, "OEEDataCollection", PlcData, PLC_Status.OEEDataCollection);
} }
@@ -1248,9 +1303,40 @@ namespace WindowsFormsApp1.server
public const int Fail = 1001; public const int Fail = 1001;
} }
#region oeedata
public class ComponentItem
{
public string Name { get; set; }
public List<string> Components { get; set; }
public ComponentItem(string name, List<string> components)
{
Name = name;
Components = components;
}
}
public class DataListManager
{
// 直接声明并赋值
public static List<ComponentItem> DataList = new List<ComponentItem>
{
new ComponentItem("Assy1", new List<string> { "EMC_PCBA" }),
new ComponentItem("ScrewE", new List<string> { "M3*10" }),
new ComponentItem("Assy2", new List<string> { "M4*14", "ST_4*12" }),
new ComponentItem("Assy3", new List<string> { "M3*10" }),
new ComponentItem("ScrewS", new List<string> { "M4*12", "M6*10" }),
new ComponentItem("Assy4", new List<string> { "MAIN_PCBA" }),
new ComponentItem("ScrewM", new List<string> { "M4*12" }),
new ComponentItem("Assy6", new List<string> { "Top_Cover" }),
new ComponentItem("ScrewTC", new List<string> { "M4*12" })
};
}
#endregion
#region checkout class #region checkout class
// 根对象类 // 根对象类
public class CheckOutObject public class CheckOutObject
{ {
public List<SNInfoItem> SNInfo { get; set; } public List<SNInfoItem> SNInfo { get; set; }
public string Qty { get; set; } public string Qty { get; set; }