From 35dd9ea8518824c61d2e502a759270d1bbbce863 Mon Sep 17 00:00:00 2001 From: quowingwang Date: Sat, 24 Jan 2026 10:49:00 +0800 Subject: [PATCH] =?UTF-8?q?PLC=E6=9F=A5=E8=AF=A2=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RIZO.Admin.WebApi/PLC/Service/PlcService.cs | 62 +++++++-------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/RIZO.Admin.WebApi/PLC/Service/PlcService.cs b/RIZO.Admin.WebApi/PLC/Service/PlcService.cs index eb4c0b0..0bb2461 100644 --- a/RIZO.Admin.WebApi/PLC/Service/PlcService.cs +++ b/RIZO.Admin.WebApi/PLC/Service/PlcService.cs @@ -37,39 +37,14 @@ namespace RIZO.Admin.WebApi.PLC.Service private readonly SemaphoreSlim _concurrencySemaphore = new SemaphoreSlim(15, 50); // 限制20并发(适配50台PLC) private readonly ConcurrentDictionary _plcConnPool = new(); // 连接池 - // PLC地址映射(严格匹配业务地址清单) - private readonly Dictionary _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 _plcIntMap = new() - { - { "QualificationFlag", "DB1010.DBW224" }, - { "ReworkFlag", "DB1010.DBW226" }, - { "ProductionCycle", "DB1010.DBW228" }, - { "AutoManual", "DB1010.DBW230" }, - { "RunStatus", "DB1010.DBW232" }, - { "QueryRequest", "DB1001.DBW2001" } - }; - #region PLC地址块儿映射 // OP070-1 专属地址映射 private readonly Dictionary _op070_1StringMap = new() { { "报警信息", ("DB1001.DBB58", 48) }, // Array[1..48] of Byte - { "产品型号_48", ("DB1001.DBB1000", 48) }, // String[48] + //{ "产品型号", ("DB1001.DBB1000", 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_2", ("DB1001.DBB2134", 28) } // String[28] }; @@ -82,9 +57,9 @@ namespace RIZO.Admin.WebApi.PLC.Service { "ByPass", "DB1001.DBW6" }, // Int { "生产模式", "DB1001.DBW8" }, // Int { "实际产量", "DB1001.DBD1104" }, // DInt - { "合格数量", "DB1001.DBD1108" }, // DInt - { "失败数量", "DB1001.DBD1112" }, // DInt - { "查询请求", "DB1001.DBW2000" }, // Int + //{ "合格数量", "DB1001.DBD1108" }, // DInt + //{ "失败数量", "DB1001.DBD1112" }, // DInt + //{ "查询请求", "DB1001.DBW2000" }, // Int { "保存请求", "DB1001.DBW2002" }, // Int { "托盘号", "DB1001.DBW2004" }, // Int { "相机结果", "DB1001.DBW2164" }, // Int @@ -92,6 +67,9 @@ namespace RIZO.Admin.WebApi.PLC.Service { "节拍时间", "DB1001.DBD2168" } // Real }; + // OP080-1 专属地址映射 + + #endregion /// /// 构造函数(依赖注入获取PLC配置) @@ -493,13 +471,13 @@ namespace RIZO.Admin.WebApi.PLC.Service private async Task ReadOP070_1DataAsync(Plc plc, string ip,string workstationCode) { // 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["产品型号_48"].Addr, _op070_1StringMap["产品型号_48"].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["产品型号"].Addr, _op070_1StringMap["产品型号"].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) ), @@ -517,11 +495,11 @@ namespace RIZO.Admin.WebApi.PLC.Service await ReadPlcIntAsync(plc, _op070_1IntMap["站位结果"]) ), // 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字段(增强版读取方法) ( await ReadPlcRealAsync(plc, _op070_1IntMap["节拍时间"]) @@ -529,9 +507,9 @@ namespace RIZO.Admin.WebApi.PLC.Service )); // 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 (actualOutput, qualifiedQty, failedQty) = dintFields; + //var (actualOutput, qualifiedQty, failedQty) = dintFields; float cycleTime = realFields; // 3. 写入保存请求(异步+增强异常日志) @@ -556,7 +534,7 @@ namespace RIZO.Admin.WebApi.PLC.Service 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. 构建数据实体(增强空值处理) return new PlcProductionData @@ -565,7 +543,7 @@ namespace RIZO.Admin.WebApi.PLC.Service OccurTime = DateTime.Now, LineCode = "line2", WorkstationCode = workstationCode, - ProductModel = productModel28 ?? productModel48 ?? string.Empty, + ProductModel = productModel ?? string.Empty, ProductName = productName ?? string.Empty, ProductCode = sn2 ?? string.Empty, SN1 = sn1 ?? string.Empty,