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.

103 lines
3.6 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Admin.Core.Common;
using Aucma.Core.HwPLc;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using System.Collections.Generic;
using System.Timers;
namespace Aucma.Core.RunPlc
{
/// <summary>
/// PLC任务
/// </summary>
public class RunPlcService : IRunPlcService
{
#region 构造函数
public RunPlcService()
{
}
#endregion
public async Task StartMelsecMcSeverAsync()
{
await StartMelsecMcPlcServer();
}
#region 心跳
public async Task StartMelsecPlcAsync()
{
await Task.Run(async () =>
{
System.Timers.Timer timer = new System.Timers.Timer(5000);
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;
await Task.CompletedTask;
});
}
private static async Task StartMelsecMcPlcServer()
{
var allPlcServices = Appsettings.app<PlcModel>("PLCServer").ToList();
if (allPlcServices == null) return;
var list = allPlcServices.FindAll(d => d.PlcType == "Melsec" && d.Enabled == true);
for (int i = 1; i <= list.Count; i++)
{
PlcModel model = new PlcModel();
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);
}
await Task.CompletedTask;
}
#endregion
#region 心跳检测
private async void ExecMelsecMcHeartTask(object? sender, ElapsedEventArgs e)
{
int num = PlcHelper.melsecList.Count;
if (num == 0) return;
for (int i = 1; i <= num; i++)
{
var item = PlcHelper.melsecList.Where(c => c.Id == i).FirstOrDefault();
if (item == null) continue;
var test = item.plc.ReadInt16("D6050");
if (item.plc.Read("D6050").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;
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;
}
}
}
Thread.Sleep(5000);
}
await Task.CompletedTask;
}
#endregion
}
}