master
杨威 7 months ago
parent edee38185d
commit 73a7cd3a0e

@ -1,28 +1 @@
using HslCommunication.Profinet.Siemens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SynPlc
{
public class PlcConnect
{
private static readonly Lazy<SiemensS7Net> lazy = new Lazy<SiemensS7Net>(() => new PlcConnect().SiemensS7NetConnection());
public static SiemensS7Net Instance => lazy.Value;
public SiemensS7Net SiemensS7NetConnection()
{
SiemensPLCS siemensPLCS = SiemensPLCS.S200Smart;
SiemensS7Net s7 = new SiemensS7Net(siemensPLCS);
s7.IpAddress = "127.0.0.1";
s7.Port = 102;
var su = s7.ConnectServer();
return s7;
}
}
}


@ -1,3 +1,4 @@
using HslCommunication.Profinet.Siemens;
using NewLife.Extensions.Hosting.AgentService;
using NewLife.Log;
using SqlSugar;
@ -16,6 +17,15 @@ IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context,services) =>
{
string sqlConect = context.Configuration.GetConnectionString("Sql");
string Plc = context.Configuration.GetConnectionString("Plc");
services.AddSingleton(() => new SiemensS7Net(SiemensPLCS.S200Smart)
{
IpAddress = Plc,
Port = 102
});
services.AddScoped<ISqlSugarClient>(s =>
{
//Scoped用SqlSugarClient

@ -1,5 +1,4 @@
using HslCommunication.Profinet.Siemens;
using Microsoft.Extensions.Logging;
using NewLife.Log;
using SqlSugar;
using SynPlc.Entity;
@ -8,19 +7,17 @@ namespace SynPlc;
public class Worker : BackgroundService
{
SiemensS7Net S7 = PlcConnect.Instance;
private IServiceScopeFactory scopeFactory;
public Worker(IServiceScopeFactory _scopeFactory)
private readonly IServiceScopeFactory scopeFactory;
public Worker(IServiceScopeFactory scopeFactory)
{
this.scopeFactory=_scopeFactory;
this.scopeFactory = scopeFactory;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
SynPlcFromDb(stoppingToken);
@ -29,7 +26,7 @@ public class Worker : BackgroundService
{
XTrace.WriteException(e);
}
XTrace.WriteLine("Worker running at: {05}", DateTimeOffset.Now);
await Task.Delay(1000, stoppingToken);
@ -38,51 +35,57 @@ public class Worker : BackgroundService
private async Task SynPlcFromDb(CancellationToken stoppingToken)
{
using var scope = scopeFactory.CreateScope();
var services = scope.ServiceProvider;
var dbClient = services.GetService<ISqlSugarClient>();
var S7 = services.GetService<SiemensS7Net>();
var operateResult = await S7.ConnectServerAsync();
if (!operateResult.IsSuccess)
{
operateResult = await S7.ConnectServerAsync();
if (!operateResult.IsSuccess) XTrace.WriteLine("连接失败");
}
var list = await dbClient.Queryable<EEquipmentStateEntity>().ToListAsync(stoppingToken);
foreach(var e in list)
foreach (var e in list)
{
var a = await S7.ReadBoolAsync(e.Point);
if (a.IsSuccess)
{
e.UpdateTime = DateTime.Now;
e.PValue = a.Content;
await dbClient.Updateable(e).UpdateColumns(x => new { x.UpdateTime, x.PValue }).ExecuteCommandAsync(stoppingToken);
await dbClient.Updateable(e).UpdateColumns(x => new { x.UpdateTime, x.PValue })
.ExecuteCommandAsync(stoppingToken);
}
else
{
XTrace.WriteLine("{0} 读取异常",e.Point );
XTrace.WriteLine("{0} 读取异常", e.Point);
}
}
var listp = await dbClient.Queryable<EPointsInfoEntity>().ToListAsync(stoppingToken);
List<EPointDataEntity> ls = new List<EPointDataEntity>();
var ls = new List<EPointDataEntity>();
foreach (var e in listp)
{
var a = await S7.ReadDoubleAsync(e.Point);
if (a.IsSuccess)
{
EPointDataEntity listd = new EPointDataEntity();
var listd = new EPointDataEntity();
listd.ID = Guid.NewGuid().ToString();
listd.PID = e.ID;
listd.CreateTime = DateTime.Now;
listd.Data = a.Content.ToDecimal();
ls.Add(listd);
}
else
{
XTrace.WriteLine("{0} 读取异常",e.Point );
XTrace.WriteLine("{0} 读取异常", e.Point);
}
}
if (ls.Any())
{
await dbClient.Insertable(ls).ExecuteCommandAsync(stoppingToken);
}
if (ls.Any()) await dbClient.Insertable(ls).ExecuteCommandAsync(stoppingToken);
}
}

@ -6,6 +6,7 @@
}
},
"ConnectionStrings": {
"Sql": "server=175.27.215.92;uid=sa;pwd=Hawei@123;database=Hsdb;"
"Sql": "server=175.27.215.92;uid=sa;pwd=Hawei@123;database=Hsdb;",
"Plc":"127.0.0.1"
}
}

Loading…
Cancel
Save