修改PLC 定时器位置

dev
liulb@mesnac.com 1 year ago
parent d86071f8dc
commit 93588129a1

@ -19,7 +19,8 @@ namespace Admin.Core.Extensions
{
if (Appsettings.app("Middleware", "Plc", "Enabled").ObjToBool())
{
await plc.StartPlcAsync();
await plc.StartMelsecPlcAsync();
await plc.StartSiemensPlcAsync();
}
}
catch (Exception e)

@ -1,8 +1,9 @@

namespace Aucma.Core.RunPlc
{
public interface IRunPlcService
public interface IRunPlcService
{
Task StartPlcAsync();
Task StartMelsecPlcAsync();
Task StartSiemensPlcAsync();
}
}

@ -11,42 +11,44 @@ namespace Aucma.Core.RunPlc
/// </summary>
public class RunPlcService : IRunPlcService
{
System.Timers.Timer timer = new System.Timers.Timer(3000);//创建定时器设置间隔时间为1000毫秒
System.Timers.Timer timer1 = new System.Timers.Timer(3000);
#region 构造函数
public RunPlcService()
{
StartMelsecMcPlcServer();
StartSiemensPlcServer();
//StartMelsecMcPlcServer();
//StartSiemensPlcServer();
}
#endregion
#region 心跳
public Task StartPlcAsync()
public async Task StartMelsecPlcAsync()
{
System.Timers.Timer timer = new System.Timers.Timer(3000);
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;
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;
return Task.CompletedTask;
await Task.CompletedTask;
}
public async Task StartSiemensPlcAsync()
{
System.Timers.Timer timer = new System.Timers.Timer(3000);//创建定时器设置间隔时间为1000毫秒
timer.Elapsed += new System.Timers.ElapsedEventHandler(ExecSiemensHeartTask);
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 void StartMelsecMcPlcServer()
{
var allPlcServices = Appsettings.app<PlcModel>("PLCServer").ToList();
if (allPlcServices == null) return ;
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.Id = list[i - 1].Id;
model.EquipName = list[i - 1].EquipName;
model.IP = list[i - 1].IP;
model.Port = list[i - 1].Port;
@ -54,15 +56,15 @@ namespace Aucma.Core.RunPlc
model.plc = new MelsecPlc(list[i - 1].IP, list[i - 1].Port);
PlcHelper.melsecList.Add(model);
}
// return Task.CompletedTask;
// return Task.CompletedTask;
}
private static void StartSiemensPlcServer()
{
List<PlcModel> allPlcServices = Appsettings.app<PlcModel>("PLCServer").ToList();
if (allPlcServices == null) return ;
var list = allPlcServices.FindAll(d => d.PlcType=="Siemens" && d.Enabled == true);
if (allPlcServices == null) return;
var list = allPlcServices.FindAll(d => d.PlcType == "Siemens" && d.Enabled == true);
foreach (var item in list)
{
PlcModel model = new PlcModel();
@ -74,7 +76,7 @@ namespace Aucma.Core.RunPlc
model.plc = new SiemensPlc(item.IP, item.Port);
PlcHelper.siemensList.Add(model);
}
}
#endregion
@ -87,8 +89,8 @@ namespace Aucma.Core.RunPlc
{
var item = PlcHelper.melsecList.Where(c => c.Id == i).FirstOrDefault();
if (item == null) continue;
if (item.plc.Read("D6050").Result)
var test = item.plc.ReadInt16("D6050");
if (item.plc.Read("M100").Result)
{
item.IsConnect = true;
item.plc.IsConnected = true;
@ -126,7 +128,7 @@ namespace Aucma.Core.RunPlc
{
var item = PlcHelper.siemensList.Where(c => c.Id == i).FirstOrDefault();
if (item == null) continue;
if (await item.plc.Read("M100"))
{
item.IsConnect = true;

@ -1,4 +1,6 @@
using Admin.Core.IService;
using Admin.Core.Common;
using Admin.Core.IService;
using Admin.Core.Model;
using Aucma.Core.HwPLc;
using Microsoft.Extensions.DependencyInjection;
using NetTaste;
@ -26,10 +28,9 @@ namespace Aucma.Core.SheetMetal.Business
{
if (PlcHelper.melsecList!=null)
{
melsecPlc = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("BackPanelPLC"));
do
{
melsecPlc = PlcHelper.melsecList.FirstOrDefault(d => d.EquipName.Equals("BackPanelPLC"));
if (melsecPlc.plc.IsConnected)
{
byte[] info = melsecPlc.plc.Read("D6030", 100);
@ -39,9 +40,16 @@ namespace Aucma.Core.SheetMetal.Business
continue;
}
//读取
string planCode = Encoding.ASCII.GetString(info.Skip(0).Take(20).ToArray()).Replace("\0", "").Trim();
//string model = Encoding.ASCII.GetString(info.Skip(0).Take(20).ToArray()).Replace("\0", "").Trim();
//string productId = Encoding.ASCII.GetString(info.Skip(20).Take(20).ToArray()).Replace("\0", "").Trim();
//int complateAmount = short.Parse(bytesToHexStr(info.Skip(40).Take(1).ToArray(), 1), System.Globalization.NumberStyles.HexNumber);
//SmSyncModel syncModel = new SmSyncModel();
//SmSyncModel smSyncModel = _smSyncModelServices.FirstAsync(d => d.SmProductId == productId).Result;
//if (smSyncModel.IsNotEmptyOrNull())
//{
// _smSyncModelServices.AddAsync(model);
//}
//_smSyncModelServices.AddAsync(model);
}
} while (true);
}

Loading…
Cancel
Save