using LB_SmartVisionCommon;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LB_VisionProcesses.Communicators.SiemensS7
{
public class SiemensLBS7 : BaseCommunicator
{
private Plc plc;
public string variable = string.Empty;
///
///
///
///
public SiemensLBS7(string name = "西门子S7")
{
CommunicatorConnections.Add("地址", "127.0.0.1");
CommunicatorConnections.Add("端口", "1");
CommunicatorConnections.Add("型号", S7.Net.CpuType.S71500);
CommunicatorBrand = CommunicatorBrand.SiemensS7;
CommunicatorName = name;
}
public override bool Connect()
{
try
{
string IP = CommunicatorConnections["地址"].ToString();
short slot;
short.TryParse(CommunicatorConnections["端口"].ToString(), out slot);
S7.Net.CpuType cpuType = (CpuType)CommunicatorConnections["型号"];
plc = new Plc(cpuType, IP, 0, slot);
plc.Open();
return true;
}
catch
{
return false;
}
}
public override bool Disconnect()
{
try
{
plc?.Close();
return true;
}
catch
{
return false;
}
}
public override bool SendMessage(string message)
{
try
{
plc.Write(variable, message);
return true;
}
catch
{
return false;
}
}
public override void Dispose()
{
try
{
AsyncLogHelper.Info($"Device:[{CommunicatorName}],Dispose()");
plc = null;
// Suppress finalization.
GC.SuppressFinalize(this);
}
catch (Exception ex)
{
AsyncLogHelper.Error($"Device:[{CommunicatorName}],Dispose(),Error" + ex);
}
}
}
}