change - PLC连接初始化

master
Wen JY 10 months ago
parent 2b8b337ada
commit 3bacba92d7

@ -1,4 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise">
<file url="file://$PROJECT_DIR$/SlnMesnac/Program.cs" charset="UTF-8" />
</component>
</project>

@ -485,7 +485,14 @@ namespace SlnMesnac.Plc.Impl
private void PrintLogInfo(string message, Exception ex = null)
{
if (ex != null)
{
_logger.LogError($"{message}{ex.Message}");
}
else
{
_logger.LogInformation(message);
}
}
}

@ -38,7 +38,7 @@ namespace SlnMesnac.Plc.Impl
/// <returns></returns>
public bool Connect(string IP, int port)
{
PrintLogInfo("三菱Q系列PLC连接开始");
PrintLogInfo($"三菱Q系列PLC连接开始,IP:{IP};Port:{port}");
melsec_net.IpAddress = IP;
melsec_net.Port = port;
try
@ -53,7 +53,7 @@ namespace SlnMesnac.Plc.Impl
else
{
this.IsConnected = false;
PrintLogInfo("三菱Q系列PLC建立连接失败");
PrintLogInfo($"三菱Q系列PLC建立连接失败:{connect.Message}");
return false;
}
}
@ -437,7 +437,14 @@ namespace SlnMesnac.Plc.Impl
private void PrintLogInfo(string message, Exception ex = null)
{
if (ex != null)
{
_logger.LogError($"{message}{ex.Message}");
}
else
{
_logger.LogInformation(message);
}
}
}
}

@ -442,7 +442,14 @@ namespace SlnMesnac.Plc.Impl
private void PrintLogInfo(string message, Exception ex = null)
{
if (ex != null)
{
_logger.LogError($"{message}{ex.Message}");
}
else
{
_logger.LogInformation(message);
}
}
}
}

@ -409,7 +409,14 @@ namespace SlnMesnac.Plc.Impl
private void PrintLogInfo(string message, Exception ex = null)
{
if (ex != null)
{
_logger.LogError($"{message}{ex.Message}");
}
else
{
_logger.LogInformation(message);
}
}
}
}

@ -4,6 +4,8 @@ using SlnMesnac.Plc.Impl;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using SlnMesnac.Config;
namespace SlnMesnac.Plc
{
@ -21,30 +23,59 @@ namespace SlnMesnac.Plc
private readonly MelsecBinaryPlc _melsecBinaryPlc;
private readonly OmronNJPlc _omronNJPlc;
private readonly SiemensPlc _siemensPlc;
private readonly AppConfig _appConfig;
public PlcPool(ILogger<PlcPool> logger, InovancePlc inovancePlc,MelsecBinaryPlc melsecBinaryPlc,OmronNJPlc omronNJPlc,SiemensPlc siemensPlc)
public PlcPool(ILogger<PlcPool> logger, InovancePlc inovancePlc,MelsecBinaryPlc melsecBinaryPlc,OmronNJPlc omronNJPlc,SiemensPlc siemensPlc,AppConfig appConfig)
{
_logger = logger;
_inovancePlc = inovancePlc;
_melsecBinaryPlc = melsecBinaryPlc;
_omronNJPlc = omronNJPlc;
_siemensPlc = siemensPlc;
_appConfig = appConfig;
//this.Init();
}
public void Init()
{
if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41"))
{
_logger.LogInformation("HslCommunication激活失败可用时长24小时");
}
_logger.LogInformation("HslCommunication 11.0.6.0激活成功");
try
{
if (_appConfig.plcConfig != null)
{
Task.Run(() =>
{
foreach (var item in _appConfig.plcConfig)
{
AddPlc(item.plcType, item.plcIp, item.plcPort, item.plcKey);
}
});
}
else
{
_logger.LogInformation("PLC配置信息为空");
}
}
catch (Exception e)
{
_logger.LogError($"PLC初始化连接异常{e.Message}");
}
}
/// <summary>
/// 初始化Plc
/// 添加PLC连接
/// </summary>
/// <param name="plcType"></param>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <param name="key"></param>
public void InitPlc(string plcType, string ip, int port, string key)
public void AddPlc(string plcType, string ip, int port, string key)
{
IPlc _plc = null;
switch (plcType)

@ -4,6 +4,7 @@ using SlnMesnac.Plc.Impl;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Builder;
namespace SlnMesnac.Plc
{
@ -17,5 +18,12 @@ namespace SlnMesnac.Plc
services.AddSingleton<SiemensPlc>();
services.AddSingleton<PlcPool>();
}
public static IApplicationBuilder UsePlcExtensions(this IApplicationBuilder app)
{
var plcPool = app.ApplicationServices.GetService<PlcPool>();
plcPool.Init();
return app;
}
}
}

@ -7,11 +7,13 @@
<ItemGroup>
<PackageReference Include="HslCommunication" Version="11.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SlnMesnac.Common\SlnMesnac.Common.csproj" />
<ProjectReference Include="..\SlnMesnac.Config\SlnMesnac.Config.csproj" />
</ItemGroup>
</Project>

@ -17,18 +17,15 @@ namespace SlnMesnac.Controllers
private readonly IBaseUserService _service;
private readonly PlcPool _plcPool;
/// <summary>
///
/// </summary>
/// <param name="logger"></param>
/// <param name="service"></param>
public BaseUserController(ILogger<BaseUserController> logger, IBaseUserService service, PlcPool plcPool)
public BaseUserController(ILogger<BaseUserController> logger, IBaseUserService service)
{
_logger = logger;
_service = service;
_plcPool = plcPool;
}
/// <summary>
@ -38,7 +35,6 @@ namespace SlnMesnac.Controllers
[HttpGet]
public IEnumerable<BaseUser> Get()
{
_plcPool.InitPlc("SiemensPlc", "127.0.0.1", 102, "SiemensPlc");
IEnumerable<BaseUser> users = null;
try
{

@ -2,6 +2,7 @@ using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using SlnMesnac.Config;
using SlnMesnac.Model.dto;
using SlnMesnac.Plc;
using SlnMesnac.Repository.service;
namespace SlnMesnac.Controllers;
@ -21,7 +22,9 @@ public class IngCheckController
///
/// </summary>
/// <param name="logger"></param>
public IngCheckController(ILogger<IngCheckController> logger,IMcsBinToMaterService mcsBinToMaterService,AppConfig appConfig)
public IngCheckController(ILogger<IngCheckController> logger,
IMcsBinToMaterService mcsBinToMaterService,
AppConfig appConfig)
{
_logger = logger;
_mcsBinToMaterService = mcsBinToMaterService;

@ -119,6 +119,9 @@ namespace SlnMesnac
//启用Serilog中间件
app.UseSerilogExtensions();
//初始化PLC中间件
app.UsePlcExtensions();
//app.UseHttpsRedirection();
app.UseRouting();

Loading…
Cancel
Save