上位机连接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;
}
}

浙公网安备 33010602011771号