diff --git a/RIZO.Admin.WebApi/PLC/Service/PlcService.cs b/RIZO.Admin.WebApi/PLC/Service/PlcService.cs index 34685eb..781cb77 100644 --- a/RIZO.Admin.WebApi/PLC/Service/PlcService.cs +++ b/RIZO.Admin.WebApi/PLC/Service/PlcService.cs @@ -46,28 +46,56 @@ namespace RIZO.Admin.WebApi.PLC.Service //MES返回PLC请求映射 private readonly Dictionary _mesIntReturnMap = new() { - { "设备使能", "DB1317.DBW0" }, // Int - { "工位开始查询结果", "DB1037.DBW2000" }, // Int - { "保存结果", "DB1317.DBW2002" }, // Int + { "设备使能", "DB1000.DBW0" }, // Int + { "工位开始查询结果", "DB1000.DBW2000" }, // Int + { "保存结果", "DB1000.DBW2002" }, // Int }; - - private readonly Dictionary _mesStringReturnMap = new() + private readonly Dictionary _mesop20_3IntReturnMap = new() { - { "产品型号", ("DB1317.DBB1016", 14) }, // String[14] - { "订单下发", ("DB1317.DBB1032", 50) }, // String[50] + { "设备使能", "DB1000.DBW0" }, // Int + { "工位开始查询结果", "DB1000.DBW2000" }, // Int + { "保存结果", "DB1000.DBW2012" }, // Int }; - - private readonly Dictionary _mesop058IntReturnMap = new() + private readonly Dictionary _mesop20_4IntReturnMap = new() + { + { "设备使能", "DB1000.DBW0" }, // Int + { "工位开始查询结果", "DB1000.DBW2000" }, // Int + { "1#产品结束保存结果", "DB1000.DBW2002" }, // Int + { "2#产品结束保存结果", "DB1000.DBW2004" }, // Int + { "3#产品结束保存结果", "DB1000.DBW2006" }, // Int + { "4#产品结束保存结果", "DB1000.DBW2008" }, // Int + }; + private readonly Dictionary _mesop50IntReturnMap = new() { { "设备使能", "DB300.DBW0" }, // Int - { "工位开始查询结果", "DB300.DBW2000" }, // Int - { "保存结果", "DB300.DBW2002" }, // Int + { "1#产品结束保存结果", "DB300.DBW48" }, // Int + { "2#产品结束保存结果", "DB300.DBW204" }, // Int + { "3#产品结束保存结果", "DB300.DBW360" }, // Int + { "4#产品结束保存结果", "DB300.DBW516" }, // Int }; - - private readonly Dictionary _mesop058StringReturnMap = new() + private readonly Dictionary _mesop058IntReturnMap = new() { - { "产品型号", ("DB300.DBB1016", 14) }, // String[14] - { "订单下发", ("DB300.DBB1032", 50) }, // String[50] + { "设备使能", "DB1003.DBW0" }, // Int + { "工位开始查询结果", "DB1003.DBW2000" }, // Int + { "保存结果", "DB1003.DBW2002" }, // Int + }; + private readonly Dictionary _mesop075IntReturnMap = new() + { + { "设备使能", "DB1000.DBW0" }, // Int + { "工位开始查询结果", "DB1000.DBW2000" }, // Int + { "保存结果", "DB1000.DBW2004" }, // Int + }; + private readonly Dictionary _mesop080_1IntReturnMap = new() + { + { "设备使能", "DB1000.DBW0" }, // Int + { "工位开始查询结果", "DB1000.DBW2000" }, // Int + { "保存结果", "DB1000.DBW2006" }, // Int + }; + private readonly Dictionary _mesop100IntReturnMap = new() + { + { "设备使能", "DB1000.DBW0" }, // Int + { "工位开始查询结果", "DB1000.DBW2000" }, // Int + { "保存结果", "DB1000.DBW2008" }, // Int }; // OP020-2 专属地址映射(合盖工位,DB1001) @@ -5615,35 +5643,23 @@ namespace RIZO.Admin.WebApi.PLC.Service { try { - if (plcName == "OP058") + var targetMap = plcName switch { - WritePlcValue(plc, _mesop058IntReturnMap["设备使能"], "1"); - //WritePlcValue(plc, _mesIntReturnMap["工位开始查询结果"], "1"); - WritePlcValue(plc, _mesop058IntReturnMap["保存结果"], saveResult); - if (prodData.ProductModel != null && prodData.ProductModel.Length > 0) - { - WritePlcString(plc, _mesop058StringReturnMap["产品型号"].Addr, _mesop058StringReturnMap["产品型号"].Len, prodData.ProductModel); - } - else - { - WritePlcString(plc, _mesop058StringReturnMap["产品型号"].Addr, _mesop058StringReturnMap["产品型号"].Len, prodData.ProductModel); - } - WritePlcString(plc, _mesop058StringReturnMap["订单下发"].Addr, _mesop058StringReturnMap["订单下发"].Len, "cpxhtest"); - } - else + "OP20-3" => _mesop20_3IntReturnMap, + "OP20-4" => _mesop20_4IntReturnMap, + "OP50-1" => _mesop20_4IntReturnMap, + //"OP050" => _mesop050IntReturnMap,//四个保存结果 + "OP058" => _mesop058IntReturnMap, + "OP075" => _mesop075IntReturnMap, + "OP080-1" => _mesop080_1IntReturnMap, + "OP100" => _mesop100IntReturnMap, + "OP110-2" => _mesop080_1IntReturnMap, + _ => _mesIntReturnMap + }; + if (targetMap != null) { - WritePlcValue(plc, _mesIntReturnMap["设备使能"], "1"); - //WritePlcValue(plc, _mesIntReturnMap["工位开始查询结果"], "1"); - WritePlcValue(plc, _mesIntReturnMap["保存结果"], saveResult); - if (prodData.ProductModel != null && prodData.ProductModel.Length > 0) - { - WritePlcString(plc, _mesStringReturnMap["产品型号"].Addr, _mesStringReturnMap["产品型号"].Len, prodData.ProductModel); - } - else - { - WritePlcString(plc, _mesStringReturnMap["产品型号"].Addr, _mesStringReturnMap["产品型号"].Len, prodData.ProductModel); - } - WritePlcString(plc, _mesStringReturnMap["订单下发"].Addr, _mesStringReturnMap["订单下发"].Len, "cpxhtest"); + WritePlcValue(plc, targetMap["设备使能"], "1"); + WritePlcValue(plc, targetMap["保存结果"], saveResult); } } catch (Exception ex) diff --git a/RIZO.Admin.WebApi/Program.cs b/RIZO.Admin.WebApi/Program.cs index 509e580..2c66ed7 100644 --- a/RIZO.Admin.WebApi/Program.cs +++ b/RIZO.Admin.WebApi/Program.cs @@ -41,7 +41,7 @@ builder.Services.Configure(builder.Configuration.GetSection( builder.Services.AddSingleton(); builder.Services.AddScoped(); // 新增:注册PLC后台监听服务(项目启动自动执行) -//builder.Services.AddHostedService(); +builder.Services.AddHostedService(); // ========================== // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle @@ -112,7 +112,8 @@ builder.Services.AddLocalization(options => options.ResourcesPath = ""); //PLC进站后台服务注册 //builder.Services.AddHostedService(); -//builder.Services.AddHostedService(); +//builder.Services.AddHostedService(); +builder.Services.AddHostedService(); //螺丝枪服务注册 //builder.Services.AddHostedService(); diff --git a/RIZO.Admin.WebApi/appsettings.json b/RIZO.Admin.WebApi/appsettings.json index 10345e2..1a0adae 100644 --- a/RIZO.Admin.WebApi/appsettings.json +++ b/RIZO.Admin.WebApi/appsettings.json @@ -416,16 +416,16 @@ }, { "Id": 17, - "isEnble": false, + "isEnble": true, "WorkStationCode": "OP100", "WorkStationName": "OP100 手动上料壳体&盖板", "PlcType": "S71500", - "IpAddress": "192.168.11.101", + "IpAddress": "192.168.11.171", "IntoStation": { "Heartbeat": "DB1000.DBW0", // 心跳地址 "IntoStationAsk": "DB1001.DBW2000", // 进站请求地址 "ProductModel": "DB1001.DBB1000", - "ProductSN": "DB1001.DBB1054", + "ProductSN": "DB1001.DBB2100", "IntoStationResp": "DB1000.DBW2000" // 进站回复结果地址 } }, @@ -446,7 +446,7 @@ }, { "Id": 19, - "isEnble": false, + "isEnble": true, "WorkStationCode": "OP1012", "WorkStationName": "OP1012 Ring terminal装配", "PlcType": "S71500", @@ -485,7 +485,7 @@ "Heartbeat": "DB1010.DBW0", // 心跳地址 "IntoStationAsk": "DB1001.DBW2000", // 进站请求地址 "ProductModel": "DB1001.DBB1000", - "ProductSN": "DB1001.DBB1054", + "ProductSN": "DB1001.DBB2100", "IntoStationResp": "DB1010.DBW2000" // 进站回复结果地址 } }, diff --git a/RIZO.Service/PLCBackground/PlcConntectHepler.cs b/RIZO.Service/PLCBackground/PlcConntectHepler.cs index 090815a..55d53b8 100644 --- a/RIZO.Service/PLCBackground/PlcConntectHepler.cs +++ b/RIZO.Service/PLCBackground/PlcConntectHepler.cs @@ -25,6 +25,7 @@ namespace RIZO.Service.PLC _cpuType = cpuType; _rack = rack; _slot = slot; + ConnectAsync(); } private async Task ConnectAsync(CancellationToken cancellationToken = default) diff --git a/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_Common.cs b/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_Common.cs index a5bc0a2..6b26e5b 100644 --- a/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_Common.cs +++ b/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_Common.cs @@ -65,8 +65,6 @@ namespace RIZO.Service.PLCBackground.Stations.Into using (_plcService = new PlcConntectHepler(plcSetting.IpAddress, cpuType)) { - - while (!stoppingToken.IsCancellationRequested) { @@ -117,12 +115,12 @@ namespace RIZO.Service.PLCBackground.Stations.Into EntryPermissionResult result = await checkEntryPermission(productModel, productSN, WorkstationCode, processOperations); // "DB1010.DBW2000" // await _plcService.WriteAsync(plcSetting.intoStation.IntoStationResp, (short)result); - await _plcService.WriteAsync2(plcSetting.intoStation.IntoStationResp, result); + await _plcService.WriteAsync2(plcSetting.intoStation.IntoStationResp,(int) result); } await Task.Delay(_pollingInterval, stoppingToken); } - catch (OperationCanceledException) + catch (OperationCanceledException ex) { _logger.Info("PLC Polling Service is stopping"); break; @@ -184,15 +182,18 @@ namespace RIZO.Service.PLCBackground.Stations.Into /// protected virtual async Task checkEntryPermission(string productModel, string productSN, string workstationCode, List processOperations) { - + workstationCode = workstationCode.Trim(); EntryPermissionResult result = EntryPermissionResult.UnkownException; //2上工位无记录 //ProcessOperation LastOperation1 = processOperations.Where(operation => operation.OperationSeq < processOperations.Where(it => it.OperationCode == workstationCode).Select(it => it.OperationSeq).First()).OrderByDescending(operation => operation.OperationSeq).First(); - var LastOperationSeqObject = processOperations - .Where(operation => operation.OperationCode == workstationCode) - .FirstOrDefault(); + //var LastOperationSeqObject = processOperations + // .Where(operation => operation.OperationCode == workstationCode) + // .FirstOrDefault(); + var LastOperationSeqObject = processOperations?.Where(operation => + string.Equals(operation.OperationCode, workstationCode, StringComparison.OrdinalIgnoreCase)) + .FirstOrDefault(); if (LastOperationSeqObject.LastOperationSeq == 0) { return EntryPermissionResult.NoRecordAtPreviousStation; @@ -204,7 +205,7 @@ namespace RIZO.Service.PLCBackground.Stations.Into .Replace("\r", "") // 移除回车符 .Replace("\n", "") // 可选:移除换行符 .Trim(); // 移除首尾空格 - + productSN = productSN.Replace("\r", "").Replace("\n", "").Trim(); // 3. 构建并执行真正的异步查询(补充执行方法,使 await 生效) var ExistLastOperationRecord = await DbScoped.SugarScope.CopyNew() .Queryable() diff --git a/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP100.cs b/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP100.cs index 56c61aa..a36fb67 100644 --- a/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP100.cs +++ b/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP100.cs @@ -24,7 +24,7 @@ namespace RIZO.Service.PLCBackground.Stations.Into /// /// OP点散热胶GF1500 /// - [AppService(ServiceType = typeof(PlcIntoStationService_OP100), ServiceLifetime = LifeTime.BackgroundService)] + //[AppService(ServiceType = typeof(PlcIntoStationService_OP100), ServiceLifetime = LifeTime.BackgroundService)] public class PlcIntoStationService_OP100 : PlcIntoStationService_Common { public PlcIntoStationService_OP100(IOptions options) : base(options) diff --git a/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP102.cs b/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP102.cs index 2927a35..21954c4 100644 --- a/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP102.cs +++ b/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP102.cs @@ -24,7 +24,7 @@ namespace RIZO.Service.PLCBackground.Stations.Into /// /// OP点散热胶GF1500 /// - [AppService(ServiceType = typeof(PlcIntoStationService_OP102), ServiceLifetime = LifeTime.BackgroundService)] + // [AppService(ServiceType = typeof(PlcIntoStationService_OP102), ServiceLifetime = LifeTime.BackgroundService)] public class PlcIntoStationService_OP102 : PlcIntoStationService_Common { public PlcIntoStationService_OP102(IOptions options) : base(options)