上位机连接PLC

上位机使用Hsl框架连接PLC
顺便讲下策略模式
话不多说,直接上代码

public interface IPlcHost
{
    bool ConnectionPlc(string path);
}

public class FastPlcHost : IPlcHost
{
    private SiemensS7Net client;
    public FastPlcHost()
    {
        client = new SiemensS7Net(SiemensPLCS.S1500)
        {
            ConnectTimeOut = 5000,
            ReceiveTimeOut = 5000
        };
    }
    public bool ConnectionPlc(string path)
    {
        client.IpAddress = path;
        client.SetPersistentConnection();
        var result = client.ConnectServer();
        if (result.IsSuccess) 
        {
          return true;
        }
        return false;
    }
}

public static class PlcProvider
{
    public static IPlcHost Default { get; set; } = new FastPlcHost();

    public static bool ConnectionPlc(this PlcData plcData)
    {
        return Default.ConnectionPlc(plcData.Ip);
    }
}
public class PlcData
{
    public string Ip { get; set; }
    public PlcData()
    {
        Ip = "127.0.0.1";
    }
}
public class Test
{
    public void ShowPlc()
    {
        PlcData plcData = new PlcData();
        var b= PlcProvider.ConnectionPlc(plcData);
        if (!b) return;
       
    }
}

本文作者:孤沉

本文链接:https://www.cnblogs.com/guchen33/p/17976365

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   孤沉  阅读(43)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开