Files
serein-flow/Net462DllTest/LogicControl/PlcLogicControl.cs

153 lines
4.5 KiB
C#
Raw Normal View History

using IoTClient.Common.Enums;
using Net462DllTest.Enums;
using Net462DllTest.Model;
2024-10-07 15:15:18 +08:00
using Net462DllTest.Trigger;
using Serein.Library;
2024-09-28 23:55:19 +08:00
using Serein.Library.Api;
using System;
using System.Threading.Tasks;
namespace Net462DllTest.LogicControl
2024-09-28 23:55:19 +08:00
{
[AutoRegister]
[DynamicFlow("[SiemensPlc]")]
public class PlcLogicControl
2024-09-28 23:55:19 +08:00
{
public Guid HandleGuid { get; } = new Guid();
private readonly SiemensPlcDevice MyPlc;
private readonly PlcVarModelDataProxy plcVarModelDataProxy;
2024-09-28 23:55:19 +08:00
public PlcLogicControl(SiemensPlcDevice MyPlc,
PlcVarModelDataProxy plcVarModelDataProxy)
{
this.MyPlc = MyPlc;
this.plcVarModelDataProxy = plcVarModelDataProxy;
}
2024-09-28 23:55:19 +08:00
#region
2024-09-28 23:55:19 +08:00
[NodeAction(NodeType.Loading)] // Loading 初始化完成已注入依赖项,可以开始逻辑上的操作
public void Loading(IDynamicContext context)
{
2024-09-28 23:55:19 +08:00
}
[NodeAction(NodeType.Exit)] // 流程结束时自动执行
public void Exit(IDynamicContext context)
{
2024-10-07 15:15:18 +08:00
MyPlc.Close();
MyPlc.CancelAllTrigger();
2024-09-28 23:55:19 +08:00
}
#endregion
#region
2024-10-11 16:46:16 +08:00
[NodeAction(NodeType.Flipflop, "等待变量更新")]
2024-10-11 19:31:34 +08:00
public async Task<IFlipflopContext<object>> WaitTask(PlcVarName varName = PlcVarName.ErrorCode)
2024-09-28 23:55:19 +08:00
{
try
{
var triggerData = await MyPlc.WaitTriggerAsync<object>(varName);
2024-10-11 16:46:16 +08:00
await Console.Out.WriteLineAsync($"PLC变量触发器[{varName}]传递数据:{triggerData}");
2024-10-11 19:31:34 +08:00
return new FlipflopContext<object>(FlipflopStateType.Succeed, triggerData);
2024-09-28 23:55:19 +08:00
}
catch (Exception)
{
2024-10-11 16:46:16 +08:00
throw;
2024-09-28 23:55:19 +08:00
}
}
#endregion
#region
[NodeAction(NodeType.Action, "等待")]
public async Task Delay(int ms = 5000)
{
await Console.Out.WriteLineAsync("开始等待");
await Task.Delay(ms);
await Console.Out.WriteLineAsync("不再等待");
}
2024-10-07 15:15:18 +08:00
[NodeAction(NodeType.Action, "PLC初始化")]
2024-09-28 23:55:19 +08:00
public SiemensPlcDevice PlcInit(SiemensVersion version = SiemensVersion.None,
string ip = "192.168.10.100",
int port = 102)
{
2024-10-11 16:46:16 +08:00
//MyPlc.Model.Set(PlcVarName.DoorVar,(Int16)1);
//MyPlc.Model.Get(PlcVarName.DoorVar);
2024-09-28 23:55:19 +08:00
if (MyPlc.Client is null)
{
try
{
MyPlc.Init(version, ip, port);
Console.WriteLine($"西门子PLC初始化成功[{version},{ip}:{port}]");
}
catch (Exception ex)
{
Console.WriteLine($"西门子PLC[{version},{ip}:{port}]初始化异常:{ex.Message}");
}
}
else
{
Console.WriteLine( $"西门子PLC已经初始化[{version},{ip}:{port}]");
}
return MyPlc;
}
[NodeAction(NodeType.Action, "设置PLC状态")]
public SiemensPlcDevice SetState(PlcState state = PlcState.PowerOff)
{
2024-10-07 15:15:18 +08:00
var oldState = MyPlc.State;
MyPlc.State = state;
Console.WriteLine($"PLC状态从[{oldState}]转为[{state}]");
return MyPlc;
2024-09-28 23:55:19 +08:00
}
2024-09-28 23:55:19 +08:00
[NodeAction(NodeType.Action, "PLC获取变量")]
public object ReadVar(PlcVarName varName)
2024-09-28 23:55:19 +08:00
{
var result = MyPlc.Read(varName);
Console.WriteLine($"获取变量成功:({varName})\t result = {result}");
2024-09-28 23:55:19 +08:00
return result;
}
2024-09-28 23:55:19 +08:00
[NodeAction(NodeType.Action, "PLC写入变量")]
public SiemensPlcDevice WriteVar(object value, PlcVarName varName)
2024-09-28 23:55:19 +08:00
{
MyPlc.Write(varName, value); // 新数据
2024-09-28 23:55:19 +08:00
return MyPlc;
}
2024-10-07 15:15:18 +08:00
[NodeAction(NodeType.Action, "批量读取")]
public PlcVarModelDataProxy BatchReadVar()
2024-09-28 23:55:19 +08:00
{
2024-10-11 16:46:16 +08:00
//MyPlc.BatchRefresh();
return plcVarModelDataProxy;
2024-10-07 15:15:18 +08:00
}
2024-10-07 15:15:18 +08:00
[NodeAction(NodeType.Action, "开启定时刷新")]
public void OpenTimedRefresh()
{
Task.Run(async () => await MyPlc.OpenTimedRefreshAsync());
2024-09-28 23:55:19 +08:00
}
2024-10-07 15:15:18 +08:00
[NodeAction(NodeType.Action, "关闭定时刷新")]
public void CloseTimedRefresh()
{
MyPlc.CloseTimedRefresh();
}
2024-09-28 23:55:19 +08:00
#endregion
}
}