Files
serein-flow/Net461DllTest/Device/SiemensPlcDevice.cs

73 lines
2.0 KiB
C#
Raw Normal View History

2024-09-28 23:55:19 +08:00
using IoTClient.Clients.PLC;
using Net461DllTest.Enums;
using Net461DllTest.Signal;
using Net461DllTest.Utils;
using Serein.Library.Attributes;
2024-09-28 23:55:19 +08:00
using Serein.Library.NodeFlow.Tool;
using System;
namespace Net461DllTest.Device
{
/// <summary>
/// 官方文档如果没有主动Open则会每次读写操作的时候自动打开自动和关闭连接这样会使读写效率大大减低。所以建议手动Open和Close。
/// </summary>
[AutoRegister]
2024-09-28 23:55:19 +08:00
public class SiemensPlcDevice : ChannelFlowTrigger<OrderSignal>
{
public SiemensClient Client { get; set; }
public IoTClient.Common.Enums.SiemensVersion Version { get; set; }
public string IP { get; set; }
public int Port { get; set; }
public PlcState State { get; set; } = PlcState.PowerOff;
public void Init(IoTClient.Common.Enums.SiemensVersion version,string ip, int port)
{
Client = new SiemensClient(version, ip, port);
Version = version;
IP = ip;
Port = port;
}
public void ResetDevice()
{
Client?.Close();
Client = null;
}
public void Write(PlcVarInfo plcValue, object value)
{
try
{
Client.WriteToPlcValue(plcValue, value);
}
catch (Exception ex)
{
Console.WriteLine($"写入出错:{this}{plcValue}。{ex.Message}");
throw;
}
}
public object Read(PlcVarInfo plcValue)
{
try
{
return Client.ReadToPlcValue(plcValue);
}
catch (Exception ex)
{
Console.WriteLine($"读取出错:{this}{plcValue}。{ex.Message}");
throw;
}
}
public override string ToString()
{
return $"西门子Plc[{this.Version}-{this.IP}:{this.Port}]";
}
}
}