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.
|
|
|
|
|
|
|
|
|
using Admin.Core.Common;
|
|
|
|
|
using Aucma.Core.MelsecPLc;
|
|
|
|
|
using System.Timers;
|
|
|
|
|
|
|
|
|
|
namespace Aucma.Core.Melsec
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 扫码器初始化
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class MelsecService : IMelsecService
|
|
|
|
|
{
|
|
|
|
|
private IMelsecPlc _melsecPlc;
|
|
|
|
|
System.Timers.Timer timer = new System.Timers.Timer(1000);//创建定时器,设置间隔时间为1000毫秒;
|
|
|
|
|
public MelsecService(IMelsecPlc melsecPlc)
|
|
|
|
|
{
|
|
|
|
|
_melsecPlc = melsecPlc;
|
|
|
|
|
}
|
|
|
|
|
public Task StartMelsecAsync()
|
|
|
|
|
{
|
|
|
|
|
timer.Elapsed += new System.Timers.ElapsedEventHandler(ExecTask); //到达时间的时候执行事件;
|
|
|
|
|
timer.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
|
|
|
|
|
timer.Enabled = true;//需要调用 timer.Start()或者timer.Enabled = true来启动它,
|
|
|
|
|
timer.Start();//timer.Start()的内部原理还是设置timer.Enabled = true;
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ExecTask(object? sender, ElapsedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_melsecPlc.Read("M100"))
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(_melsecPlc.ReadBool("M100"));
|
|
|
|
|
Console.WriteLine("PLC连接成功!");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!_melsecPlc.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("PLC连接失败!");
|
|
|
|
|
System.GC.Collect();
|
|
|
|
|
if (_melsecPlc.DisConnect())
|
|
|
|
|
{
|
|
|
|
|
string address = Appsettings.app("Middleware","Melsec", "ConString").ToString();
|
|
|
|
|
int port = Appsettings.app("Middleware", "Melsec", "Port").ObjToInt();
|
|
|
|
|
_melsecPlc.Connect(address, port);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|