diff --git a/RIZO.Admin.WebApi/PLC/Service/PlcService.cs b/RIZO.Admin.WebApi/PLC/Service/PlcService.cs index 17e7ead..f2cd783 100644 --- a/RIZO.Admin.WebApi/PLC/Service/PlcService.cs +++ b/RIZO.Admin.WebApi/PLC/Service/PlcService.cs @@ -38,6 +38,33 @@ namespace RIZO.Admin.WebApi.PLC.Service private readonly ConcurrentDictionary _plcConnPool = new(); // 连接池 #region PLC地址块儿映射 + + // OP020-2 专属地址映射(合盖工位,DB1001) + private readonly Dictionary _op020_2StringMap = new() + { + { "报警信息", ("DB1001.DBB58", 48) }, // Array[1..48] of Byte + { "产品型号", ("DB1001.DBB1000", 48) }, // String[48] + { "产品名称", ("DB1001.DBB1054", 48) }, // String[48] + { "SN", ("DB1001.DBB2106", 48) } // String[48] + }; + + private readonly Dictionary _op020_2IntMap = new() + { + { "运行状态", "DB1001.DBW0" }, // Int - 1=空闲,2=运行中,3=故障 + { "设备模式", "DB1001.DBW2" }, // Int - 1=空模式;2=手动;4=初始化;8=自动;16=CycleStop + { "设备在线状态", "DB1001.DBW4" }, // Int - 1=离线,0=在线 + //{ "ByPass", "DB1001.DBW6" }, // Int - 1=ByPass,0=正常模式 + { "生产模式", "DB1001.DBW8" }, // Int - 1=正常模式;2=清线模式;4=返工模式;8=换型模式;16=预热模式 + + //{ "实际产量", "DB1001.DBD1104" }, // DInt + //{ "合格数量", "DB1001.DBD1108" }, // DInt + //{ "失败数量", "DB1001.DBD1112" }, // DInt + + { "上传请求", "DB1001.DBW2002" }, // Int - 1=请求开始,0=无请求 + { "托盘号", "DB1001.DBW2054" }, // Int + { "总结果", "DB1001.DBW2158" }, // Int - 一个托盘上传四次结果 + //{ "节拍时间", "DB1001.DBD2988" } // Real + }; // OP070-1 专属地址映射 private readonly Dictionary _op070_1StringMap = new() { @@ -333,7 +360,12 @@ namespace RIZO.Admin.WebApi.PLC.Service int iQueryRequest = 0; int iSaveRequest = 0; string strSaveRequest = ""; - if (plcName == "OP070-1" || plcName == "OP070-2" || plcName == "OP070-3") + if (plcName == "OP020-2") + { + //查询请求??20260125 PM + iSaveRequest = await ReadPlcIntAsync(plc, _op020_2IntMap["上传请求"]); + } + else if (plcName == "OP070-1" || plcName == "OP070-2" || plcName == "OP070-3") { iQueryRequest = await ReadPlcIntAsync(plc, _op070_1IntMap["查询请求"]); iSaveRequest = await ReadPlcIntAsync(plc, _op070_1IntMap["保存请求"]); @@ -381,7 +413,15 @@ namespace RIZO.Admin.WebApi.PLC.Service if (iSaveRequest == 1) { // 5. 多工位专属数据读取(预留扩展,逻辑隔离) - if (plcName == "OP070-1" || plcName == "OP070-2" || plcName == "OP070-3") + if (plcName == "OP020-2") + { + prodData = await ReadOP020_2DataAsync(plc, ip, plcName); + } + if (plcName == "OP020-3") + { + prodData = await ReadOP020_2DataAsync(plc, ip, plcName); + } + else if (plcName == "OP070-1" || plcName == "OP070-2" || plcName == "OP070-3") { prodData = await ReadOP070_1DataAsync(plc, ip, plcName); } @@ -443,6 +483,93 @@ namespace RIZO.Admin.WebApi.PLC.Service #region 工位专属读取方法 /// + /// 读取OP020-2数据(合盖工位) + /// + private async Task ReadOP020_2DataAsync(Plc plc, string ip, string workstationCode) + { + try + { + // 1. 批量并行读取所有字段 + var (strFields, intFields) = await Task.Run(async () => ( + // 字符串字段 + ( + await ReadPlcStringAsync(plc, _op020_2StringMap["产品型号"].Addr, _op020_2StringMap["产品型号"].Len), + await ReadPlcStringAsync(plc, _op020_2StringMap["产品名称"].Addr, _op020_2StringMap["产品名称"].Len), + await ReadPlcStringAsync(plc, _op020_2StringMap["SN"].Addr, _op020_2StringMap["SN"].Len) + ), + // Int字段 + ( + await ReadPlcIntAsync(plc, _op020_2IntMap["运行状态"]), + await ReadPlcIntAsync(plc, _op020_2IntMap["设备模式"]), + await ReadPlcIntAsync(plc, _op020_2IntMap["设备在线状态"]), + await ReadPlcIntAsync(plc, _op020_2IntMap["生产模式"]), + await ReadPlcIntAsync(plc, _op020_2IntMap["托盘号"]), + await ReadPlcIntAsync(plc, _op020_2IntMap["总结果"]) + ) + )); + + // 2. 解构字段 + var (productModel, productName, sn) = strFields; + var (runStatus, machineModel, onlineStatus, produceModel,trayNo, totalResult) = intFields; + + // 3. 复位上传请求 + //try + //{ + // WritePlcValue(plc, _op020_2IntMap["上传请求"], "0"); + //} + //catch (Exception ex) + //{ + // Console.WriteLine($"OP020-2({ip})写上传请求失败:{ex.Message}"); + //} + + // 4. 业务逻辑计算 + var reworkFlag = produceModel == 4 ? "1" : "0"; + string produceModelDesc = produceModel switch + { + 1 => "正常模式", + 2 => "清线模式", + 4 => "返工模式", + 8 => "换型模式", + 16 => "预热模式", + _ => $"未知({produceModel})" + }; + string runStatusDesc = runStatus switch { 1 => "空闲", 2 => "运行中", 3 => "故障", _ => $"未知({runStatus})" }; + string onlineStatusDesc = onlineStatus == 1 ? "离线" : "在线"; + string qualificationFlag = totalResult switch { 1 => "1", 2 => "0", _ => totalResult.ToString() }; + + // 调试日志 + Console.WriteLine($"OP020-2({ip})读取结果:产品型号={productModel},运行状态={runStatusDesc},总结果={qualificationFlag}"); + + // 5. 构建数据实体 + return new PlcProductionData + { + PlcIp = ip.Trim(), + OccurTime = DateTime.Now, + LineCode = "line2", + WorkstationCode = workstationCode, + ProductModel = productModel ?? string.Empty, + ProductName = productName ?? string.Empty, + ProductCode = sn ?? string.Empty, + SN1 = sn ?? string.Empty, + SN2 = sn ?? string.Empty, + QualificationFlag = qualificationFlag, + ReworkFlag = reworkFlag, + Automanual = machineModel, + Runstatus = runStatus, + OnlineStatus = onlineStatusDesc, + ProduceModel = produceModelDesc, + TrayNo = trayNo.ToString(), + CreatedBy = "PLC", + CreatedTime = DateTime.Now + }; + } + catch (Exception ex) + { + Console.WriteLine($"OP020-2({ip})数据读取异常:{ex.Message}"); + return null; + } + } + /// /// OP070-1数据读取 /// private async Task ReadOP070_1DataAsync(Plc plc, string ip,string workstationCode)