You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
6.2 KiB
C#

using Admin.Core.Common;
1 year ago
using Aucma.Core.HwPLc;
1 year ago
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using System.Collections.Generic;
using System.Timers;
namespace Aucma.Core.RunPlc
{
/// <summary>
/// PLC任务
/// </summary>
public class RunPlcService : IRunPlcService
{
1 year ago
System.Timers.Timer timer = new System.Timers.Timer(3000);//创建定时器设置间隔时间为1000毫秒
1 year ago
System.Timers.Timer timer1 = new System.Timers.Timer(3000);
#region 构造函数
public RunPlcService()
{
1 year ago
StartMelsecMcPlcServer();
StartSiemensPlcServer();
}
#endregion
#region 心跳
public Task StartPlcAsync()
{
timer.Elapsed += new System.Timers.ElapsedEventHandler(ExecMelsecMcHeartTask); //到达时间的时候执行事件;
timer.AutoReset = true;//设置是执行一次false还是一直执行(true)
timer.Enabled = true;//需要调用 timer.Start()或者timer.Enabled = true来启动它
timer.Start();//timer.Start()的内部原理还是设置timer.Enabled = true;
1 year ago
1 year ago
timer1.Elapsed += new System.Timers.ElapsedEventHandler(ExecSiemensHeartTask);
timer1.AutoReset = true;//设置是执行一次false还是一直执行(true)
timer1.Enabled = true;//需要调用 timer.Start()或者timer.Enabled = true来启动它
timer1.Start();//timer.Start()的内部原理还是设置timer.Enabled = true;
1 year ago
return Task.CompletedTask;
}
1 year ago
private static void StartMelsecMcPlcServer()
{
var allPlcServices = Appsettings.app<PlcModel>("PLCServer").ToList();
1 year ago
if (allPlcServices == null) return ;
1 year ago
var list = allPlcServices.FindAll(d => d.PlcType == "Melsec" && d.Enabled == true);
1 year ago
for (int i = 1; i <= list.Count; i++)
{
PlcModel model = new PlcModel();
1 year ago
model.Id = list[i-1].Id;
model.EquipName = list[i - 1].EquipName;
model.IP = list[i - 1].IP;
model.Port = list[i - 1].Port;
model.PlcType = list[i - 1].PlcType;
model.plc = new MelsecPlc(list[i - 1].IP, list[i - 1].Port);
PlcHelper.melsecList.Add(model);
}
1 year ago
// return Task.CompletedTask;
}
1 year ago
private static void StartSiemensPlcServer()
{
1 year ago
List<PlcModel> allPlcServices = Appsettings.app<PlcModel>("PLCServer").ToList();
1 year ago
if (allPlcServices == null) return ;
1 year ago
var list = allPlcServices.FindAll(d => d.PlcType=="Siemens" && d.Enabled == true);
foreach (var item in list)
{
PlcModel model = new PlcModel();
model.Id = item.Id;
model.EquipName = item.EquipName;
model.IP = item.IP;
model.Port = item.Port;
model.PlcType = item.PlcType;
model.plc = new SiemensPlc(item.IP, item.Port);
PlcHelper.siemensList.Add(model);
}
1 year ago
}
#endregion
#region 心跳检测
private async void ExecMelsecMcHeartTask(object? sender, ElapsedEventArgs e)
{
int num = PlcHelper.melsecList.Count;
1 year ago
if (num == 0) return;
for (int i = 1; i <= num; i++)
{
1 year ago
var item = PlcHelper.melsecList.Where(c => c.Id == i).FirstOrDefault();
if (item == null) continue;
1 year ago
var test= item.plc.ReadInt16("D6050");
1 year ago
if (item.plc.Read("M100").Result)
{
item.IsConnect = true;
item.plc.IsConnected = true;
//Console.WriteLine(item.melsecPlc.ReadBool("M100"));
//Console.WriteLine($"{item.EquipName}:PLC连接成功");
}
else
{
if (item.plc.IsConnected == false)
{
item.IsConnect = false;
//Console.WriteLine($"{item.EquipName}:PLC连接失败");
// System.GC.Collect();
bool r = item.plc.Connect(item.IP, item.Port);
if (r)
{
item.plc.IsConnected = r;
item.IsConnect = r;
}
else
{
item.plc.IsConnected = r;
item.IsConnect = r;
}
}
}
}
await Task.CompletedTask;
}
private async void ExecSiemensHeartTask(object? sender, ElapsedEventArgs e)
{
int num = PlcHelper.siemensList.Count;
1 year ago
if (num == 0) return;
for (int i = 1; i <= num; i++)
{
var item = PlcHelper.siemensList.Where(c => c.Id == i).FirstOrDefault();
1 year ago
if (item == null) continue;
if (await item.plc.Read("M100"))
{
item.IsConnect = true;
item.plc.IsConnected = true;
//Console.WriteLine(item.melsecPlc.ReadBool("M100"));
//Console.WriteLine($"{item.EquipName}:PLC连接成功");
}
else
{
if (item.plc.IsConnected == false)
{
item.IsConnect = false;
//Console.WriteLine($"{item.EquipName}:PLC连接失败");
System.GC.Collect();
bool r = item.plc.Connect(item.IP, item.Port);
if (r)
{
item.plc.IsConnected = r;
item.IsConnect = r;
}
else
{
item.plc.IsConnected = r;
item.plc.DisConnect();
}
}
}
}
}
#endregion
}
}