PLC通讯

This commit is contained in:
quowingwang 2026-01-19 15:41:58 +08:00
parent f08d0ef36f
commit f2e4626616
6 changed files with 52 additions and 9 deletions

View File

@ -38,7 +38,7 @@ namespace RIZO.Admin.WebApi.PLC.Controllers
/// 批量测试所有PLC的连接、读写功能核心接口
/// </summary>
/// <returns>批量测试结果</returns>
[HttpPost("batch-test")]
[HttpGet("batch-test")]
[Log(Title = "PLC测试", BusinessType = BusinessType.OTHER)]
public async Task<IActionResult> BatchTestAllPlc()
{
@ -58,7 +58,7 @@ namespace RIZO.Admin.WebApi.PLC.Controllers
/// </summary>
/// <param name="config">单个PLC配置参数</param>
/// <returns>单PLC测试结果</returns>
[HttpPost("single-test")]
[HttpGet("single-test")]
[ActionPermissionFilter(Permission = "business:plc:singletest")]
[Log(Title = "PLC测试", BusinessType = BusinessType.OTHER)]
public async Task<IActionResult> TestSinglePlc([FromBody] PlcConfig config)

View File

@ -44,6 +44,8 @@ namespace RIZO.Admin.WebApi.PLC.Model.Dto
public string ReworkFlag { get; set; }
public int? ProductionCycle { get; set; }
public int? AutoManual { get; set; }
public int? RunStatus { get; set; }
public string Remark { get; set; }

View File

@ -10,8 +10,8 @@
public class GlobalPlcConfig
{
public int ConnectTimeout { get; set; } = 5000;
public int ReadWriteTimeout { get; set; } = 5000;
public int ConnectTimeout { get; set; } = 2000;
public int ReadWriteTimeout { get; set; } = 2000;
}
// PLC测试结果实体

View File

@ -93,6 +93,18 @@ namespace RIZO.Admin.WebApi.PLC.Model
[SugarColumn(ColumnName = "production_cycle")]
public int? ProductionCycle { get; set; }
/// <summary>
/// 设备自动手动0-自动1-手动
/// </summary>
[SugarColumn(ColumnName = "automanual")]
public int? AutoManual { get; set; }
/// <summary>
/// 运行状态:1正常2异常
/// </summary>
[SugarColumn(ColumnName = "runstatus")]
public int? RunStatus { get; set; }
/// <summary>
/// 备注
/// </summary>

View File

@ -44,12 +44,10 @@ namespace RIZO.Admin.WebApi.PLC.Service
public PlcHostedService(
ILogger<PlcHostedService> logger,
PlcService plcService,
IOptions<List<PlcConfig>> plcConfigs)
PlcService plcService)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_plcService = plcService ?? throw new ArgumentNullException(nameof(plcService));
_plcConfigs = plcConfigs?.Value ?? new List<PlcConfig>();
//初始化plcConfigs
_plcConfigs = initPlcConfigs(_plcConfigs);

View File

@ -1,4 +1,5 @@
// 统一引入所有必要命名空间
using MDM.Services.Plant;
using Microsoft.Extensions.Options;
using RIZO.Admin.WebApi.PLC.Model;
using RIZO.Common;
@ -26,6 +27,7 @@ namespace RIZO.Admin.WebApi.PLC.Service
private readonly GlobalPlcConfig _globalConfig;
private PlcProductionDataService plcProductionDataService = new PlcProductionDataService();
private PlantWorkstationService plantWorkstationService = new PlantWorkstationService();
// PLC地址映射严格匹配业务地址清单
private readonly Dictionary<string, (string Addr, int Len)> _plcStringMap = new()
@ -56,10 +58,11 @@ namespace RIZO.Admin.WebApi.PLC.Service
/// </summary>
/// <param name="plcConfigs">所有PLC的连接配置</param>
/// <param name="globalConfig">PLC全局超时配置</param>
public PlcService(IOptions<List<PlcConfig>> plcConfigs, IOptions<GlobalPlcConfig> globalConfig)
public PlcService(IOptions<GlobalPlcConfig> globalConfig)
{
_plcConfigs = plcConfigs?.Value ?? throw new ArgumentNullException(nameof(plcConfigs), "PLC配置不能为空");
_globalConfig = globalConfig?.Value ?? throw new ArgumentNullException(nameof(globalConfig), "PLC全局配置不能为空");
//初始化plcConfigs
_plcConfigs = initPlcConfigs(_plcConfigs);
}
#endregion
@ -113,6 +116,10 @@ namespace RIZO.Admin.WebApi.PLC.Service
QualificationFlag = (await ReadPlcIntAsync(plc, _plcIntMap["QualificationFlag"])).ToString(),
//返工标志0正常1返工
ReworkFlag = (await ReadPlcIntAsync(plc, _plcIntMap["ReworkFlag"])).ToString(),
//设备自动手动0-自动1-手动
AutoManual = (await ReadPlcIntAsync(plc, _plcIntMap["AutoManual"])),
//运行状态:1正常2异常
RunStatus = (await ReadPlcIntAsync(plc, _plcIntMap["AutoManual"])),
//生产节拍秒
ProductionCycle = await ReadPlcIntAsync(plc, _plcIntMap["ProductionCycle"])
};
@ -608,5 +615,29 @@ namespace RIZO.Admin.WebApi.PLC.Service
Dispose(false);
}
#endregion
private List<PlcConfig> initPlcConfigs(List<PlcConfig> result)
{
var defaultResult = result ?? new List<PlcConfig>();
try
{
List<PlcConfig> query = plantWorkstationService.Queryable()
.Where(it => it.Status == 1)
.Select(it => new PlcConfig
{
PlcName = it.WorkstationCode,
Ip = it.PlcIP,
Rack = (short)it.Rack, // 直接强制转换it.Rack是int非空
Slot = (short)it.Slot // 同理it.Slot是int非空
})
.ToList();
return query.Count > 0 ? query : defaultResult;
}
catch (Exception ex)
{
Console.WriteLine($"初始化PLC配置异常{ex.Message}");
return defaultResult;
}
}
}
}