From 4c13ac9cfd8362c317418cb4a9016aa06608bcd1 Mon Sep 17 00:00:00 2001 From: wenjy Date: Fri, 26 Jan 2024 15:10:26 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20PLC=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Plc/IPlc.cs | 34 +---- SlnMesnac.Plc/Impl/InovancePlc.cs | 154 ++++----------------- SlnMesnac.Plc/Impl/MelsecBinaryPlc.cs | 149 ++++---------------- SlnMesnac.Plc/Impl/OmronNJPlc.cs | 149 ++++---------------- SlnMesnac.Plc/Impl/SiemensPlc.cs | 189 ++++++++------------------ 5 files changed, 148 insertions(+), 527 deletions(-) diff --git a/SlnMesnac.Plc/IPlc.cs b/SlnMesnac.Plc/IPlc.cs index c1b1055..2f9c4d3 100644 --- a/SlnMesnac.Plc/IPlc.cs +++ b/SlnMesnac.Plc/IPlc.cs @@ -36,47 +36,25 @@ namespace SlnMesnac.Plc /// /// 通过PLC地址写入int类型数据 /// - /// - /// - /// - bool writeValueByAddress(int value, string address); - - /// - /// 通过PLC地址清零数据 - /// - /// - /// - /// - bool resetByAddress(string address, int len); - - /// - /// 通过PLC地址读取EA值 - /// - /// - /// - string readEaByAddress(string address); - - /// - /// 通过PLC地址读取交互信号 - /// /// + /// /// - int readInteractiveSignal(string address); + bool writeValueByAddress(string address,int value); /// - /// 通过PLC地址读取int32类型数据 + /// 通过PLC地址读取int16类型数据 /// /// /// - int readInt32ByAddress(string address); + int readInt16ByAddress(string address); /// - /// 通过PLC地址写入int32类型数据 + /// 通过PLC地址写入Short类型数据 /// /// /// /// - bool writeInt32ByAddress(string address, int value); + bool writeShortByAddress(string address, int value); /// /// 通过PLC地址读取string类型数据 diff --git a/SlnMesnac.Plc/Impl/InovancePlc.cs b/SlnMesnac.Plc/Impl/InovancePlc.cs index dcd5c69..874d509 100644 --- a/SlnMesnac.Plc/Impl/InovancePlc.cs +++ b/SlnMesnac.Plc/Impl/InovancePlc.cs @@ -63,7 +63,7 @@ namespace SlnMesnac.Plc.Impl catch (Exception ex) { this.IsConnected = false; - PrintLogInfo("欧姆龙NJ系列PLC建立连接异常", ex); + PrintLogInfo("汇川PLC建立连接异常", ex); return false; } } @@ -86,7 +86,7 @@ namespace SlnMesnac.Plc.Impl /// public byte[] readValueByAddress(int len, string address) { - //PrintLogInfo("开始通过PLC地址和长度读取PLC数据"); + PrintLogInfo($"开始通过地址:{address},读取长度:{len};的PLC数据"); try { OperateResult read = inovanceTcp.Read(address, (ushort)(len)); @@ -98,7 +98,7 @@ namespace SlnMesnac.Plc.Impl } else { - PrintLogInfo("通过地址和长度读取PLC数据失败!!!"); + PrintLogInfo($"通过地址和长度读取PLC数据失败:{read.Message}"); this.IsConnected = false; return new byte[0]; } @@ -114,13 +114,12 @@ namespace SlnMesnac.Plc.Impl /// /// 通过PLC地址写入int类型数据,模切汇川PLC复位逻辑 /// - /// /// + /// /// - /// - public bool writeValueByAddress(int value, string address) + public bool writeValueByAddress(string address,int value) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}", address, value)); + PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}", address, value)); OperateResult operateResult = new OperateResult(); try { @@ -175,171 +174,73 @@ namespace SlnMesnac.Plc.Impl } 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地址清零数据 - /// - /// - /// - /// - /// - public bool resetByAddress(string address, int len) - { - // PrintLogInfo(String.Format("开发通过PLC地址{0}清零数据", address)); - try - { - byte[] write = new byte[len * 2]; - for (int i = 0; i < len * 2; i++) - { - write[i] = 0; - } - OperateResult operateResult = inovanceTcp.Write(address, write); - - if (operateResult.IsSuccess) - { - PrintLogInfo(String.Format("通过PLC地址{0}清零数据成功", address)); + PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}成功", address, value)); return true; } - PrintLogInfo(String.Format("通过PLC地址{0}清零数据失败!!!", address)); + PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}失败!!!", address, value)); this.IsConnected = false; return false; } catch (Exception ex) { - PrintLogInfo(String.Format("通过PLC地址{0}清零数据异常", address), ex); + PrintLogInfo("通过PLC地址写入int类型数据异常", ex); this.IsConnected = false; return false; } } - - /// - /// 通过PLC地址读取EA值 - /// - /// - /// - /// - public string readEaByAddress(string address) - { - //PrintLogInfo(String.Format("通过PLC地址{0}读取EA值", address)); - try - { - OperateResult read = inovanceTcp.Read(address, (ushort)(8)); - - if (read.IsSuccess && read.Content != null) - { - string result = Convert.ToString(read.Content); - PrintLogInfo(String.Format("通过PLC地址{0}读取EA值成功:{1}", address, result)); - return result; - } - else - { - PrintLogInfo(String.Format("通过PLC地址{0}读取EA值失败!!!", address)); - this.IsConnected = false; - return ""; - } - } - catch (Exception ex) - { - PrintLogInfo("通过PLC地址读取EA值异常", ex); - this.IsConnected = false; - return ""; - } - } - - /// - /// 通过PLC地址读取交互信号 - /// - /// - /// - public int readInteractiveSignal(string address) - { - // PrintLogInfo(String.Format("开始通过PLC地址{0}读取交互信号", address)); - try - { - OperateResult read = inovanceTcp.ReadInt16(address); - if (read.IsSuccess) - { - PrintLogInfo(String.Format("通过PLC地址{0}读取交互信号成功:{1}", address, read.Content)); - return read.Content; - } - PrintLogInfo(String.Format("通过PLC地址{0}读取交互信号失败!!!", address)); - this.IsConnected = false; - return 0; - } - catch (Exception ex) - { - PrintLogInfo("通过PLC地址读取交互信号异常", ex); - this.IsConnected = false; - return 0; - } - } - + /// - /// 通过PLC地址读取int32类型数据 + /// 通过PLC地址读取int16类型数据 /// /// /// - public int readInt32ByAddress(string address) + public int readInt16ByAddress(string address) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}读取int32类型数据", address)); + PrintLogInfo(String.Format("开始通过PLC地址{0}读取int16类型数据", address)); try { OperateResult read = inovanceTcp.ReadInt16(address); if (read.IsSuccess) { - PrintLogInfo(String.Format("通过PLC地址{0}读取int32类型数据成功:{1}", address, read.Content)); + PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据成功:{1}", address, read.Content)); return read.Content; } - PrintLogInfo(String.Format("通过PLC地址{0}读取int32类型数据失败!!!", address)); + PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据失败!!!", address)); this.IsConnected = false; return 0; } catch (Exception ex) { - PrintLogInfo("通过PLC地址读取int32类型数据异常", ex); + PrintLogInfo("通过PLC地址读取int16类型数据异常", ex); this.IsConnected = false; return 0; } } /// - /// 通过PLC地址写入int32类型数据 + /// 通过PLC地址写入Short类型数据 /// /// /// /// /// - public bool writeInt32ByAddress(string address, int value) + public bool writeShortByAddress(string address, int value) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}写入int32类型数据{1}", address, 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}写入int32类型数据{1}成功", address, value)); + PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}成功", address, value)); return true; } - PrintLogInfo(String.Format("通过PLC地址{0}写入int32类型数据{1}失败!!!", address, value)); + PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}失败!!!", address, value)); return false; } catch (Exception ex) { - PrintLogInfo(String.Format("通过PLC地址{0}写入int32类型数据异常", address), ex); + PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据异常", address), ex); return false; } } @@ -353,7 +254,7 @@ namespace SlnMesnac.Plc.Impl /// public bool writeStringByAddress(string address, string value) { - //PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}", address, value)); + PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}", address, value)); try { OperateResult operateResult = inovanceTcp.Write(address, value); @@ -380,7 +281,7 @@ namespace SlnMesnac.Plc.Impl /// public string readStringByAddress(string address, ushort length) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address)); + PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address)); try { OperateResult read = inovanceTcp.ReadString(address, length); @@ -394,7 +295,7 @@ namespace SlnMesnac.Plc.Impl } catch (Exception ex) { - PrintLogInfo("通过PLC地址读取int32类型数据异常", ex); + PrintLogInfo("通过PLC地址读取string类型数据异常", ex); return ""; } } @@ -407,7 +308,7 @@ namespace SlnMesnac.Plc.Impl /// public bool readBoolByAddress(string address) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address)); + PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address)); try { OperateResult read = inovanceTcp.ReadBool(address); @@ -435,7 +336,7 @@ namespace SlnMesnac.Plc.Impl /// public bool writeBoolByAddress(string address, bool value) { - // PrintLogInfo(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value)); + PrintLogInfo(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value)); try { @@ -464,6 +365,7 @@ namespace SlnMesnac.Plc.Impl /// 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)); diff --git a/SlnMesnac.Plc/Impl/MelsecBinaryPlc.cs b/SlnMesnac.Plc/Impl/MelsecBinaryPlc.cs index ef427c2..881ec38 100644 --- a/SlnMesnac.Plc/Impl/MelsecBinaryPlc.cs +++ b/SlnMesnac.Plc/Impl/MelsecBinaryPlc.cs @@ -83,7 +83,7 @@ namespace SlnMesnac.Plc.Impl /// public byte[] readValueByAddress(int len, string address) { - //PrintLogInfo("开始通过PLC地址和长度读取PLC数据"); + PrintLogInfo($"开始通过地址:{address},读取长度:{len};的PLC数据"); try { OperateResult read = melsec_net.Read(address, (ushort)(len)); @@ -95,7 +95,7 @@ namespace SlnMesnac.Plc.Impl } else { - PrintLogInfo("通过地址和长度读取PLC数据失败!!!"); + PrintLogInfo($"通过地址和长度读取PLC数据失败:{read.Message}"); this.IsConnected = false; return new byte[0]; } @@ -111,182 +111,86 @@ namespace SlnMesnac.Plc.Impl /// /// 通过PLC地址写入int类型数据 /// - /// /// + /// /// /// - public bool writeValueByAddress(int value, string address) + public bool writeValueByAddress(string address,int value) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}",address,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)); + PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}成功", address, value)); return true; } - PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}失败!!!", address, value)); + PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}失败!!!", address, value)); this.IsConnected = false; return false; } catch (Exception ex) { - PrintLogInfo("通过PLC地址写入int类型数据", ex); + PrintLogInfo("通过PLC地址写入int类型数据异常", ex); this.IsConnected = false; return false; } } - - /// - /// 通过PLC地址清零数据 - /// - /// - /// - /// - /// - public bool resetByAddress(string address, int len) - { - //PrintLogInfo(String.Format("开发通过PLC地址{0}清零数据", address)); - try - { - byte[] write = new byte[len * 2]; - for (int i = 0; i < len * 2; i++) - { - write[i] = 0; - } - OperateResult operateResult = melsec_net.Write(address, write); - - if (operateResult.IsSuccess) - { - PrintLogInfo(String.Format("通过PLC地址{0}清零数据成功", address)); - return true; - } - PrintLogInfo(String.Format("通过PLC地址{0}清零数据失败!!!", address)); - return false; - } - catch (Exception ex) - { - PrintLogInfo(String.Format("通过PLC地址{0}清零数据异常", address), ex); - return false; - } - } - + /// - /// 通过PLC地址读取EA值 + /// 通过PLC地址读取int16类型数据 /// /// /// - /// - public string readEaByAddress(string address) + public int readInt16ByAddress(string address) { - //PrintLogInfo(String.Format("通过PLC地址{0}读取EA值", address)); - try - { - OperateResult read = melsec_net.Read(address, (ushort)(8)); - - if (read.IsSuccess && read.Content != null) - { - string result = Convert.ToString(read.Content); - PrintLogInfo(String.Format("通过PLC地址{0}读取EA值成功:{1}", address, result)); - return result; - } - else - { - PrintLogInfo(String.Format("通过PLC地址{0}读取EA值失败!!!", address)); - this.IsConnected = false; - return ""; - } - } - catch (Exception ex) - { - PrintLogInfo("通过PLC地址读取EA值异常", ex); - this.IsConnected = false; - return ""; - } - } - - /// - /// 通过PLC地址读取交互信号 - /// - /// - /// - public int readInteractiveSignal(string address) - { - //PrintLogInfo(String.Format("开始通过PLC地址{0}读取交互信号", address)); + PrintLogInfo(String.Format("开始通过PLC地址{0}读取int16类型数据",address)); try { OperateResult read = melsec_net.ReadInt16(address); if (read.IsSuccess) { - PrintLogInfo(String.Format("通过PLC地址{0}读取交互信号成功:{1}", address, read.Content)); + PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据成功:{1}", address, read.Content)); return read.Content; } - PrintLogInfo(String.Format("通过PLC地址{0}读取交互信号失败!!!", address)); + PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据失败!!!", address)); this.IsConnected = false; return 0; } catch (Exception ex) { - PrintLogInfo("通过PLC地址读取交互信号异常", ex); + PrintLogInfo("通过PLC地址读取int16类型数据异常", ex); this.IsConnected = false; return 0; } } /// - /// 通过PLC地址读取int32类型数据 - /// - /// - /// - public int readInt32ByAddress(string address) - { - //PrintLogInfo(String.Format("开始通过PLC地址{0}读取int32类型数据",address)); - try - { - OperateResult read = melsec_net.ReadInt16(address); - if (read.IsSuccess) - { - PrintLogInfo(String.Format("通过PLC地址{0}读取int32类型数据成功:{1}", address, read.Content)); - return read.Content; - } - PrintLogInfo(String.Format("通过PLC地址{0}读取int32类型数据失败!!!", address)); - this.IsConnected = false; - return 0; - } - catch (Exception ex) - { - PrintLogInfo("通过PLC地址读取int32类型数据异常", ex); - this.IsConnected = false; - return 0; - } - } - - /// - /// 通过PLC地址写入int32类型数据 + /// 通过PLC地址写入Short类型数据 /// /// /// /// /// - public bool writeInt32ByAddress(string address, int value) + public bool writeShortByAddress(string address, int value) { - PrintLogInfo(String.Format("开始通过PLC地址{0}写入int32类型数据{1}", address, 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}写入int32类型数据{1}成功", address, value)); + PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}成功", address, value)); return true; } - PrintLogInfo(String.Format("通过PLC地址{0}写入int32类型数据{1}失败!!!", address, value)); + PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}失败!!!", address, value)); this.IsConnected = false; return false; } catch (Exception ex) { - PrintLogInfo(String.Format("通过PLC地址{0}写入int32类型数据异常", address), ex); + PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据异常", address), ex); this.IsConnected = false; return false; } @@ -301,7 +205,7 @@ namespace SlnMesnac.Plc.Impl /// public bool writeStringByAddress(string address, string value) { - //PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value)); + PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value)); try { OperateResult operateResult = melsec_net.Write(address, value); @@ -330,7 +234,7 @@ namespace SlnMesnac.Plc.Impl /// public string readStringByAddress(string address, ushort length) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address)); + PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address)); try { OperateResult read = melsec_net.ReadString(address, length); @@ -345,7 +249,7 @@ namespace SlnMesnac.Plc.Impl } catch (Exception ex) { - PrintLogInfo("通过PLC地址读取int32类型数据异常", ex); + PrintLogInfo("通过PLC地址读取string类型数据异常", ex); return ""; } } @@ -358,7 +262,7 @@ namespace SlnMesnac.Plc.Impl /// public bool readBoolByAddress(string address) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address)); + PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address)); try { OperateResult read = melsec_net.ReadBool(address); @@ -387,7 +291,7 @@ namespace SlnMesnac.Plc.Impl /// public bool writeBoolByAddress(string address, bool value) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, 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())); @@ -416,6 +320,7 @@ namespace SlnMesnac.Plc.Impl /// 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)); diff --git a/SlnMesnac.Plc/Impl/OmronNJPlc.cs b/SlnMesnac.Plc/Impl/OmronNJPlc.cs index be21228..6f495ed 100644 --- a/SlnMesnac.Plc/Impl/OmronNJPlc.cs +++ b/SlnMesnac.Plc/Impl/OmronNJPlc.cs @@ -88,7 +88,7 @@ namespace SlnMesnac.Plc.Impl /// public byte[] readValueByAddress(int len, string address) { - //PrintLogInfo("开始通过PLC地址和长度读取PLC数据"); + PrintLogInfo($"开始通过地址:{address},读取长度:{len};的PLC数据"); try { OperateResult read = omronFinsNet.Read(address, (ushort)(len)); @@ -100,7 +100,7 @@ namespace SlnMesnac.Plc.Impl } else { - PrintLogInfo("通过地址和长度读取PLC数据失败!!!"); + PrintLogInfo($"通过地址和长度读取PLC数据失败:{read.Message}"); this.IsConnected = false; return new byte[0]; } @@ -116,182 +116,86 @@ namespace SlnMesnac.Plc.Impl /// /// 通过PLC地址写入int类型数据 /// - /// /// + /// /// /// - public bool writeValueByAddress(int value, string address) + public bool writeValueByAddress(string address,int value) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}",address,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)); + PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}成功", address, value)); return true; } - PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}失败!!!", address, value)); + PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}失败!!!", address, value)); this.IsConnected = false; return false; } catch (Exception ex) { - PrintLogInfo("通过PLC地址写入int类型数据", ex); + PrintLogInfo("通过PLC地址写入int类型数据异常", ex); this.IsConnected = false; return false; } } - - /// - /// 通过PLC地址清零数据 - /// - /// - /// - /// - /// - public bool resetByAddress(string address, int len) - { - //PrintLogInfo(String.Format("开发通过PLC地址{0}清零数据", address)); - try - { - byte[] write = new byte[len * 2]; - for (int i = 0; i < len * 2; i++) - { - write[i] = 0; - } - OperateResult operateResult = omronFinsNet.Write(address, write); - - if (operateResult.IsSuccess) - { - PrintLogInfo(String.Format("通过PLC地址{0}清零数据成功", address)); - return true; - } - PrintLogInfo(String.Format("通过PLC地址{0}清零数据失败!!!", address)); - return false; - } - catch (Exception ex) - { - PrintLogInfo(String.Format("通过PLC地址{0}清零数据异常", address), ex); - return false; - } - } - + /// - /// 通过PLC地址读取EA值 + /// 通过PLC地址读取int16类型数据 /// /// /// - /// - public string readEaByAddress(string address) + public int readInt16ByAddress(string address) { - //PrintLogInfo(String.Format("通过PLC地址{0}读取EA值", address)); - try - { - OperateResult read = omronFinsNet.Read(address, (ushort)(8)); - - if (read.IsSuccess && read.Content != null) - { - string result = Convert.ToString(read.Content); - PrintLogInfo(String.Format("通过PLC地址{0}读取EA值成功:{1}", address, result)); - return result; - } - else - { - PrintLogInfo(String.Format("通过PLC地址{0}读取EA值失败!!!", address)); - this.IsConnected = false; - return ""; - } - } - catch (Exception ex) - { - PrintLogInfo("通过PLC地址读取EA值异常", ex); - this.IsConnected = false; - return ""; - } - } - - /// - /// 通过PLC地址读取交互信号 - /// - /// - /// - public int readInteractiveSignal(string address) - { - //PrintLogInfo(String.Format("开始通过PLC地址{0}读取交互信号", address)); + PrintLogInfo(String.Format("开始通过PLC地址{0}读取int16类型数据",address)); try { OperateResult read = omronFinsNet.ReadInt16(address); if (read.IsSuccess) { - PrintLogInfo(String.Format("通过PLC地址{0}读取交互信号成功:{1}", address, read.Content)); + PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据成功:{1}", address, read.Content)); return read.Content; } - PrintLogInfo(String.Format("通过PLC地址{0}读取交互信号失败!!!", address)); + PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据失败!!!", address)); this.IsConnected = false; return 0; } catch (Exception ex) { - PrintLogInfo("通过PLC地址读取交互信号异常", ex); + PrintLogInfo("通过PLC地址读取int16类型数据异常", ex); this.IsConnected = false; return 0; } } /// - /// 通过PLC地址读取int32类型数据 - /// - /// - /// - public int readInt32ByAddress(string address) - { - //PrintLogInfo(String.Format("开始通过PLC地址{0}读取int32类型数据",address)); - try - { - OperateResult read = omronFinsNet.ReadInt16(address); - if (read.IsSuccess) - { - PrintLogInfo(String.Format("通过PLC地址{0}读取int32类型数据成功:{1}", address, read.Content)); - return read.Content; - } - PrintLogInfo(String.Format("通过PLC地址{0}读取int32类型数据失败!!!", address)); - this.IsConnected = false; - return 0; - } - catch (Exception ex) - { - PrintLogInfo("通过PLC地址读取int32类型数据异常", ex); - this.IsConnected = false; - return 0; - } - } - - /// - /// 通过PLC地址写入int32类型数据 + /// 通过PLC地址写入Short类型数据 /// /// /// /// /// - public bool writeInt32ByAddress(string address, int value) + public bool writeShortByAddress(string address, int value) { - PrintLogInfo(String.Format("开始通过PLC地址{0}写入int32类型数据{1}", address, 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}写入int32类型数据{1}成功", address, value)); + PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}成功", address, value)); return true; } - PrintLogInfo(String.Format("通过PLC地址{0}写入int32类型数据{1}失败!!!", address, value)); + PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}失败!!!", address, value)); this.IsConnected = false; return false; } catch (Exception ex) { - PrintLogInfo(String.Format("通过PLC地址{0}写入int32类型数据异常", address), ex); + PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据异常", address), ex); this.IsConnected = false; return false; } @@ -306,7 +210,7 @@ namespace SlnMesnac.Plc.Impl /// public bool writeStringByAddress(string address, string value) { - //PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value)); + PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value)); try { OperateResult operateResult = omronFinsNet.Write(address, value); @@ -335,7 +239,7 @@ namespace SlnMesnac.Plc.Impl /// public string readStringByAddress(string address, ushort length) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address)); + PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address)); try { OperateResult read = omronFinsNet.ReadString(address, length); @@ -350,7 +254,7 @@ namespace SlnMesnac.Plc.Impl } catch (Exception ex) { - PrintLogInfo("通过PLC地址读取int32类型数据异常", ex); + PrintLogInfo("通过PLC地址读取string类型数据异常", ex); return ""; } } @@ -363,7 +267,7 @@ namespace SlnMesnac.Plc.Impl /// public bool readBoolByAddress(string address) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address)); + PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address)); try { OperateResult read = omronFinsNet.ReadBool(address); @@ -392,7 +296,7 @@ namespace SlnMesnac.Plc.Impl /// public bool writeBoolByAddress(string address, bool value) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value)); + PrintLogInfo(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value)); try { OperateResult write = omronFinsNet.Write(address, short.Parse(_stringChange.ParseToInt(value ? "1" : "0").ToString())); @@ -421,6 +325,7 @@ namespace SlnMesnac.Plc.Impl /// 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)); diff --git a/SlnMesnac.Plc/Impl/SiemensPlc.cs b/SlnMesnac.Plc/Impl/SiemensPlc.cs index b7e0423..d920d35 100644 --- a/SlnMesnac.Plc/Impl/SiemensPlc.cs +++ b/SlnMesnac.Plc/Impl/SiemensPlc.cs @@ -62,7 +62,16 @@ namespace SlnMesnac.Plc.Impl return false; } } - + + /// + /// 断开连接 + /// + /// + public bool DisConnect() + { + return s7.ConnectClose().IsSuccess; + } + /// /// 通过地址和长度读取PLC数据 /// @@ -72,7 +81,7 @@ namespace SlnMesnac.Plc.Impl /// public byte[] readValueByAddress(int len, string address) { - //PrintLogInfo("开始通过PLC地址和长度读取PLC数据"); + PrintLogInfo($"开始通过地址:{address},读取长度:{len};的PLC数据"); try { OperateResult read = s7.Read(address, (ushort)(len)); @@ -84,7 +93,7 @@ namespace SlnMesnac.Plc.Impl } else { - PrintLogInfo("通过地址和长度读取PLC数据失败!!!"); + PrintLogInfo($"通过地址和长度读取PLC数据失败:{read.Message}"); this.IsConnected = false; return new byte[0]; } @@ -100,182 +109,86 @@ namespace SlnMesnac.Plc.Impl /// /// 通过PLC地址写入int类型数据 /// - /// /// + /// /// /// - public bool writeValueByAddress(int value, string address) + public bool writeValueByAddress(string address,int value) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}",address,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)); + PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}成功", address, value)); return true; } - PrintLogInfo(String.Format("开始通过PLC地址{0}写入int类型数据{1}失败!!!", address, value)); + PrintLogInfo(String.Format("通过PLC地址{0}写入int类型数据{1}失败!!!", address, value)); this.IsConnected = false; return false; } catch (Exception ex) { - PrintLogInfo("通过PLC地址写入int类型数据", ex); + PrintLogInfo("通过PLC地址写入int类型数据异常", ex); this.IsConnected = false; return false; } } - - /// - /// 通过PLC地址清零数据 - /// - /// - /// - /// - /// - public bool resetByAddress(string address, int len) - { - //PrintLogInfo(String.Format("开发通过PLC地址{0}清零数据", address)); - try - { - byte[] write = new byte[len * 2]; - for (int i = 0; i < len * 2; i++) - { - write[i] = 0; - } - OperateResult operateResult = s7.Write(address, write); - - if (operateResult.IsSuccess) - { - PrintLogInfo(String.Format("通过PLC地址{0}清零数据成功", address)); - return true; - } - PrintLogInfo(String.Format("通过PLC地址{0}清零数据失败!!!", address)); - return false; - } - catch (Exception ex) - { - PrintLogInfo(String.Format("通过PLC地址{0}清零数据异常", address), ex); - return false; - } - } - - /// - /// 通过PLC地址读取EA值 - /// - /// - /// - /// - public string readEaByAddress(string address) - { - //PrintLogInfo(String.Format("通过PLC地址{0}读取EA值", address)); - try - { - OperateResult read = s7.Read(address, (ushort)(8)); - - if (read.IsSuccess && read.Content != null) - { - string result = Convert.ToString(read.Content); - PrintLogInfo(String.Format("通过PLC地址{0}读取EA值成功:{1}", address, result)); - return result; - } - else - { - PrintLogInfo(String.Format("通过PLC地址{0}读取EA值失败!!!", address)); - this.IsConnected = false; - return ""; - } - } - catch (Exception ex) - { - PrintLogInfo("通过PLC地址读取EA值异常", ex); - this.IsConnected = false; - return ""; - } - } - - /// - /// 通过PLC地址读取交互信号 - /// - /// - /// - public int readInteractiveSignal(string address) - { - //PrintLogInfo(String.Format("开始通过PLC地址{0}读取交互信号", address)); - try - { - OperateResult read = s7.ReadInt16(address); - if (read.IsSuccess) - { - PrintLogInfo(String.Format("通过PLC地址{0}读取交互信号成功:{1}", address, read.Content)); - return read.Content; - } - PrintLogInfo(String.Format("通过PLC地址{0}读取交互信号失败!!!", address)); - this.IsConnected = false; - return 0; - } - catch (Exception ex) - { - PrintLogInfo("通过PLC地址读取交互信号异常", ex); - this.IsConnected = false; - return 0; - } - } - + /// - /// 通过PLC地址读取int32类型数据 + /// 通过PLC地址读取int16类型数据 /// /// /// - public int readInt32ByAddress(string address) + public int readInt16ByAddress(string address) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}读取int32类型数据",address)); + PrintLogInfo(String.Format("开始通过PLC地址{0}读取int16类型数据",address)); try { OperateResult read = s7.ReadInt16(address); if (read.IsSuccess) { - PrintLogInfo(String.Format("通过PLC地址{0}读取int32类型数据成功:{1}", address, read.Content)); + PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据成功:{1}", address, read.Content)); return read.Content; } - PrintLogInfo(String.Format("通过PLC地址{0}读取int32类型数据失败!!!", address)); + PrintLogInfo(String.Format("通过PLC地址{0}读取int16类型数据失败!!!", address)); this.IsConnected = false; return 0; } catch (Exception ex) { - PrintLogInfo("通过PLC地址读取int32类型数据异常", ex); + PrintLogInfo("通过PLC地址读取int16类型数据异常", ex); this.IsConnected = false; return 0; } } /// - /// 通过PLC地址写入int32类型数据 + /// 通过PLC地址写入Short类型数据 /// /// /// /// /// - public bool writeInt32ByAddress(string address, int value) + public bool writeShortByAddress(string address, int value) { - PrintLogInfo(String.Format("开始通过PLC地址{0}写入int32类型数据{1}", address, 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}写入int32类型数据{1}成功", address, value)); + PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}成功", address, value)); return true; } - PrintLogInfo(String.Format("通过PLC地址{0}写入int32类型数据{1}失败!!!", address, value)); + PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据{1}失败!!!", address, value)); this.IsConnected = false; return false; } catch (Exception ex) { - PrintLogInfo(String.Format("通过PLC地址{0}写入int32类型数据异常", address), ex); + PrintLogInfo(String.Format("通过PLC地址{0}写入Short类型数据异常", address), ex); this.IsConnected = false; return false; } @@ -290,7 +203,7 @@ namespace SlnMesnac.Plc.Impl /// public bool writeStringByAddress(string address, string value) { - //PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value)); + PrintLogInfo(String.Format("通过PLC地址{0}写入String类型数据{1}",address,value)); try { OperateResult operateResult = s7.Write(address, value); @@ -319,7 +232,7 @@ namespace SlnMesnac.Plc.Impl /// public string readStringByAddress(string address, ushort length) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address)); + PrintLogInfo(String.Format("开始通过PLC地址{0}读取string类型数据", address)); try { OperateResult read = s7.ReadString(address, length); @@ -334,7 +247,7 @@ namespace SlnMesnac.Plc.Impl } catch (Exception ex) { - PrintLogInfo("通过PLC地址读取int32类型数据异常", ex); + PrintLogInfo("通过PLC地址读取string类型数据异常", ex); return ""; } } @@ -347,7 +260,7 @@ namespace SlnMesnac.Plc.Impl /// public bool readBoolByAddress(string address) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address)); + PrintLogInfo(String.Format("开始通过PLC地址{0}读取bool类型数据", address)); try { OperateResult read = s7.ReadBool(address); @@ -376,7 +289,7 @@ namespace SlnMesnac.Plc.Impl /// public bool writeBoolByAddress(string address, bool value) { - //PrintLogInfo(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value)); + PrintLogInfo(String.Format("开始通过PLC地址{0}写入bool类型数据{1}", address, value)); try { OperateResult write = s7.Write(address, short.Parse(_stringChange.ParseToInt(value ? "1" : "0").ToString())); @@ -397,14 +310,32 @@ namespace SlnMesnac.Plc.Impl } } - public bool DisConnect() - { - throw new NotImplementedException(); - } - + /// + /// 写入Double类型 + /// + /// + /// + /// public bool writeDoubleByAddress(string address, int value) { - throw new NotImplementedException(); + 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)