diff --git a/SlnMesnac.Plc/Factory/InovanceFactory.cs b/SlnMesnac.Plc/Factory/InovanceFactory.cs
new file mode 100644
index 0000000..3884959
--- /dev/null
+++ b/SlnMesnac.Plc/Factory/InovanceFactory.cs
@@ -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; }
+
+ ///
+ /// 建立连接
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 断开连接
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 根据地址读取指定长度数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ public override byte[] readValueByAddress(string address, int len)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 根据地址读取int16数据
+ ///
+ ///
+ ///
+ ///
+ public override int readInt16ByAddress(string address)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 根据地址写入int16数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址读取string类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ public override string readStringByAddress(string address, ushort length)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址写入String类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址读取Bool类型数据
+ ///
+ ///
+ ///
+ ///
+ public override bool readBoolByAddress(string address)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址写入Bool类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址写入Double类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/SlnMesnac.Plc/Factory/MelsecBinaryFactory.cs b/SlnMesnac.Plc/Factory/MelsecBinaryFactory.cs
new file mode 100644
index 0000000..94fccd9
--- /dev/null
+++ b/SlnMesnac.Plc/Factory/MelsecBinaryFactory.cs
@@ -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; }
+
+ ///
+ /// 建立连接
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 断开连接
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 根据地址读取指定长度数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ public override byte[] readValueByAddress(string address, int len)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 根据地址读取int16数据
+ ///
+ ///
+ ///
+ ///
+ public override int readInt16ByAddress(string address)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 根据地址写入int16数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址读取string类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ public override string readStringByAddress(string address, ushort length)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址写入String类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址读取Bool类型数据
+ ///
+ ///
+ ///
+ ///
+ public override bool readBoolByAddress(string address)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址写入Bool类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址写入Double类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/SlnMesnac.Plc/Factory/OmronNJFactory.cs b/SlnMesnac.Plc/Factory/OmronNJFactory.cs
new file mode 100644
index 0000000..29b0b15
--- /dev/null
+++ b/SlnMesnac.Plc/Factory/OmronNJFactory.cs
@@ -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; }
+
+ ///
+ /// 建立连接
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 断开连接
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 根据地址读取指定长度数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ public override byte[] readValueByAddress(string address, int len)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 根据地址读取int16数据
+ ///
+ ///
+ ///
+ ///
+ public override int readInt16ByAddress(string address)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 根据地址写入int16数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址读取string类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ public override string readStringByAddress(string address, ushort length)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址写入String类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址读取Bool类型数据
+ ///
+ ///
+ ///
+ ///
+ public override bool readBoolByAddress(string address)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址写入Bool类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址写入Double类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/SlnMesnac.Plc/Factory/SiemensFactory.cs b/SlnMesnac.Plc/Factory/SiemensFactory.cs
new file mode 100644
index 0000000..f2b6805
--- /dev/null
+++ b/SlnMesnac.Plc/Factory/SiemensFactory.cs
@@ -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; }
+
+ ///
+ /// 建立连接
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 断开连接
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 根据地址读取指定长度数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ public override byte[] readValueByAddress(string address, int len)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 根据地址读取int16数据
+ ///
+ ///
+ ///
+ ///
+ public override int readInt16ByAddress(string address)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 根据地址写入int16数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址读取string类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ public override string readStringByAddress(string address, ushort length)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址写入String类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址读取Bool类型数据
+ ///
+ ///
+ ///
+ ///
+ public override bool readBoolByAddress(string address)
+ {
+ try
+ {
+ OperateResult 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址写入Bool类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+
+ ///
+ /// 通过PLC地址写入Double类型数据
+ ///
+ ///
+ ///
+ ///
+ ///
+ 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}");
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/SlnMesnac.Plc/Impl/InovancePlc.cs b/SlnMesnac.Plc/Impl/InovancePlc.cs
deleted file mode 100644
index 874d509..0000000
--- a/SlnMesnac.Plc/Impl/InovancePlc.cs
+++ /dev/null
@@ -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
-{
- ///
- /// 汇川PLC
- ///
- public class InovancePlc : IPlc
- {
- private ILogger _logger;
-
- private StringChange _stringChange;
-
- private InovanceTcpNet inovanceTcp = null;
-
- public InovancePlc(ILogger logger, StringChange stringChange)
- {
- this._logger = logger;
- _stringChange = stringChange;
-
- this.inovanceTcp = new InovanceTcpNet();
- this.inovanceTcp.ConnectTimeOut = 2000;
- }
-
- public bool IsConnected { get; set; }
-
- ///
- /// 建立连接
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 断开连接
- ///
- ///
- public bool DisConnect()
- {
- return inovanceTcp.ConnectClose().IsSuccess;
- }
-
- ///
- /// 通过地址和长度读取PLC数据
- ///
- ///
- ///
- ///
- ///
- public byte[] readValueByAddress(int len, string address)
- {
- PrintLogInfo($"开始通过地址:{address},读取长度:{len};的PLC数据");
- try
- {
- OperateResult 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];
- }
- }
-
- ///
- /// 通过PLC地址写入int类型数据,模切汇川PLC复位逻辑
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 通过PLC地址读取int16类型数据
- ///
- ///
- ///
- public int readInt16ByAddress(string address)
- {
- PrintLogInfo(String.Format("开始通过PLC地址{0}读取int16类型数据", address));
- try
- {
- OperateResult 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;
- }
- }
-
- ///
- /// 通过PLC地址写入Short类型数据
- ///
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 通过PLC地址写入String类型数据
- ///
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 通过PLC地址读取string类型数据
- ///
- ///
- ///
- ///
- public string readStringByAddress(string address, ushort length)
- {
- PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address));
- try
- {
- OperateResult 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 "";
- }
- }
-
- ///
- /// 通过PLC地址读取Bool类型数据
- ///
- ///
- ///
- ///
- public bool readBoolByAddress(string address)
- {
- PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
- try
- {
- OperateResult 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;
- }
- }
-
- ///
- /// 通过PLC地址写入Bool类型数据
- ///
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 写入Double类型
- ///
- ///
- ///
- ///
- 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);
- }
- }
-
- }
-}
\ No newline at end of file
diff --git a/SlnMesnac.Plc/Impl/MelsecBinaryPlc.cs b/SlnMesnac.Plc/Impl/MelsecBinaryPlc.cs
deleted file mode 100644
index 881ec38..0000000
--- a/SlnMesnac.Plc/Impl/MelsecBinaryPlc.cs
+++ /dev/null
@@ -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
-{
- ///
- /// 三菱Q系列Plc-Binary
- ///
- public class MelsecBinaryPlc : IPlc
- {
- private ILogger _logger;
-
- private StringChange _stringChange;
-
- private MelsecMcNet melsec_net = null;
-
- public MelsecBinaryPlc(ILogger logger, StringChange stringChange)
- {
- _logger = logger;
- _stringChange = stringChange;
-
- this.melsec_net = new MelsecMcNet();
- this.melsec_net.ConnectTimeOut = 2000;
- }
-
- public bool IsConnected { get; set; }
-
- ///
- /// 建立连接
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 断开连接
- ///
- ///
- public bool DisConnect()
- {
- return melsec_net.ConnectClose().IsSuccess;
- }
-
- ///
- /// 通过地址和长度读取PLC数据
- ///
- ///
- ///
- ///
- ///
- public byte[] readValueByAddress(int len, string address)
- {
- PrintLogInfo($"开始通过地址:{address},读取长度:{len};的PLC数据");
- try
- {
- OperateResult 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];
- }
- }
-
- ///
- /// 通过PLC地址写入int类型数据
- ///
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 通过PLC地址读取int16类型数据
- ///
- ///
- ///
- public int readInt16ByAddress(string address)
- {
- PrintLogInfo(String.Format("开始通过PLC地址{0}读取int16类型数据",address));
- try
- {
- OperateResult 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;
- }
- }
-
- ///
- /// 通过PLC地址写入Short类型数据
- ///
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 通过PLC地址写入String类型数据
- ///
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 通过PLC地址读取string类型数据
- ///
- ///
- ///
- ///
- public string readStringByAddress(string address, ushort length)
- {
- PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address));
- try
- {
- OperateResult 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 "";
- }
- }
-
- ///
- /// 通过PLC地址读取Bool类型数据
- ///
- ///
- ///
- ///
- public bool readBoolByAddress(string address)
- {
- PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
- try
- {
- OperateResult 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;
- }
- }
-
- ///
- /// 通过PLC地址写入Bool类型数据
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 写入Double类型
- ///
- ///
- ///
- ///
- 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);
- }
- }
- }
-}
diff --git a/SlnMesnac.Plc/Impl/OmronNJPlc.cs b/SlnMesnac.Plc/Impl/OmronNJPlc.cs
deleted file mode 100644
index 6f495ed..0000000
--- a/SlnMesnac.Plc/Impl/OmronNJPlc.cs
+++ /dev/null
@@ -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
-{
- ///
- /// 欧姆龙NJ系列PLC
- ///
- public class OmronNJPlc : IPlc
- {
-
- private ILogger _logger;
-
- private StringChange _stringChange;
-
- private OmronFinsNet omronFinsNet = null;
-
- public OmronNJPlc(ILogger logger,StringChange stringChange)
- {
- _logger = logger;
- _stringChange = stringChange;
-
- this.omronFinsNet = new OmronFinsNet();
- this.omronFinsNet.ConnectTimeOut = 2000;
- }
-
- public bool IsConnected { get; set; }
-
- ///
- /// 建立连接
- ///
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 断开连接
- ///
- ///
- public bool DisConnect()
- {
- return omronFinsNet.ConnectClose().IsSuccess;
- }
-
- ///
- /// 通过地址和长度读取PLC数据
- ///
- ///
- ///
- ///
- ///
- public byte[] readValueByAddress(int len, string address)
- {
- PrintLogInfo($"开始通过地址:{address},读取长度:{len};的PLC数据");
- try
- {
- OperateResult 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];
- }
- }
-
- ///
- /// 通过PLC地址写入int类型数据
- ///
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 通过PLC地址读取int16类型数据
- ///
- ///
- ///
- public int readInt16ByAddress(string address)
- {
- PrintLogInfo(String.Format("开始通过PLC地址{0}读取int16类型数据",address));
- try
- {
- OperateResult 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;
- }
- }
-
- ///
- /// 通过PLC地址写入Short类型数据
- ///
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 通过PLC地址写入String类型数据
- ///
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 通过PLC地址读取string类型数据
- ///
- ///
- ///
- ///
- public string readStringByAddress(string address, ushort length)
- {
- PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address));
- try
- {
- OperateResult 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 "";
- }
- }
-
- ///
- /// 通过PLC地址读取Bool类型数据
- ///
- ///
- ///
- ///
- public bool readBoolByAddress(string address)
- {
- PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
- try
- {
- OperateResult 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;
- }
- }
-
- ///
- /// 通过PLC地址写入Bool类型数据
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 写入Double类型
- ///
- ///
- ///
- ///
- 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);
- }
- }
- }
-}
diff --git a/SlnMesnac.Plc/Impl/SiemensPlc.cs b/SlnMesnac.Plc/Impl/SiemensPlc.cs
deleted file mode 100644
index d920d35..0000000
--- a/SlnMesnac.Plc/Impl/SiemensPlc.cs
+++ /dev/null
@@ -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 _logger;
-
- private StringChange _stringChange;
-
- private const SiemensPLCS type = SiemensPLCS.S200Smart;
-
- private SiemensS7Net s7 = new SiemensS7Net(type);
-
- public SiemensPlc(ILogger logger, StringChange stringChange)
- {
- this._logger = logger;
- _stringChange = stringChange;
-
- }
-
- public bool IsConnected { get; set; }
-
- ///
- /// 建立连接
- ///
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 断开连接
- ///
- ///
- public bool DisConnect()
- {
- return s7.ConnectClose().IsSuccess;
- }
-
- ///
- /// 通过地址和长度读取PLC数据
- ///
- ///
- ///
- ///
- ///
- public byte[] readValueByAddress(int len, string address)
- {
- PrintLogInfo($"开始通过地址:{address},读取长度:{len};的PLC数据");
- try
- {
- OperateResult 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];
- }
- }
-
- ///
- /// 通过PLC地址写入int类型数据
- ///
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 通过PLC地址读取int16类型数据
- ///
- ///
- ///
- public int readInt16ByAddress(string address)
- {
- PrintLogInfo(String.Format("开始通过PLC地址{0}读取int16类型数据",address));
- try
- {
- OperateResult 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;
- }
- }
-
- ///
- /// 通过PLC地址写入Short类型数据
- ///
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 通过PLC地址写入String类型数据
- ///
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 通过PLC地址读取string类型数据
- ///
- ///
- ///
- ///
- public string readStringByAddress(string address, ushort length)
- {
- PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address));
- try
- {
- OperateResult 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 "";
- }
- }
-
- ///
- /// 通过PLC地址读取Bool类型数据
- ///
- ///
- ///
- ///
- public bool readBoolByAddress(string address)
- {
- PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address));
- try
- {
- OperateResult 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;
- }
- }
-
- ///
- /// 通过PLC地址写入Bool类型数据
- ///
- ///
- ///
- ///
- 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;
- }
- }
-
- ///
- /// 写入Double类型
- ///
- ///
- ///
- ///
- 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);
- }
- }
- }
-}
\ No newline at end of file
diff --git a/SlnMesnac.Plc/IPlc.cs b/SlnMesnac.Plc/PlcAbsractFactory.cs
similarity index 60%
rename from SlnMesnac.Plc/IPlc.cs
rename to SlnMesnac.Plc/PlcAbsractFactory.cs
index 2f9c4d3..2771d75 100644
--- a/SlnMesnac.Plc/IPlc.cs
+++ b/SlnMesnac.Plc/PlcAbsractFactory.cs
@@ -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
{
///
- /// 连接标识
+ /// 是否连接
///
- bool IsConnected { get; set; }
-
+ public abstract bool IsConnected { get; set; }
+
///
/// 建立连接
///
- ///
+ ///
///
///
- bool Connect(string IP, int port);
-
+ public abstract bool Connect(string ip, int port);
+
///
/// 断开连接
///
///
- bool DisConnect();
-
+ public abstract bool DisConnect();
+
///
- /// 通过地址和长度读取PLC数据
+ /// 根据地址读取指定长度数据
///
- ///
///
+ ///
///
- byte[] readValueByAddress(int len, string address);
-
- ///
- /// 通过PLC地址写入int类型数据
- ///
- ///
- ///
- ///
- bool writeValueByAddress(string address,int value);
-
+ public abstract byte[] readValueByAddress(string address,int len);
+
///
/// 通过PLC地址读取int16类型数据
///
///
///
- int readInt16ByAddress(string address);
-
+ public abstract int readInt16ByAddress(string address);
+
///
- /// 通过PLC地址写入Short类型数据
+ /// 通过PLC地址写入int16类型数据
///
///
///
///
- bool writeShortByAddress(string address, int value);
-
+ public abstract bool writeInt16ByAddress(string address,int value);
+
///
/// 通过PLC地址读取string类型数据
///
///
///
- string readStringByAddress(string address, ushort length);
+ public abstract string readStringByAddress(string address, ushort length);
///
/// 通过PLC地址写入String类型数据
@@ -69,21 +57,21 @@ namespace SlnMesnac.Plc
///
///
///
- bool writeStringByAddress(string address, string value);
+ public abstract bool writeStringByAddress(string address, string value);
///
/// 通过PLC地址读取Bool类型数据
///
///
///
- bool readBoolByAddress(string address);
+ public abstract bool readBoolByAddress(string address);
///
/// 通过PLC地址写入Bool类型数据
///
///
///
- bool writeBoolByAddress(string address, bool value);
+ public abstract bool writeBoolByAddress(string address, bool value);
///
/// 通过PLC地址写入Double类型数据
@@ -91,6 +79,6 @@ namespace SlnMesnac.Plc
///
///
///
- bool writeDoubleByAddress(string address, int value);
+ public abstract bool writeDoubleByAddress(string address, int value);
}
-}
+}
\ No newline at end of file
diff --git a/SlnMesnac.Plc/PlcPool.cs b/SlnMesnac.Plc/PlcPool.cs
index 2e7d517..81d43fa 100644
--- a/SlnMesnac.Plc/PlcPool.cs
+++ b/SlnMesnac.Plc/PlcPool.cs
@@ -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
///
public class PlcPool
{
-
private ILogger _logger;
+ private readonly AppConfig _appConfig;
- private Dictionary keyValuePairs = new Dictionary();
+ private Dictionary keyValuePairs = new Dictionary();
- 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 logger, InovancePlc inovancePlc, MelsecBinaryPlc melsecBinaryPlc, OmronNJPlc omronNJPlc, SiemensPlc siemensPlc, AppConfig appConfig)
+ public PlcPool(ILogger 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
///
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")}");
+ }
}
///
@@ -115,7 +114,7 @@ namespace SlnMesnac.Plc
///
///
///
- public IPlc GetPlcByKey(string key)
+ public PlcAbsractFactory GetPlcByKey(string key)
{
try
{
@@ -131,7 +130,7 @@ namespace SlnMesnac.Plc
/// 获取所有PLC信息
///
///
- public Dictionary GetAll()
+ public Dictionary GetAll()
{
return keyValuePairs;
}
diff --git a/SlnMesnac.Plc/PlcSetup.cs b/SlnMesnac.Plc/PlcSetup.cs
index 9fc9439..6220baa 100644
--- a/SlnMesnac.Plc/PlcSetup.cs
+++ b/SlnMesnac.Plc/PlcSetup.cs
@@ -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();
- services.AddSingleton();
- services.AddSingleton();
- services.AddSingleton();
services.AddSingleton();
+
+ //services.AddSingleton();
+ services.AddSingleton();
+ services.AddSingleton();
+ services.AddSingleton();
+ services.AddSingleton();
}
public static IApplicationBuilder UsePlcExtensions(this IApplicationBuilder app)
diff --git a/SlnMesnac/Startup.cs b/SlnMesnac/Startup.cs
index 07f9445..7dccfe0 100644
--- a/SlnMesnac/Startup.cs
+++ b/SlnMesnac/Startup.cs
@@ -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;