From aa1770307aee9e5271f87f814bb49e3ec64becfb Mon Sep 17 00:00:00 2001 From: SoulStar Date: Wed, 25 Dec 2024 14:45:37 +0800 Subject: [PATCH] =?UTF-8?q?feat=20-=20plc=E6=B5=8B=E8=AF=95=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HighWayIot.Plc/PlcConnect.cs | 15 ++++++++++++--- HighWayIot.Winform/Business/GeneralUtils.cs | 2 +- HighWayIot.Winform/HighWayIot.Winform.csproj | 1 + HighWayIot.Winform/UserControlPages/TestPage.cs | 10 ++++++++-- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/HighWayIot.Plc/PlcConnect.cs b/HighWayIot.Plc/PlcConnect.cs index b134379..551442c 100644 --- a/HighWayIot.Plc/PlcConnect.cs +++ b/HighWayIot.Plc/PlcConnect.cs @@ -42,6 +42,9 @@ namespace HighWayIot.Plc logHelper.Error("初始化PLC服务器发生错误!", ex); } + string s = IsConnect ? "正常" : "异常"; + logHelper.Info($"PLC连接:{s}"); + return plc; } @@ -53,6 +56,7 @@ namespace HighWayIot.Plc get { var result = MelsecInstance.ReadPlcType(); + logHelper.Info($"PLC型号:{result.Content}"); return result.IsSuccess; } } @@ -123,9 +127,14 @@ namespace HighWayIot.Plc public static T PlcRead(string address, DataTypeEnum type) { T result = default; - - result = (T)Convert.ChangeType(PlcRead(address, type), typeof(T)); - + try + { + result = (T)Convert.ChangeType(PlcRead(address, type), typeof(T)); + } + catch(Exception ex) + { + logHelper.Error($"传入类型有误!", ex); + } return result; } diff --git a/HighWayIot.Winform/Business/GeneralUtils.cs b/HighWayIot.Winform/Business/GeneralUtils.cs index 77297c6..30284f2 100644 --- a/HighWayIot.Winform/Business/GeneralUtils.cs +++ b/HighWayIot.Winform/Business/GeneralUtils.cs @@ -97,7 +97,7 @@ namespace HighWayIot.Winform.Business case DataTypeEnum.Double: return typeof(double); default: - throw new ArgumentException($"Unsupported data type: {type}"); + return default; } } } diff --git a/HighWayIot.Winform/HighWayIot.Winform.csproj b/HighWayIot.Winform/HighWayIot.Winform.csproj index dbb7fd8..c1fb5dc 100644 --- a/HighWayIot.Winform/HighWayIot.Winform.csproj +++ b/HighWayIot.Winform/HighWayIot.Winform.csproj @@ -36,6 +36,7 @@ + diff --git a/HighWayIot.Winform/UserControlPages/TestPage.cs b/HighWayIot.Winform/UserControlPages/TestPage.cs index f195a49..944197c 100644 --- a/HighWayIot.Winform/UserControlPages/TestPage.cs +++ b/HighWayIot.Winform/UserControlPages/TestPage.cs @@ -59,7 +59,10 @@ namespace HighWayIot.Winform.UserControlPages /// private void ReadButton_Click(object sender, EventArgs e) { - var result = PlcConnect.PlcRead(PlcAddress.Text, (DataTypeEnum)Convert.ToInt32(PlcType.SelectedValue)); + 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(); } /// @@ -69,7 +72,10 @@ namespace HighWayIot.Winform.UserControlPages /// private void WriteButton_Click(object sender, EventArgs e) { - decimal.TryParse(PlcValue.Text, out decimal value); + if(!decimal.TryParse(PlcValue.Text, out decimal value)) + { + MessageBox.Show("类型转换错误!"); + } var result = PlcConnect.PlcWrite(PlcAddress.Text, value, (DataTypeEnum)Convert.ToInt32(PlcType.SelectedValue)); bool r = result.IsSuccess; PlcShowValue.Text = r.ToString();