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;
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="name"></param>
|
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["型号"];
|
variable = CommunicatorConnections["变量地址"].ToString();
|
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
|
{
|
if (plc!=null)
|
{
|
if (string.IsNullOrEmpty(variable))
|
{
|
variable = CommunicatorConnections["变量地址"].ToString();
|
}
|
plc.Write(variable, message);
|
return true;
|
}
|
else
|
{
|
return false;
|
}
|
}
|
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);
|
}
|
}
|
}
|
}
|