change - 将PLC进行抽象,实现工厂模式

master
wenjy 10 months ago
parent 4c13ac9cfd
commit c9e63d0d16

@ -0,0 +1,324 @@
using System;
using HslCommunication;
using HslCommunication.Profinet.Inovance;
using SlnMesnac.Common;
namespace SlnMesnac.Plc.Factory
{
public class InovanceFactory:PlcAbsractFactory
{
private StringChange _stringChange;
private InovanceTcpNet inovanceTcp = null;
public InovanceFactory(StringChange stringChange)
{
_stringChange = stringChange;
this.inovanceTcp = new InovanceTcpNet();
this.inovanceTcp.ConnectTimeOut = 2000;
}
public override bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public override bool Connect(string ip, int port)
{
try
{
inovanceTcp?.ConnectClose();
if (inovanceTcp != null)
{
inovanceTcp.IpAddress = ip;
inovanceTcp.Port = port;
inovanceTcp.DataFormat = HslCommunication.Core.DataFormat.CDAB;
OperateResult connect = inovanceTcp.ConnectServer();
this.IsConnected = connect.IsSuccess;
if (!connect.IsSuccess)
{
throw new InvalidOperationException($"汇川PLC连接失败:{connect.Message}");
}
return connect.IsSuccess;
}
else
{
throw new ArgumentException($"汇川PLC实例inovanceTcp为null");
}
}
catch (Exception ex)
{
throw new InvalidOperationException($"汇川PLC连接异常{ex.Message}");
}
}
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public override bool DisConnect()
{
try
{
OperateResult disConnect = inovanceTcp.ConnectClose();
this.IsConnected = false;
if (!disConnect.IsSuccess)
{
throw new InvalidOperationException($"汇川PLC断开连接失败:{disConnect.Message}");
}
return disConnect.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"汇川PLC断开连接异常{ex.Message}");
}
}
/// <summary>
/// 根据地址读取指定长度数据
/// </summary>
/// <param name="address"></param>
/// <param name="len"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override byte[] readValueByAddress(string address, int len)
{
try
{
OperateResult<byte[]> read = inovanceTcp.Read(address, (ushort)(len));
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取指定长度数据失败:{read.Message}");
}
return _stringChange.ConvertFloatToINt(read.Content);
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取指定长度数据异常:{ex.Message}");
}
}
/// <summary>
/// 根据地址读取int16数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override int readInt16ByAddress(string address)
{
try
{
OperateResult<short> read = inovanceTcp.ReadInt16(address);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取int16数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取int16数据异常{ex.Message}");
}
}
/// <summary>
/// 根据地址写入int16数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeInt16ByAddress(string address, int value)
{
try
{
OperateResult operateResult = new OperateResult();
int s = 0;
string[] strArry = address.Split('.');
//先读取整个块的内容
var info = inovanceTcp.ReadInt16(strArry[0]);
if (info.Content == 0)
{
int length = _stringChange.ParseToInt(strArry[1]) + 1;
string[] array = new string[length];
for (int i = 0; i < length; i++)
{
if (i == _stringChange.ParseToInt(strArry[1]))
{
array[i] = value.ToString();
}
else
{
array[i] = "0";
}
}
//反转
Array.Reverse(array);
byte[] buffer = new byte[array.Length];
string result = "";
for (int i = 0; i < array.Length; i++)
{
result += (byte)Convert.ToInt32(array[i], 16);
}
s = Convert.ToInt32(result.Trim(), 2);
operateResult = inovanceTcp.Write(strArry[0], (ushort)s);
}
else
{
var inf2 = Convert.ToString(info.Content, 2);
string[] infoArray = new string[inf2.Length];
for (int i = 0; i < inf2.Length; i++)
{
infoArray[i] = inf2.Substring(i, 1);
}
Array.Reverse(infoArray);
infoArray[_stringChange.ParseToInt(strArry[1])] = value.ToString();
string result = "";
foreach (var item in infoArray)
{
result = result + item;
}
s = Convert.ToInt32(result.Trim(), 10);
operateResult = inovanceTcp.Write(strArry[0], s);
}
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入int16数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入int16数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="length"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override string readStringByAddress(string address, ushort length)
{
try
{
OperateResult<String> read = inovanceTcp.ReadString(address, length);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取string数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取string数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeStringByAddress(string address, string value)
{
try
{
OperateResult operateResult = inovanceTcp.Write(address, value);
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入string数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入string数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool readBoolByAddress(string address)
{
try
{
OperateResult<bool> read = inovanceTcp.ReadBool(address);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取bool数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取bool数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeBoolByAddress(string address, bool value)
{
try
{
OperateResult operateResult = inovanceTcp.Write(address, short.Parse(_stringChange.ParseToInt(value ? "1" : "0").ToString()));
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入bool数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入bool数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址写入Double类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeDoubleByAddress(string address, int value)
{
try
{
OperateResult operateResult = inovanceTcp.Write(address, Convert.ToDouble(value));
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入double数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入double数据异常{ex.Message}");
}
}
}
}

@ -0,0 +1,265 @@
using System;
using HslCommunication;
using HslCommunication.Profinet.Melsec;
using SlnMesnac.Common;
namespace SlnMesnac.Plc.Factory
{
public class MelsecBinaryFactory:PlcAbsractFactory
{
private StringChange _stringChange;
private MelsecMcNet melsec_net = null;
public MelsecBinaryFactory(StringChange stringChange)
{
_stringChange = stringChange;
this.melsec_net = new MelsecMcNet();
this.melsec_net.ConnectTimeOut = 2000;
}
public override bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="iP"></param>
/// <param name="port"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool Connect(string iP, int port)
{
try
{
melsec_net.IpAddress = iP;
melsec_net.Port = port;
OperateResult connect = melsec_net.ConnectServer();
this.IsConnected = connect.IsSuccess;
if (!connect.IsSuccess)
{
throw new InvalidOperationException($"三菱PLC连接失败:{connect.Message}");
}
return connect.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"三菱PLC连接异常{ex.Message}");
}
}
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool DisConnect()
{
try
{
OperateResult disConnect = melsec_net.ConnectClose();
this.IsConnected = false;
if (!disConnect.IsSuccess)
{
throw new InvalidOperationException($"三菱PLC断开连接失败:{disConnect.Message}");
}
return disConnect.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"三菱PLC断开连接异常{ex.Message}");
}
}
/// <summary>
/// 根据地址读取指定长度数据
/// </summary>
/// <param name="address"></param>
/// <param name="len"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override byte[] readValueByAddress(string address, int len)
{
try
{
OperateResult<byte[]> read = melsec_net.Read(address, (ushort)(len));
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取指定长度数据失败:{read.Message}");
}
return _stringChange.ConvertFloatToINt(read.Content);
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取指定长度数据异常:{ex.Message}");
}
}
/// <summary>
/// 根据地址读取int16数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override int readInt16ByAddress(string address)
{
try
{
OperateResult<short> read = melsec_net.ReadInt16(address);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取int16数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取int16数据异常{ex.Message}");
}
}
/// <summary>
/// 根据地址写入int16数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeInt16ByAddress(string address, int value)
{
try
{
OperateResult operateResult = melsec_net.Write(address, Convert.ToInt16(value));
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入int16数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入int16数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="length"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override string readStringByAddress(string address, ushort length)
{
try
{
OperateResult<String> read = melsec_net.ReadString(address, length);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取string数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取string数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeStringByAddress(string address, string value)
{
try
{
OperateResult operateResult = melsec_net.Write(address, value);
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入string数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入string数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool readBoolByAddress(string address)
{
try
{
OperateResult<bool> read = melsec_net.ReadBool(address);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取bool数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取bool数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeBoolByAddress(string address, bool value)
{
try
{
OperateResult operateResult = melsec_net.Write(address, short.Parse(_stringChange.ParseToInt(value ? "1" : "0").ToString()));
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入bool数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入bool数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址写入Double类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeDoubleByAddress(string address, int value)
{
try
{
OperateResult operateResult = melsec_net.Write(address, Convert.ToDouble(value));
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入double数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入double数据异常{ex.Message}");
}
}
}
}

@ -0,0 +1,268 @@
using System;
using HslCommunication;
using HslCommunication.Profinet.Omron;
using SlnMesnac.Common;
namespace SlnMesnac.Plc.Factory
{
public class OmronNJFactory:PlcAbsractFactory
{
private StringChange _stringChange;
private OmronFinsNet omronFinsNet = null;
public OmronNJFactory(StringChange stringChange)
{
_stringChange = stringChange;
this.omronFinsNet = new OmronFinsNet();
this.omronFinsNet.ConnectTimeOut = 2000;
}
public override bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="iP"></param>
/// <param name="port"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool Connect(string iP, int port)
{
try
{
omronFinsNet.IpAddress = iP;
omronFinsNet.Port = 9600;
omronFinsNet.SA1 = (byte)192;
omronFinsNet.DA1 = (byte)239;
omronFinsNet.DA2 = (byte)0;
OperateResult connect = omronFinsNet.ConnectServer();
this.IsConnected = connect.IsSuccess;
if (!connect.IsSuccess)
{
throw new InvalidOperationException($"欧姆龙PLC连接失败:{connect.Message}");
}
return connect.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"欧姆龙PLC连接异常{ex.Message}");
}
}
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool DisConnect()
{
try
{
OperateResult disConnect = omronFinsNet.ConnectClose();
this.IsConnected = false;
if (!disConnect.IsSuccess)
{
throw new InvalidOperationException($"欧姆龙PLC断开连接失败:{disConnect.Message}");
}
return disConnect.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"欧姆龙PLC断开连接异常{ex.Message}");
}
}
/// <summary>
/// 根据地址读取指定长度数据
/// </summary>
/// <param name="address"></param>
/// <param name="len"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override byte[] readValueByAddress(string address, int len)
{
try
{
OperateResult<byte[]> read = omronFinsNet.Read(address, (ushort)(len));
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取指定长度数据失败:{read.Message}");
}
return _stringChange.ConvertFloatToINt(read.Content);
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取指定长度数据异常:{ex.Message}");
}
}
/// <summary>
/// 根据地址读取int16数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override int readInt16ByAddress(string address)
{
try
{
OperateResult<short> read = omronFinsNet.ReadInt16(address);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取int16数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取int16数据异常{ex.Message}");
}
}
/// <summary>
/// 根据地址写入int16数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeInt16ByAddress(string address, int value)
{
try
{
OperateResult operateResult = omronFinsNet.Write(address, Convert.ToInt16(value));
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入int16数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入int16数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="length"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override string readStringByAddress(string address, ushort length)
{
try
{
OperateResult<String> read = omronFinsNet.ReadString(address, length);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取string数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取string数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeStringByAddress(string address, string value)
{
try
{
OperateResult operateResult = omronFinsNet.Write(address, value);
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入string数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入string数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool readBoolByAddress(string address)
{
try
{
OperateResult<bool> read = omronFinsNet.ReadBool(address);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取bool数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取bool数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeBoolByAddress(string address, bool value)
{
try
{
OperateResult operateResult = omronFinsNet.Write(address, short.Parse(_stringChange.ParseToInt(value ? "1" : "0").ToString()));
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入bool数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入bool数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址写入Double类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeDoubleByAddress(string address, int value)
{
try
{
OperateResult operateResult = omronFinsNet.Write(address, Convert.ToDouble(value));
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入double数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入double数据异常{ex.Message}");
}
}
}
}

@ -0,0 +1,265 @@
using System;
using HslCommunication;
using HslCommunication.Profinet.Siemens;
using SlnMesnac.Common;
namespace SlnMesnac.Plc.Factory
{
public class SiemensFactory:PlcAbsractFactory
{
private StringChange _stringChange;
private const SiemensPLCS type = SiemensPLCS.S200Smart;
private SiemensS7Net s7 = new SiemensS7Net(type);
public SiemensFactory(StringChange stringChange)
{
_stringChange = stringChange;
}
public override bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="iP"></param>
/// <param name="port"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool Connect(string iP, int port)
{
try
{
s7.IpAddress = iP;
s7.Port = 102;
OperateResult connect = s7.ConnectServer();
this.IsConnected = connect.IsSuccess;
if (!connect.IsSuccess)
{
throw new InvalidOperationException($"西门子S系列PLC连接失败:{connect.Message}");
}
return connect.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"西门子S系列PLC连接异常{ex.Message}");
}
}
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool DisConnect()
{
try
{
OperateResult disConnect = s7.ConnectClose();
this.IsConnected = false;
if (!disConnect.IsSuccess)
{
throw new InvalidOperationException($"西门子S系列PLC断开连接失败:{disConnect.Message}");
}
return disConnect.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"西门子S系列PLC断开连接异常{ex.Message}");
}
}
/// <summary>
/// 根据地址读取指定长度数据
/// </summary>
/// <param name="address"></param>
/// <param name="len"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override byte[] readValueByAddress(string address, int len)
{
try
{
OperateResult<byte[]> read = s7.Read(address, (ushort)(len));
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取指定长度数据失败:{read.Message}");
}
return _stringChange.ConvertFloatToINt(read.Content);
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取指定长度数据异常:{ex.Message}");
}
}
/// <summary>
/// 根据地址读取int16数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override int readInt16ByAddress(string address)
{
try
{
OperateResult<short> read = s7.ReadInt16(address);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取int16数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取int16数据异常{ex.Message}");
}
}
/// <summary>
/// 根据地址写入int16数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeInt16ByAddress(string address, int value)
{
try
{
OperateResult operateResult = s7.Write(address, Convert.ToInt16(value));
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入int16数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入int16数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="length"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override string readStringByAddress(string address, ushort length)
{
try
{
OperateResult<String> read = s7.ReadString(address, length);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取string数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取string数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeStringByAddress(string address, string value)
{
try
{
OperateResult operateResult = s7.Write(address, value);
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入string数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入string数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool readBoolByAddress(string address)
{
try
{
OperateResult<bool> read = s7.ReadBool(address);
if (!read.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};读取bool数据失败{read.Content}");
}
return read.Content;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};读取bool数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeBoolByAddress(string address, bool value)
{
try
{
OperateResult operateResult = s7.Write(address, short.Parse(_stringChange.ParseToInt(value ? "1" : "0").ToString()));
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入bool数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入bool数据异常{ex.Message}");
}
}
/// <summary>
/// 通过PLC地址写入Double类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override bool writeDoubleByAddress(string address, int value)
{
try
{
OperateResult operateResult = s7.Write(address, Convert.ToDouble(value));
if (!operateResult.IsSuccess)
{
throw new InvalidOperationException($"根据地址:{address};写入double数据失败{operateResult.Message}");
}
return operateResult.IsSuccess;
}
catch (Exception ex)
{
throw new InvalidOperationException($"根据地址:{address};写入double数据异常{ex.Message}");
}
}
}
}

@ -1,401 +0,0 @@
using HslCommunication.Profinet.Inovance;
using HslCommunication;
using System;
using System.Collections.Generic;
using System.Text;
using SlnMesnac.Common;
using Microsoft.Extensions.Logging;
namespace SlnMesnac.Plc.Impl
{
/// <summary>
/// 汇川PLC
/// </summary>
public class InovancePlc : IPlc
{
private ILogger<InovancePlc> _logger;
private StringChange _stringChange;
private InovanceTcpNet inovanceTcp = null;
public InovancePlc(ILogger<InovancePlc> logger, StringChange stringChange)
{
this._logger = logger;
_stringChange = stringChange;
this.inovanceTcp = new InovanceTcpNet();
this.inovanceTcp.ConnectTimeOut = 2000;
}
public bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <returns></returns>
public bool Connect(string IP, int port)
{
inovanceTcp?.ConnectClose();
PrintLogInfo("汇川PLC连接开始");
inovanceTcp.IpAddress = IP;
inovanceTcp.Port = 502;
inovanceTcp.DataFormat = HslCommunication.Core.DataFormat.CDAB;
try
{
OperateResult connect = inovanceTcp.ConnectServer();
if (connect.IsSuccess)
{
this.IsConnected = true;
PrintLogInfo("汇川PLC建立连接成功");
return true;
}
else
{
this.IsConnected = false;
PrintLogInfo($"汇川PLC建立连接失败:{connect.Message}");
return false;
}
}
catch (Exception ex)
{
this.IsConnected = false;
PrintLogInfo("汇川PLC建立连接异常", ex);
return false;
}
}
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
public bool DisConnect()
{
return inovanceTcp.ConnectClose().IsSuccess;
}
/// <summary>
/// 通过地址和长度读取PLC数据
/// </summary>
/// <param name="len"></param>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public byte[] readValueByAddress(int len, string address)
{
PrintLogInfo($"开始通过地址:{address},读取长度:{len}的PLC数据");
try
{
OperateResult<byte[]> read = inovanceTcp.Read(address, (ushort)(len));
if (read.IsSuccess)
{
byte[] result = _stringChange.ConvertFloatToINt(read.Content);
PrintLogInfo(String.Format("通过地址和长度读取PLC数据成功{0}", _stringChange.bytesToHexStr(result, result.Length)));
return result;
}
else
{
PrintLogInfo($"通过地址和长度读取PLC数据失败:{read.Message}");
this.IsConnected = false;
return new byte[0];
}
}
catch (Exception ex)
{
PrintLogInfo("通过地址和长度读取PLC数据异常", ex);
this.IsConnected = false;
return new byte[0];
}
}
/// <summary>
/// 通过PLC地址写入int类型数据,模切汇川PLC复位逻辑
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeValueByAddress(string address,int value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}", address, value));
OperateResult operateResult = new OperateResult();
try
{
int s = 0;
string[] strArry = address.Split('.');
//先读取整个块的内容
var info = inovanceTcp.ReadInt16(strArry[0]);
if (info.Content == 0)
{
int length = _stringChange.ParseToInt(strArry[1]) + 1;
string[] array = new string[length];
for (int i = 0; i < length; i++)
{
if (i == _stringChange.ParseToInt(strArry[1]))
{
array[i] = value.ToString();
}
else
{
array[i] = "0";
}
}
//反转
Array.Reverse(array);
byte[] buffer = new byte[array.Length];
string result = "";
for (int i = 0; i < array.Length; i++)
{
result += (byte)Convert.ToInt32(array[i], 16);
}
s = Convert.ToInt32(result.Trim(), 2);
operateResult = inovanceTcp.Write(strArry[0], (ushort)s);
}
else
{
var inf2 = Convert.ToString(info.Content, 2);
string[] infoArray = new string[inf2.Length];
for (int i = 0; i < inf2.Length; i++)
{
infoArray[i] = inf2.Substring(i, 1);
}
Array.Reverse(infoArray);
infoArray[_stringChange.ParseToInt(strArry[1])] = value.ToString();
string result = "";
foreach (var item in infoArray)
{
result = result + item;
}
s = Convert.ToInt32(result.Trim(), 10);
operateResult = inovanceTcp.Write(strArry[0], s);
}
if (operateResult.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址写入int类型数据异常", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址读取int16类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int readInt16ByAddress(string address)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}读取int16类型数据", address));
try
{
OperateResult<short> read = inovanceTcp.ReadInt16(address);
if (read.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据成功{1}", address, read.Content));
return read.Content;
}
PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据失败", address));
this.IsConnected = false;
return 0;
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址读取int16类型数据异常", ex);
this.IsConnected = false;
return 0;
}
}
/// <summary>
/// 通过PLC地址写入Short类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeShortByAddress(string address, int value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入Short类型数据{1}", address, value));
try
{
OperateResult write = inovanceTcp.Write(address, short.Parse(Convert.ToString(value)));
if (write.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}失败!!!", address, value));
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据异常", address), ex);
return false;
}
}
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeStringByAddress(string address, string value)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}", address, value));
try
{
OperateResult operateResult = inovanceTcp.Write(address, value);
if (operateResult.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}失败!!!", address, value));
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据异常", address), ex);
return false;
}
}
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public string readStringByAddress(string address, ushort length)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address));
try
{
OperateResult<String> read = inovanceTcp.ReadString(address, length);
if (read.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}读取string类型数据成功{1}", address, read.Content));
return read.Content;
}
PrintLogInfo(String.Format("通过PLC地址{0}读取string类型数据失败", address));
return "";
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址读取string类型数据异常", ex);
return "";
}
}
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool readBoolByAddress(string address)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
try
{
OperateResult<bool> read = inovanceTcp.ReadBool(address);
if (read.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}读取bool类型数据成功{1}", address, read.Content));
return read.Content;
}
PrintLogInfo(String.Format("通过PLC地址{0}读取bool类型数据失败", address));
return false;
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址读取bool类型数据异常", ex);
return false;
}
}
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeBoolByAddress(string address, bool value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value));
try
{
OperateResult write = inovanceTcp.Write(address, value);
if (write.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入bool类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入bool类型数据{1}失败!!!", address, value));
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入bool类型数据异常", address), ex);
return false;
}
}
/// <summary>
/// 写入Double类型
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeDoubleByAddress(string address, int value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入Double类型数据{1}", address, value));
try
{
OperateResult write = inovanceTcp.Write(address, Convert.ToDouble(value));
if (write.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Double类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入Double类型数据{1}失败!!!", address, value));
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Double类型数据异常", address), ex);
return false;
}
}
private void PrintLogInfo(string message, Exception ex = null)
{
if (ex != null)
{
_logger.LogError($"{message}{ex.Message}");
}
else
{
_logger.LogInformation(message);
}
}
}
}

@ -1,355 +0,0 @@
using HslCommunication.Profinet.Melsec;
using HslCommunication;
using SlnMesnac.Common;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Logging;
namespace SlnMesnac.Plc.Impl
{
/// <summary>
/// 三菱Q系列Plc-Binary
/// </summary>
public class MelsecBinaryPlc : IPlc
{
private ILogger<MelsecBinaryPlc> _logger;
private StringChange _stringChange;
private MelsecMcNet melsec_net = null;
public MelsecBinaryPlc(ILogger<MelsecBinaryPlc> logger, StringChange stringChange)
{
_logger = logger;
_stringChange = stringChange;
this.melsec_net = new MelsecMcNet();
this.melsec_net.ConnectTimeOut = 2000;
}
public bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <returns></returns>
public bool Connect(string IP, int port)
{
PrintLogInfo("三菱Q系列PLC连接开始");
melsec_net.IpAddress = IP;
melsec_net.Port = port;
try
{
OperateResult connect = melsec_net.ConnectServer();
if (connect.IsSuccess)
{
this.IsConnected = true;
PrintLogInfo($"三菱Q系列PLC建立连接失败:{connect.Message}");
return true;
}
else
{
this.IsConnected = false;
PrintLogInfo("三菱Q系列PLC建立连接失败");
return false;
}
}
catch (Exception ex)
{
this.IsConnected = false;
PrintLogInfo("三菱Q系列PLC建立连接异常", ex);
return false;
}
}
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
public bool DisConnect()
{
return melsec_net.ConnectClose().IsSuccess;
}
/// <summary>
/// 通过地址和长度读取PLC数据
/// </summary>
/// <param name="len"></param>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public byte[] readValueByAddress(int len, string address)
{
PrintLogInfo($"开始通过地址:{address},读取长度:{len}的PLC数据");
try
{
OperateResult<byte[]> read = melsec_net.Read(address, (ushort)(len));
if (read.IsSuccess)
{
byte[] result = _stringChange.ConvertFloatToINt(read.Content);
PrintLogInfo(String.Format("通过地址和长度读取PLC数据成功{0}", _stringChange.bytesToHexStr(result, result.Length)));
return result;
}
else
{
PrintLogInfo($"通过地址和长度读取PLC数据失败:{read.Message}");
this.IsConnected = false;
return new byte[0];
}
}
catch (Exception ex)
{
PrintLogInfo("通过地址和长度读取PLC数据异常", ex);
this.IsConnected = false;
return new byte[0];
}
}
/// <summary>
/// 通过PLC地址写入int类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeValueByAddress(string address,int value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}",address,value));
try
{
OperateResult operateResult = melsec_net.Write(address, Convert.ToInt32(value));
if (operateResult.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址写入int类型数据异常", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址读取int16类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int readInt16ByAddress(string address)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}读取int16类型数据",address));
try
{
OperateResult<short> read = melsec_net.ReadInt16(address);
if (read.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据成功{1}", address, read.Content));
return read.Content;
}
PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据失败", address));
this.IsConnected = false;
return 0;
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址读取int16类型数据异常", ex);
this.IsConnected = false;
return 0;
}
}
/// <summary>
/// 通过PLC地址写入Short类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeShortByAddress(string address, int value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入Short类型数据{1}", address, value));
try
{
OperateResult write = melsec_net.Write(address, short.Parse(Convert.ToString(value)));
if (write.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeStringByAddress(string address, string value)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value));
try
{
OperateResult operateResult = melsec_net.Write(address, value);
if (operateResult.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}失败!!!", address, value));
//this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据异常", address), ex);
//this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public string readStringByAddress(string address, ushort length)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address));
try
{
OperateResult<String> read = melsec_net.ReadString(address, length);
if (read.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}读取string类型数据成功{1}", address, read.Content));
return read.Content;
}
PrintLogInfo(String.Format("通过PLC地址{0}读取string类型数据失败", address));
this.IsConnected = false;
return "";
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址读取string类型数据异常", ex);
return "";
}
}
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool readBoolByAddress(string address)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
try
{
OperateResult<bool> read = melsec_net.ReadBool(address);
if (read.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}读取bool类型数据成功{1}", address, read.Content));
return read.Content;
}
PrintLogInfo(String.Format("通过PLC地址{0}读取bool类型数据失败", address));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址读取int32类型数据异常", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeBoolByAddress(string address, bool value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value));
try
{
OperateResult write = melsec_net.Write(address, short.Parse(_stringChange.ParseToInt(value ? "1" : "0").ToString()));
if (write.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入bool类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入bool类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入bool类型数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 写入Double类型
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeDoubleByAddress(string address, int value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入Double类型数据{1}", address, value));
try
{
OperateResult write = melsec_net.Write(address, Convert.ToDouble(value));
if (write.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Double类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入Double类型数据{1}失败!!!", address, value));
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Double类型数据异常", address), ex);
return false;
}
}
private void PrintLogInfo(string message, Exception ex = null)
{
if (ex != null)
{
_logger.LogError($"{message}{ex.Message}");
}
else
{
_logger.LogInformation(message);
}
}
}
}

@ -1,360 +0,0 @@
using HslCommunication.Profinet.Omron;
using HslCommunication;
using SlnMesnac.Common;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Logging;
namespace SlnMesnac.Plc.Impl
{
/// <summary>
/// 欧姆龙NJ系列PLC
/// </summary>
public class OmronNJPlc : IPlc
{
private ILogger<OmronNJPlc> _logger;
private StringChange _stringChange;
private OmronFinsNet omronFinsNet = null;
public OmronNJPlc(ILogger<OmronNJPlc> logger,StringChange stringChange)
{
_logger = logger;
_stringChange = stringChange;
this.omronFinsNet = new OmronFinsNet();
this.omronFinsNet.ConnectTimeOut = 2000;
}
public bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool Connect(string IP, int port)
{
PrintLogInfo("欧姆龙NJ系列PLC连接开始");
omronFinsNet.IpAddress = IP;
omronFinsNet.Port = 9600;
omronFinsNet.SA1 = (byte)192;
omronFinsNet.DA1 = (byte)239;
omronFinsNet.DA2 = (byte)0;
try
{
OperateResult connect = omronFinsNet.ConnectServer();
if (connect.IsSuccess)
{
this.IsConnected = true;
PrintLogInfo("欧姆龙NJ系列PLC建立连接成功");
return true;
}
else
{
this.IsConnected = false;
PrintLogInfo($"欧姆龙NJ系列PLC建立连接失败:{connect.Message}");
return false;
}
}
catch (Exception ex)
{
this.IsConnected = false;
PrintLogInfo("欧姆龙NJ系列PLC建立连接异常", ex);
return false;
}
}
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
public bool DisConnect()
{
return omronFinsNet.ConnectClose().IsSuccess;
}
/// <summary>
/// 通过地址和长度读取PLC数据
/// </summary>
/// <param name="len"></param>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public byte[] readValueByAddress(int len, string address)
{
PrintLogInfo($"开始通过地址:{address},读取长度:{len}的PLC数据");
try
{
OperateResult<byte[]> read = omronFinsNet.Read(address, (ushort)(len));
if (read.IsSuccess)
{
byte[] result = _stringChange.ConvertFloatToINt(read.Content);
PrintLogInfo(String.Format("通过地址和长度读取PLC数据成功{0}", _stringChange.bytesToHexStr(result, result.Length)));
return result;
}
else
{
PrintLogInfo($"通过地址和长度读取PLC数据失败:{read.Message}");
this.IsConnected = false;
return new byte[0];
}
}
catch (Exception ex)
{
PrintLogInfo("通过地址和长度读取PLC数据异常", ex);
this.IsConnected = false;
return new byte[0];
}
}
/// <summary>
/// 通过PLC地址写入int类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeValueByAddress(string address,int value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}",address,value));
try
{
OperateResult operateResult = omronFinsNet.Write(address, Convert.ToInt32(value));
if (operateResult.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址写入int类型数据异常", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址读取int16类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int readInt16ByAddress(string address)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}读取int16类型数据",address));
try
{
OperateResult<short> read = omronFinsNet.ReadInt16(address);
if (read.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据成功{1}", address, read.Content));
return read.Content;
}
PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据失败", address));
this.IsConnected = false;
return 0;
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址读取int16类型数据异常", ex);
this.IsConnected = false;
return 0;
}
}
/// <summary>
/// 通过PLC地址写入Short类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeShortByAddress(string address, int value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入Short类型数据{1}", address, value));
try
{
OperateResult write = omronFinsNet.Write(address, short.Parse(Convert.ToString(value)));
if (write.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeStringByAddress(string address, string value)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value));
try
{
OperateResult operateResult = omronFinsNet.Write(address, value);
if (operateResult.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}失败!!!", address, value));
//this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据异常", address), ex);
//this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public string readStringByAddress(string address, ushort length)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address));
try
{
OperateResult<String> read = omronFinsNet.ReadString(address, length);
if (read.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}读取string类型数据成功{1}", address, read.Content));
return read.Content;
}
PrintLogInfo(String.Format("通过PLC地址{0}读取string类型数据失败", address));
this.IsConnected = false;
return "";
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址读取string类型数据异常", ex);
return "";
}
}
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool readBoolByAddress(string address)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
try
{
OperateResult<bool> read = omronFinsNet.ReadBool(address);
if (read.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}读取bool类型数据成功{1}", address, read.Content));
return read.Content;
}
PrintLogInfo(String.Format("通过PLC地址{0}读取bool类型数据失败", address));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址读取int32类型数据异常", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeBoolByAddress(string address, bool value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value));
try
{
OperateResult write = omronFinsNet.Write(address, short.Parse(_stringChange.ParseToInt(value ? "1" : "0").ToString()));
if (write.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入bool类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入bool类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入bool类型数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 写入Double类型
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeDoubleByAddress(string address, int value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入Double类型数据{1}", address, value));
try
{
OperateResult write = omronFinsNet.Write(address, Convert.ToDouble(value));
if (write.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Double类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入Double类型数据{1}失败!!!", address, value));
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Double类型数据异常", address), ex);
return false;
}
}
private void PrintLogInfo(string message, Exception ex = null)
{
if (ex != null)
{
_logger.LogError($"{message}{ex.Message}");
}
else
{
_logger.LogInformation(message);
}
}
}
}

@ -1,353 +0,0 @@
using HslCommunication.Profinet.Siemens;
using HslCommunication;
using SlnMesnac.Common;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Logging;
namespace SlnMesnac.Plc.Impl
{
public class SiemensPlc : IPlc
{
private ILogger<SiemensPlc> _logger;
private StringChange _stringChange;
private const SiemensPLCS type = SiemensPLCS.S200Smart;
private SiemensS7Net s7 = new SiemensS7Net(type);
public SiemensPlc(ILogger<SiemensPlc> logger, StringChange stringChange)
{
this._logger = logger;
_stringChange = stringChange;
}
public bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool Connect(string IP, int port)
{
PrintLogInfo("西门子S7系列PLC连接开始");
s7.IpAddress = IP;
s7.Port = 102;
try
{
OperateResult connect = s7.ConnectServer();
if (connect.IsSuccess)
{
this.IsConnected = true;
PrintLogInfo("西门子S7系列PLC建立连接成功");
return true;
}
else
{
this.IsConnected = false;
PrintLogInfo($"西门子S7系列PLC建立连接失败:{connect.Message}");
return false;
}
}
catch (Exception ex)
{
this.IsConnected = false;
PrintLogInfo("西门子S7系列PLC建立连接异常", ex);
return false;
}
}
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
public bool DisConnect()
{
return s7.ConnectClose().IsSuccess;
}
/// <summary>
/// 通过地址和长度读取PLC数据
/// </summary>
/// <param name="len"></param>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public byte[] readValueByAddress(int len, string address)
{
PrintLogInfo($"开始通过地址:{address},读取长度:{len}的PLC数据");
try
{
OperateResult<byte[]> read = s7.Read(address, (ushort)(len));
if (read.IsSuccess)
{
byte[] result = _stringChange.ConvertFloatToINt(read.Content);
PrintLogInfo(String.Format("通过地址和长度读取PLC数据成功{0}", _stringChange.bytesToHexStr(result, result.Length)));
return result;
}
else
{
PrintLogInfo($"通过地址和长度读取PLC数据失败:{read.Message}");
this.IsConnected = false;
return new byte[0];
}
}
catch (Exception ex)
{
PrintLogInfo("通过地址和长度读取PLC数据异常", ex);
this.IsConnected = false;
return new byte[0];
}
}
/// <summary>
/// 通过PLC地址写入int类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeValueByAddress(string address,int value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}",address,value));
try
{
OperateResult operateResult = s7.Write(address, Convert.ToInt32(value));
if (operateResult.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址写入int类型数据异常", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址读取int16类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public int readInt16ByAddress(string address)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}读取int16类型数据",address));
try
{
OperateResult<short> read = s7.ReadInt16(address);
if (read.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据成功{1}", address, read.Content));
return read.Content;
}
PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据失败", address));
this.IsConnected = false;
return 0;
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址读取int16类型数据异常", ex);
this.IsConnected = false;
return 0;
}
}
/// <summary>
/// 通过PLC地址写入Short类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeShortByAddress(string address, int value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入Short类型数据{1}", address, value));
try
{
OperateResult write = s7.Write(address, short.Parse(Convert.ToString(value)));
if (write.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入String类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool writeStringByAddress(string address, string value)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value));
try
{
OperateResult operateResult = s7.Write(address, value);
if (operateResult.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}失败!!!", address, value));
//this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据异常", address), ex);
//this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public string readStringByAddress(string address, ushort length)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address));
try
{
OperateResult<String> read = s7.ReadString(address, length);
if (read.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}读取string类型数据成功{1}", address, read.Content));
return read.Content;
}
PrintLogInfo(String.Format("通过PLC地址{0}读取string类型数据失败", address));
this.IsConnected = false;
return "";
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址读取string类型数据异常", ex);
return "";
}
}
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool readBoolByAddress(string address)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
try
{
OperateResult<bool> read = s7.ReadBool(address);
if (read.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}读取bool类型数据成功{1}", address, read.Content));
return read.Content;
}
PrintLogInfo(String.Format("通过PLC地址{0}读取bool类型数据失败", address));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo("通过PLC地址读取int32类型数据异常", ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeBoolByAddress(string address, bool value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value));
try
{
OperateResult write = s7.Write(address, short.Parse(_stringChange.ParseToInt(value ? "1" : "0").ToString()));
if (write.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入bool类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入bool类型数据{1}失败!!!", address, value));
this.IsConnected = false;
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入bool类型数据异常", address), ex);
this.IsConnected = false;
return false;
}
}
/// <summary>
/// 写入Double类型
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool writeDoubleByAddress(string address, int value)
{
PrintLogInfo(String.Format("开始通过PLC地址{0}写入Double类型数据{1}", address, value));
try
{
OperateResult write = s7.Write(address, Convert.ToDouble(value));
if (write.IsSuccess)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Double类型数据{1}成功", address, value));
return true;
}
PrintLogInfo(String.Format("通过PLC地址{0}写入Double类型数据{1}失败!!!", address, value));
return false;
}
catch (Exception ex)
{
PrintLogInfo(String.Format("通过PLC地址{0}写入Double类型数据异常", address), ex);
return false;
}
}
private void PrintLogInfo(string message, Exception ex = null)
{
if (ex != null)
{
_logger.LogError($"{message}{ex.Message}");
}
else
{
_logger.LogInformation(message);
}
}
}
}

@ -1,67 +1,55 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace SlnMesnac.Plc
namespace SlnMesnac.Plc
{
public interface IPlc
public abstract class PlcAbsractFactory
{
/// <summary>
/// 连接标识
/// 是否连接
/// </summary>
bool IsConnected { get; set; }
public abstract bool IsConnected { get; set; }
/// <summary>
/// 建立连接
/// </summary>
/// <param name="IP"></param>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <returns></returns>
bool Connect(string IP, int port);
public abstract bool Connect(string ip, int port);
/// <summary>
/// 断开连接
/// </summary>
/// <returns></returns>
bool DisConnect();
public abstract bool DisConnect();
/// <summary>
/// 通过地址和长度读取PLC数据
/// 根据地址读取指定长度数据
/// </summary>
/// <param name="len"></param>
/// <param name="address"></param>
/// <param name="len"></param>
/// <returns></returns>
byte[] readValueByAddress(int len, string address);
/// <summary>
/// 通过PLC地址写入int类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
bool writeValueByAddress(string address,int value);
public abstract byte[] readValueByAddress(string address,int len);
/// <summary>
/// 通过PLC地址读取int16类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
int readInt16ByAddress(string address);
public abstract int readInt16ByAddress(string address);
/// <summary>
/// 通过PLC地址写入Short类型数据
/// 通过PLC地址写入int16类型数据
/// </summary>
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
bool writeShortByAddress(string address, int value);
public abstract bool writeInt16ByAddress(string address,int value);
/// <summary>
/// 通过PLC地址读取string类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
string readStringByAddress(string address, ushort length);
public abstract string readStringByAddress(string address, ushort length);
/// <summary>
/// 通过PLC地址写入String类型数据
@ -69,21 +57,21 @@ namespace SlnMesnac.Plc
/// <param name="address"></param>
/// <param name="SFC"></param>
/// <returns></returns>
bool writeStringByAddress(string address, string value);
public abstract bool writeStringByAddress(string address, string value);
/// <summary>
/// 通过PLC地址读取Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
bool readBoolByAddress(string address);
public abstract bool readBoolByAddress(string address);
/// <summary>
/// 通过PLC地址写入Bool类型数据
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
bool writeBoolByAddress(string address, bool value);
public abstract bool writeBoolByAddress(string address, bool value);
/// <summary>
/// 通过PLC地址写入Double类型数据
@ -91,6 +79,6 @@ namespace SlnMesnac.Plc
/// <param name="address"></param>
/// <param name="value"></param>
/// <returns></returns>
bool writeDoubleByAddress(string address, int value);
public abstract bool writeDoubleByAddress(string address, int value);
}
}
}

@ -1,11 +1,9 @@
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using SlnMesnac.Config;
using SlnMesnac.Plc.Impl;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using SlnMesnac.Plc.Factory;
namespace SlnMesnac.Plc
{
@ -14,29 +12,25 @@ namespace SlnMesnac.Plc
/// </summary>
public class PlcPool
{
private ILogger<PlcPool> _logger;
private readonly AppConfig _appConfig;
private Dictionary<string, IPlc> keyValuePairs = new Dictionary<string, IPlc>();
private Dictionary<string, PlcAbsractFactory> keyValuePairs = new Dictionary<string, PlcAbsractFactory>();
private readonly InovancePlc _inovancePlc;
private readonly MelsecBinaryPlc _melsecBinaryPlc;
private readonly OmronNJPlc _omronNJPlc;
private readonly SiemensPlc _siemensPlc;
private readonly AppConfig _appConfig;
private readonly InovanceFactory _inovance;
private readonly MelsecBinaryFactory _melsecBinary;
private readonly OmronNJFactory _omronNj;
private readonly SiemensFactory _siemens;
public PlcPool(ILogger<PlcPool> logger, InovancePlc inovancePlc, MelsecBinaryPlc melsecBinaryPlc, OmronNJPlc omronNJPlc, SiemensPlc siemensPlc, AppConfig appConfig)
public PlcPool(ILogger<PlcPool> logger, InovanceFactory inovance,MelsecBinaryFactory melsecBinary,OmronNJFactory omronNj,SiemensFactory siemens, AppConfig appConfig)
{
_logger = logger;
_inovancePlc = inovancePlc;
_melsecBinaryPlc = melsecBinaryPlc;
_omronNJPlc = omronNJPlc;
_siemensPlc = siemensPlc;
_inovance = inovance;
_melsecBinary = melsecBinary;
_omronNj = omronNj;
_siemens = siemens;
_appConfig = appConfig;
//this.Init();
}
public void Init()
{
if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41"))
@ -77,20 +71,20 @@ namespace SlnMesnac.Plc
/// <param name="key"></param>
public void AddPlc(string plcType, string ip, int port, string key)
{
IPlc _plc = null;
PlcAbsractFactory _plc = null;
switch (plcType)
{
case "InovancePlc":
_plc = _inovancePlc;
_plc = _inovance;
break;
case "MelsecBinaryPlc":
_plc = _melsecBinaryPlc;
_plc = _melsecBinary;
break;
case "OmronNJPlc":
_plc = _omronNJPlc;
_plc = _omronNj;
break;
case "SiemensPlc":
_plc = _siemensPlc;
_plc = _siemens;
break;
default:
break;
@ -98,6 +92,7 @@ namespace SlnMesnac.Plc
var connectResult = _plc.Connect(ip, port);
if (connectResult)
{
_logger.LogInformation($"PLC{ip}:{port};连接成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
keyValuePairs.Add(key, _plc);
if (!keyValuePairs.ContainsKey(key))
{
@ -108,6 +103,10 @@ namespace SlnMesnac.Plc
keyValuePairs[key] = _plc;
}
}
else
{
_logger.LogInformation($"PLC{ip}:{port};连接失败,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
}
}
/// <summary>
@ -115,7 +114,7 @@ namespace SlnMesnac.Plc
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public IPlc GetPlcByKey(string key)
public PlcAbsractFactory GetPlcByKey(string key)
{
try
{
@ -131,7 +130,7 @@ namespace SlnMesnac.Plc
/// 获取所有PLC信息
/// </summary>
/// <returns></returns>
public Dictionary<string, IPlc> GetAll()
public Dictionary<string, PlcAbsractFactory> GetAll()
{
return keyValuePairs;
}

@ -1,10 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Common;
using SlnMesnac.Plc.Impl;
using System;
using System.Collections.Generic;
using System.Text;
using SlnMesnac.Plc.Factory;
namespace SlnMesnac.Plc
{
@ -12,11 +8,13 @@ namespace SlnMesnac.Plc
{
public static void AddPlcSetup(this IServiceCollection services)
{
services.AddSingleton<InovancePlc>();
services.AddSingleton<MelsecBinaryPlc>();
services.AddSingleton<OmronNJPlc>();
services.AddSingleton<SiemensPlc>();
services.AddSingleton<PlcPool>();
//services.AddSingleton<PlcAbsractFactory>();
services.AddSingleton<InovanceFactory>();
services.AddSingleton<MelsecBinaryFactory>();
services.AddSingleton<OmronNJFactory>();
services.AddSingleton<SiemensFactory>();
}
public static IApplicationBuilder UsePlcExtensions(this IApplicationBuilder app)

@ -1,10 +1,7 @@
using Microsoft.OpenApi.Models;
using Serilog;
using Serilog.Events;
using SlnMesnac.Common;
using SlnMesnac.Config;
using SlnMesnac.Plc;
using SlnMesnac.Plc.Impl;
using SlnMesnac.Quartz;
using SlnMesnac.Repository;
using SlnMesnac.Serilog;

Loading…
Cancel
Save