|
|
|
|
using Admin.Core.Common;
|
|
|
|
|
using Aucma.Core.PLc;
|
|
|
|
|
using System.Timers;
|
|
|
|
|
|
|
|
|
|
namespace Aucma.Core.RunPlc
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 扫码器初始化
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class RunPlcService : IRunPlcService
|
|
|
|
|
{
|
|
|
|
|
System.Timers.Timer timer = new System.Timers.Timer(5000);//创建定时器,设置间隔时间为1000毫秒;
|
|
|
|
|
|
|
|
|
|
#region 构造函数
|
|
|
|
|
public RunPlcService()
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () => {
|
|
|
|
|
await StartMelsecMcPlcServer();//三菱PLC
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//StartSiemensPlcServer();//西门子
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 心跳
|
|
|
|
|
public Task StartPlcAsync()
|
|
|
|
|
{
|
|
|
|
|
//timer.Elapsed += new System.Timers.ElapsedEventHandler(ExecSiemensHeartTask);
|
|
|
|
|
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;
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Task StartMelsecMcPlcServer()
|
|
|
|
|
{
|
|
|
|
|
// string ip = Appsettings.app("Middleware", "Scanner1", "Ip");
|
|
|
|
|
var allPlcServices = Appsettings.app<PlcModel>("PLCServer").ToList();
|
|
|
|
|
foreach (var item in allPlcServices)
|
|
|
|
|
{
|
|
|
|
|
PlcModel model = new PlcModel();
|
|
|
|
|
model.Id = item.Id;
|
|
|
|
|
model.EquipName = item.EquipName;
|
|
|
|
|
model.IP = item.IP;
|
|
|
|
|
model.Port = item.Port;
|
|
|
|
|
model.plc = new MelsecPlc(item.IP, item.Port);
|
|
|
|
|
// model.IsConnect = model.plc.IsConnected;
|
|
|
|
|
PlcHelper.melsecList.Add(model);
|
|
|
|
|
}
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void StartSiemensPlcServer()
|
|
|
|
|
{
|
|
|
|
|
var allPlcServices = Appsettings.app<PlcModel>("PLCServer").ToList();
|
|
|
|
|
foreach (var item in allPlcServices)
|
|
|
|
|
{
|
|
|
|
|
PlcModel model = new PlcModel();
|
|
|
|
|
model.Id = item.Id;
|
|
|
|
|
model.EquipName = item.EquipName;
|
|
|
|
|
model.IP = item.IP;
|
|
|
|
|
model.Port = item.Port;
|
|
|
|
|
model.plc = new SiemensPlc(item.IP, item.Port);
|
|
|
|
|
PlcHelper.siemensList.Add(model);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 心跳检测
|
|
|
|
|
private async void ExecMelsecMcHeartTask(object? sender, ElapsedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in PlcHelper.melsecList)
|
|
|
|
|
{
|
|
|
|
|
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.IsConnect = r;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private async void ExecSiemensHeartTask(object? sender, ElapsedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in PlcHelper.siemensList)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|