From f30e997935f5a5120440f2bd92214672067d5f27 Mon Sep 17 00:00:00 2001 From: SoulStar Date: Wed, 25 Dec 2024 17:45:44 +0800 Subject: [PATCH] =?UTF-8?q?feat=20-=20=E6=B7=BB=E5=8A=A0PLC=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E7=B1=BB=EF=BC=8C=E5=87=86=E5=A4=87=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HighWayIot.Plc/HighWayIot.Plc.csproj | 3 - HighWayIot.Plc/PlcConnect.cs | 377 +++++++++--------- HighWayIot.Plc/packages.config | 5 - .../UserControlPages/TestPage.cs | 7 +- 4 files changed, 191 insertions(+), 201 deletions(-) delete mode 100644 HighWayIot.Plc/packages.config diff --git a/HighWayIot.Plc/HighWayIot.Plc.csproj b/HighWayIot.Plc/HighWayIot.Plc.csproj index 1fac2c2..0dcc92f 100644 --- a/HighWayIot.Plc/HighWayIot.Plc.csproj +++ b/HighWayIot.Plc/HighWayIot.Plc.csproj @@ -63,8 +63,5 @@ - - - \ No newline at end of file diff --git a/HighWayIot.Plc/PlcConnect.cs b/HighWayIot.Plc/PlcConnect.cs index d1b06e9..52fc8ef 100644 --- a/HighWayIot.Plc/PlcConnect.cs +++ b/HighWayIot.Plc/PlcConnect.cs @@ -48,9 +48,6 @@ namespace HighWayIot.Plc logHelper.Error("初始化PLC服务器发生错误!", ex); } - //string s = IsConnect ? "成功" : "失败"; - //logHelper.Info($"PLC连接:{s}"); - return plc; } @@ -62,18 +59,12 @@ namespace HighWayIot.Plc get { if (MelsecInstance == null) return false; - var result = MelsecInstance.ReadPlcType(); - logHelper.Info($"PLC型号 信息:[{result.Message}] 内容:[{result.Message}]"); - return result.IsSuccess; + var result = MelsecInstance.IpAddress; + logHelper.Info($"PLCIP:[{result}]"); + return !string.IsNullOrEmpty(result); } } - public static int Test() - { - var s = IsConnect; - return MelsecInstance.ReadInt16("D1").Content; - } - /// /// 写入数据 /// @@ -131,210 +122,220 @@ namespace HighWayIot.Plc } /// - /// 读取数据 使用泛型 + /// 读取bool /// - /// 读取类型 - /// 地址 - /// 数据类型 + /// /// - public static T PlcRead(string address, DataTypeEnum type) + public static bool ReadBool(string address) { - T result = default; + OperateResult result = new OperateResult(); try { - result = (T)Convert.ChangeType(PlcRead(address, type), typeof(T)); + result = MelsecInstance.ReadBool(address); } - catch(Exception ex) + catch (Exception ex) { - logHelper.Error($"传入类型有误!", ex); + logHelper.Error($"PLC读取Bool发生错误!address:[{address}]", ex); + return default; } - return result; + if (!result.IsSuccess) + { + return default; + } + return result.Content; } /// - /// 读取数据 不使用泛型 + /// 读取Int16 /// - /// 读取类型 - /// 地址 - /// 数据类型 + /// /// - public static object PlcRead(string address, DataTypeEnum type) + public static short ReadInt16(string address) { - object result = default; - var oResult = new OperateResult() { IsSuccess = false, Content = null }; + OperateResult result = new OperateResult(); try { - switch (type) - { - case DataTypeEnum.Bool: - oResult = (OperateResult)Convert.ChangeType(MelsecInstance.ReadBool(address), typeof(OperateResult)); - break; - case DataTypeEnum.Int16: - oResult = (OperateResult)Convert.ChangeType(MelsecInstance.ReadInt16(address), typeof(OperateResult)); - break; - case DataTypeEnum.UInt16: - oResult = (OperateResult)Convert.ChangeType(MelsecInstance.ReadUInt16(address), typeof(OperateResult)); - break; - case DataTypeEnum.Int32: - oResult = (OperateResult)Convert.ChangeType(MelsecInstance.ReadInt32(address), typeof(OperateResult)); - break; - case DataTypeEnum.UInt32: - oResult = (OperateResult)Convert.ChangeType(MelsecInstance.ReadUInt32(address), typeof(OperateResult)); - break; - case DataTypeEnum.Int64: - oResult = (OperateResult)Convert.ChangeType(MelsecInstance.ReadInt64(address), typeof(OperateResult)); - break; - case DataTypeEnum.UInt64: - oResult = (OperateResult)Convert.ChangeType(MelsecInstance.ReadUInt64(address), typeof(OperateResult)); - break; - case DataTypeEnum.Float: - oResult = (OperateResult)Convert.ChangeType(MelsecInstance.ReadFloat(address), typeof(OperateResult)); - break; - case DataTypeEnum.Double: - oResult = (OperateResult)Convert.ChangeType(MelsecInstance.ReadDouble(address), typeof(OperateResult)); - break; - default: - throw new ArgumentException($"Unsupported data type: {type}"); - } - - result = oResult.Content; - logHelper.PlcLog($"Read address:[{address}] value:[{result}] type:[{type.ToString()}] result:[{oResult.IsSuccess}]"); - if (!oResult.IsSuccess) - { - logHelper.Error("读取失败!"); - } + result = MelsecInstance.ReadInt16(address); } catch (Exception ex) { - logHelper.Error("PLC读取数据发生错误!", ex); + logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); + return default; } - return result; + if (!result.IsSuccess) + { + return default; + } + return result.Content; } - ///// - ///// 读取bool - ///// - ///// - ///// - //public static bool ReadBool(string address) - //{ - // var result = MelsecInstance.ReadBool(address); - // return result.IsSuccess && result.Content; - //} + /// + /// 读取UInt16 + /// + /// + /// + public static ushort ReadUInt16(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance.ReadUInt16(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } - ///// - ///// 读取Int16 - ///// - ///// - ///// - //public static short ReadInt16(string address) - //{ - // OperateResult result = new OperateResult(); - // try - // { - // result = MelsecInstance.ReadInt16(address); - // } - // catch (Exception ex) - // { - // logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); - // } - // return result.Content; - //} + /// + /// 读取Int32 + /// + /// + /// + public static int ReadInt32(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance.ReadInt32(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } - ///// - ///// 读取UInt16 - ///// - ///// - ///// - //public static ushort ReadUInt16(string address) - //{ - // OperateResult result = new OperateResult(); - // try - // { - // result = MelsecInstance.ReadUInt16(address); - // } - // catch (Exception ex) - // { - // logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); - // } - // return result.Content; - //} + /// + /// 读取UInt32 + /// + /// + /// + public static uint ReadUInt32(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance.ReadUInt32(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } - ///// - ///// 读取Int32 - ///// - ///// - ///// - //public static int ReadInt32(string address) - //{ - // OperateResult result = new OperateResult(); - // try - // { - // result = MelsecInstance.ReadInt32(address); - // } - // catch (Exception ex) - // { - // logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); - // } - // return result.Content; - //} + /// + /// 读取Int64 + /// + /// + /// + public static long ReadInt64(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance.ReadInt64(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } - ///// - ///// 读取UInt32 - ///// - ///// - ///// - //public static uint ReadUInt32(string address) - //{ - // OperateResult result = new OperateResult(); - // try - // { - // result = MelsecInstance.ReadUInt32(address); - // } - // catch (Exception ex) - // { - // logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); - // } - // return result.Content; - //} + /// + /// 读取UInt64 + /// + /// + /// + public static ulong ReadUInt64(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance.ReadUInt64(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } - ///// - ///// 读取Int64 - ///// - ///// - ///// - //public static long ReadInt64(string address) - //{ - // OperateResult result = new OperateResult(); - // try - // { - // result = MelsecInstance.ReadInt64(address); - // } - // catch (Exception ex) - // { - // logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); - // } - // return result.Content; - //} + /// + /// 读取Float + /// + /// + /// + public static float ReadFloat(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance.ReadFloat(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Float发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } - ///// - ///// 读取UInt64 - ///// - ///// - ///// - //public static ulong ReadUInt64(string address) - //{ - // OperateResult result = new OperateResult(); - // try - // { - // result = MelsecInstance.ReadUInt64(address); - // } - // catch (Exception ex) - // { - // logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); - // } - // return result.Content; - //} + /// + /// 读取Double + /// + /// + /// + public static double ReadDouble(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance.ReadDouble(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Double发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } } diff --git a/HighWayIot.Plc/packages.config b/HighWayIot.Plc/packages.config deleted file mode 100644 index 277ca52..0000000 --- a/HighWayIot.Plc/packages.config +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file diff --git a/HighWayIot.Winform/UserControlPages/TestPage.cs b/HighWayIot.Winform/UserControlPages/TestPage.cs index b6852ce..1986e32 100644 --- a/HighWayIot.Winform/UserControlPages/TestPage.cs +++ b/HighWayIot.Winform/UserControlPages/TestPage.cs @@ -48,7 +48,7 @@ namespace HighWayIot.Winform.UserControlPages private void button2_Click(object sender, EventArgs e) { - var res = PlcConnect.Test(); + var res = PlcConnect.IsConnect; PlcShowValue.Text = res.ToString(); } @@ -59,10 +59,7 @@ namespace HighWayIot.Winform.UserControlPages /// private void ReadButton_Click(object sender, EventArgs e) { - DataTypeEnum type = (DataTypeEnum)Convert.ToInt32(PlcType.SelectedValue); - Type t = GeneralUtils.GetTypeByEnum(type); - var result = PlcConnect.PlcRead(PlcAddress.Text, type); - PlcShowValue.Text = Convert.ToDecimal(result).ToString(); + } ///