采集PLC数据保存后返回保存结果

This commit is contained in:
quowingwang 2026-01-25 21:59:16 +08:00
parent 603eeda7df
commit 9e9350a440

View File

@ -38,6 +38,11 @@ namespace RIZO.Admin.WebApi.PLC.Service
private readonly ConcurrentDictionary<string, (Plc Client, DateTime LastUsedTime)> _plcConnPool = new(); // 连接池
#region PLC地址块儿映射
//保存请求返回结果
private readonly Dictionary<string, string> _saveRequestReturnMap = new()
{
{ "保存请求返回", "DB1050.DBW2002" }, // Int
};
// OP020-2 专属地址映射合盖工位DB1001
private readonly Dictionary<string, (string Addr, int Len)> _op020_2StringMap = new()
@ -139,7 +144,7 @@ namespace RIZO.Admin.WebApi.PLC.Service
{ "托盘号", "DB1011.DBW2004" }, // Int
{ "相机结果", "DB1011.DBW2164" }, // Int
{ "站位结果", "DB1011.DBW2166" }, // Int
{ "节拍时间", "DB1011.DBD2168" } // Real
{ "节拍时间", "DB1011.DBD2168" }, // Real
};
// OP075 专属地址映射 PWM折弯&装配
@ -519,6 +524,8 @@ namespace RIZO.Admin.WebApi.PLC.Service
{
if (t.IsFaulted)
{
// 保存失败时写入返回值"6"
WritePlcSaveRequestResult(plc, ip, plcName, "6");
Console.WriteLine($"{plcName}({ip})数据保存失败:{t.Exception?.InnerException?.Message}");
}
});
@ -529,6 +536,8 @@ namespace RIZO.Admin.WebApi.PLC.Service
var successMsg = isConnReused
? $"{plcName}生产数据读取成功(复用连接)"
: $"{plcName}生产数据读取成功(新建连接)";
//9.写入保存请求返回结果,保存成功时写入返回值"1"
WritePlcSaveRequestResult(plc, ip, plcName, "1");
return (true, prodData, successMsg);
}
catch (Exception ex)
@ -2013,5 +2022,19 @@ namespace RIZO.Admin.WebApi.PLC.Service
return defaultResult;
}
}
// 提取写PLC返回值的通用方法统一处理逻辑和日志
private void WritePlcSaveRequestResult(Plc plc, string ip, string plcName, string returnValue)
{
try
{
WritePlcValue(plc, _saveRequestReturnMap["保存请求返回"], returnValue);
}
catch (Exception ex)
{
// 标准化日志格式,包含更多上下文信息
Console.WriteLine($"[{DateTime.Now}] OP070-1({ip}) {plcName} 写保存请求返回值失败[目标值:{returnValue}]{ex.Message}");
}
}
}
}