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.

61 lines
2.0 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 Admin.Core.PlcServer;
using log4net;
using S71500.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Admin.Core.Plc
{
public class S71500Service : IS71500Service
{
bool flag = true;
public S71500Service()
{
string address = Appsettings.app("Siemens", "ConString").ToString();
int port = Appsettings.app("Siemens", "Port").ObjToInt();
Adapter.Registe();
Adapter.Connect(address, port);
}
public Task AddPlcAsync()
{
System.Timers.Timer timer = new System.Timers.Timer(5000);//创建定时器设置间隔时间为1000毫秒
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 source, System.Timers.ElapsedEventArgs e)
{
if (Adapter.Device_GetState("DB110.DBW24"))
{
Console.WriteLine("PLC连接成功");
}
else
{
if (!Adapter.IsConnected)
{
Console.WriteLine("PLC连接失败");
if (Adapter.Device_Destroy())
{
string address = Appsettings.app("Siemens", "ConString").ToString();
int port = Appsettings.app("Siemens", "Port").ObjToInt();
Adapter.Connect(address, port);
}
}
}
}
}
}