PLC查询变更

This commit is contained in:
quowingwang 2026-01-24 10:49:00 +08:00
parent ad2f0021e6
commit 35dd9ea851

View File

@ -37,39 +37,14 @@ namespace RIZO.Admin.WebApi.PLC.Service
private readonly SemaphoreSlim _concurrencySemaphore = new SemaphoreSlim(15, 50); // 限制20并发适配50台PLC private readonly SemaphoreSlim _concurrencySemaphore = new SemaphoreSlim(15, 50); // 限制20并发适配50台PLC
private readonly ConcurrentDictionary<string, (Plc Client, DateTime LastUsedTime)> _plcConnPool = new(); // 连接池 private readonly ConcurrentDictionary<string, (Plc Client, DateTime LastUsedTime)> _plcConnPool = new(); // 连接池
// PLC地址映射严格匹配业务地址清单
private readonly Dictionary<string, (string Addr, int Len)> _plcStringMap = new()
{
{ "LineCode", ("DB1010.DBB50", 14) },
{ "IpStation", ("DB1010.DBB64", 16) },
{ "ProductCode", ("DB1010.DBB80", 16) },
{ "ProductName", ("DB1010.DBB94", 50) },
{ "PartCode", ("DB1010.DBB144", 16) },
{ "PartName", ("DB1010.DBB158", 28) },
{ "ProcessName", ("DB1010.DBB186", 12) },
{ "ParamName", ("DB1010.DBB198", 12) },
{ "ParamValue", ("DB1010.DBB210", 14) },
{ "Static16", ("DB1010.DBB238", 68) }
};
private readonly Dictionary<string, string> _plcIntMap = new()
{
{ "QualificationFlag", "DB1010.DBW224" },
{ "ReworkFlag", "DB1010.DBW226" },
{ "ProductionCycle", "DB1010.DBW228" },
{ "AutoManual", "DB1010.DBW230" },
{ "RunStatus", "DB1010.DBW232" },
{ "QueryRequest", "DB1001.DBW2001" }
};
#region PLC地址块儿映射 #region PLC地址块儿映射
// OP070-1 专属地址映射 // OP070-1 专属地址映射
private readonly Dictionary<string, (string Addr, int Len)> _op070_1StringMap = new() private readonly Dictionary<string, (string Addr, int Len)> _op070_1StringMap = new()
{ {
{ "报警信息", ("DB1001.DBB58", 48) }, // Array[1..48] of Byte { "报警信息", ("DB1001.DBB58", 48) }, // Array[1..48] of Byte
{ "产品型号_48", ("DB1001.DBB1000", 48) }, // String[48] //{ "产品型号", ("DB1001.DBB1000", 48) }, // String[48]
{ "产品名称", ("DB1001.DBB1054", 48) }, // String[48] { "产品名称", ("DB1001.DBB1054", 48) }, // String[48]
{ "产品型号_28", ("DB1001.DBB2006", 28) }, // String[28] { "产品型号", ("DB1001.DBB2006", 28) }, // String[28]
{ "SN_1", ("DB1001.DBB2100", 28) }, // String[28] { "SN_1", ("DB1001.DBB2100", 28) }, // String[28]
{ "SN_2", ("DB1001.DBB2134", 28) } // String[28] { "SN_2", ("DB1001.DBB2134", 28) } // String[28]
}; };
@ -82,9 +57,9 @@ namespace RIZO.Admin.WebApi.PLC.Service
{ "ByPass", "DB1001.DBW6" }, // Int { "ByPass", "DB1001.DBW6" }, // Int
{ "生产模式", "DB1001.DBW8" }, // Int { "生产模式", "DB1001.DBW8" }, // Int
{ "实际产量", "DB1001.DBD1104" }, // DInt { "实际产量", "DB1001.DBD1104" }, // DInt
{ "合格数量", "DB1001.DBD1108" }, // DInt //{ "合格数量", "DB1001.DBD1108" }, // DInt
{ "失败数量", "DB1001.DBD1112" }, // DInt //{ "失败数量", "DB1001.DBD1112" }, // DInt
{ "查询请求", "DB1001.DBW2000" }, // Int //{ "查询请求", "DB1001.DBW2000" }, // Int
{ "保存请求", "DB1001.DBW2002" }, // Int { "保存请求", "DB1001.DBW2002" }, // Int
{ "托盘号", "DB1001.DBW2004" }, // Int { "托盘号", "DB1001.DBW2004" }, // Int
{ "相机结果", "DB1001.DBW2164" }, // Int { "相机结果", "DB1001.DBW2164" }, // Int
@ -92,6 +67,9 @@ namespace RIZO.Admin.WebApi.PLC.Service
{ "节拍时间", "DB1001.DBD2168" } // Real { "节拍时间", "DB1001.DBD2168" } // Real
}; };
// OP080-1 专属地址映射
#endregion #endregion
/// <summary> /// <summary>
/// 构造函数依赖注入获取PLC配置 /// 构造函数依赖注入获取PLC配置
@ -493,13 +471,13 @@ namespace RIZO.Admin.WebApi.PLC.Service
private async Task<PlcProductionData> ReadOP070_1DataAsync(Plc plc, string ip,string workstationCode) private async Task<PlcProductionData> ReadOP070_1DataAsync(Plc plc, string ip,string workstationCode)
{ {
// 1. 批量并行读取所有字段(沿用原有结构+增强型读取方法) // 1. 批量并行读取所有字段(沿用原有结构+增强型读取方法)
var (strFields, intFields, dintFields, realFields) = await Task.Run(async () => ( var (strFields, intFields, realFields) = await Task.Run(async () => (
// 字符串字段(增强空值和异常处理) // 字符串字段(增强空值和异常处理)
( (
await ReadPlcStringAsync(plc, _op070_1StringMap["报警信息"].Addr, _op070_1StringMap["报警信息"].Len), await ReadPlcStringAsync(plc, _op070_1StringMap["报警信息"].Addr, _op070_1StringMap["报警信息"].Len),
await ReadPlcStringAsync(plc, _op070_1StringMap["产品名称"].Addr, _op070_1StringMap["产品名称"].Len), await ReadPlcStringAsync(plc, _op070_1StringMap["产品名称"].Addr, _op070_1StringMap["产品名称"].Len),
await ReadPlcStringAsync(plc, _op070_1StringMap["产品型号_48"].Addr, _op070_1StringMap["产品型号_48"].Len), //await ReadPlcStringAsync(plc, _op070_1StringMap["产品型号"].Addr, _op070_1StringMap["产品型号"].Len),
await ReadPlcStringAsync(plc, _op070_1StringMap["产品型号_28"].Addr, _op070_1StringMap["产品型号_28"].Len), await ReadPlcStringAsync(plc, _op070_1StringMap["产品型号"].Addr, _op070_1StringMap["产品型号"].Len),
await ReadPlcStringAsync(plc, _op070_1StringMap["SN_1"].Addr, _op070_1StringMap["SN_1"].Len), await ReadPlcStringAsync(plc, _op070_1StringMap["SN_1"].Addr, _op070_1StringMap["SN_1"].Len),
await ReadPlcStringAsync(plc, _op070_1StringMap["SN_2"].Addr, _op070_1StringMap["SN_2"].Len) await ReadPlcStringAsync(plc, _op070_1StringMap["SN_2"].Addr, _op070_1StringMap["SN_2"].Len)
), ),
@ -517,11 +495,11 @@ namespace RIZO.Admin.WebApi.PLC.Service
await ReadPlcIntAsync(plc, _op070_1IntMap["站位结果"]) await ReadPlcIntAsync(plc, _op070_1IntMap["站位结果"])
), ),
// DInt字段增强版读取方法 // DInt字段增强版读取方法
( //(
await ReadPlcDIntAsync(plc, _op070_1IntMap["实际产量"]), // await ReadPlcDIntAsync(plc, _op070_1IntMap["实际产量"]),
await ReadPlcDIntAsync(plc, _op070_1IntMap["合格数量"]), // await ReadPlcDIntAsync(plc, _op070_1IntMap["合格数量"]),
await ReadPlcDIntAsync(plc, _op070_1IntMap["失败数量"]) // await ReadPlcDIntAsync(plc, _op070_1IntMap["失败数量"])
), //),
// Real字段增强版读取方法 // Real字段增强版读取方法
( (
await ReadPlcRealAsync(plc, _op070_1IntMap["节拍时间"]) await ReadPlcRealAsync(plc, _op070_1IntMap["节拍时间"])
@ -529,9 +507,9 @@ namespace RIZO.Admin.WebApi.PLC.Service
)); ));
// 2. 解构字段(保持原有逻辑,空值兜底) // 2. 解构字段(保持原有逻辑,空值兜底)
var (alarmMsg, productName, productModel48, productModel28, sn1, sn2) = strFields; var (alarmMsg, productName, productModel, sn1, sn2) = strFields;
var (runStatus, machineModel, onlineStatus, byPass, produceModel, queryReq, saveReq, trayNo, cameraResult, stationResult) = intFields; var (runStatus, machineModel, onlineStatus, byPass, produceModel, queryReq, saveReq, trayNo, cameraResult, stationResult) = intFields;
var (actualOutput, qualifiedQty, failedQty) = dintFields; //var (actualOutput, qualifiedQty, failedQty) = dintFields;
float cycleTime = realFields; float cycleTime = realFields;
// 3. 写入保存请求(异步+增强异常日志) // 3. 写入保存请求(异步+增强异常日志)
@ -556,7 +534,7 @@ namespace RIZO.Admin.WebApi.PLC.Service
string cameraResultDesc = cameraResult switch { 1 => "OK", 2 => "NG", _ => $"未知({cameraResult})" }; string cameraResultDesc = cameraResult switch { 1 => "OK", 2 => "NG", _ => $"未知({cameraResult})" };
// 调试日志:输出关键读取结果 // 调试日志:输出关键读取结果
Console.WriteLine($"OP070-1({ip})读取结果:产品型号={productModel28 ?? productModel48},运行状态={runStatusDesc},产量={actualOutput}"); Console.WriteLine($"OP070-1({ip})读取结果:产品型号={productModel},运行状态={runStatusDesc}");
// 5. 构建数据实体(增强空值处理) // 5. 构建数据实体(增强空值处理)
return new PlcProductionData return new PlcProductionData
@ -565,7 +543,7 @@ namespace RIZO.Admin.WebApi.PLC.Service
OccurTime = DateTime.Now, OccurTime = DateTime.Now,
LineCode = "line2", LineCode = "line2",
WorkstationCode = workstationCode, WorkstationCode = workstationCode,
ProductModel = productModel28 ?? productModel48 ?? string.Empty, ProductModel = productModel ?? string.Empty,
ProductName = productName ?? string.Empty, ProductName = productName ?? string.Empty,
ProductCode = sn2 ?? string.Empty, ProductCode = sn2 ?? string.Empty,
SN1 = sn1 ?? string.Empty, SN1 = sn1 ?? string.Empty,