diff --git a/HighWayIot.Plc/PlcConnect.cs b/HighWayIot.Plc/PlcConnect.cs index b890643..678adad 100644 --- a/HighWayIot.Plc/PlcConnect.cs +++ b/HighWayIot.Plc/PlcConnect.cs @@ -160,64 +160,64 @@ namespace HighWayIot.Plc //} - /// - /// PLC2写入数据 - /// - /// 地址 - /// 值 - /// 数据类型 - /// - public static OperateResult PlcWrite2(string address, object value, DataTypeEnum type) - { - var result = new OperateResult() { IsSuccess = false }; - try - { - switch (type) - { - case DataTypeEnum.Bool: - result = MelsecInstance2.Write(address, Convert.ToBoolean(value)); - break; - //case DataTypeEnum.Byte: - // result = Instance.Write(address, Convert.ToByte(value)); - // break; - case DataTypeEnum.Int16: - result = MelsecInstance2.Write(address, Convert.ToInt16(value)); - break; - case DataTypeEnum.UInt16: - result = MelsecInstance2.Write(address, Convert.ToUInt16(value)); - break; - case DataTypeEnum.Int32: - result = MelsecInstance2.Write(address, Convert.ToInt32(value)); - break; - case DataTypeEnum.UInt32: - result = MelsecInstance2.Write(address, Convert.ToUInt32(value)); - break; - case DataTypeEnum.Int64: - result = MelsecInstance2.Write(address, Convert.ToInt64(value)); - break; - case DataTypeEnum.UInt64: - result = MelsecInstance2.Write(address, Convert.ToUInt64(value)); - break; - case DataTypeEnum.Float: - result = MelsecInstance2.Write(address, Convert.ToSingle(value)); - break; - case DataTypeEnum.Double: - result = MelsecInstance2.Write(address, Convert.ToDouble(value)); - break; - case DataTypeEnum.String: - result = MelsecInstance2.Write(address, Convert.ToString(value)); - break; - default: - throw new ArgumentException($"Unsupported data type: {type}"); - } - logHelper.PlcLog($"Read address:[{address}] value:[{value}] type:[{type.ToString()}] result:[{result.IsSuccess}]"); - } - catch (Exception ex) - { - logHelper.Error("PLC写入数据发生错误!", ex); - } - return result; - } + ///// + ///// PLC2写入数据 + ///// + ///// 地址 + ///// 值 + ///// 数据类型 + ///// + //public static OperateResult PlcWrite2(string address, object value, DataTypeEnum type) + //{ + // var result = new OperateResult() { IsSuccess = false }; + // try + // { + // switch (type) + // { + // case DataTypeEnum.Bool: + // result = MelsecInstance2.Write(address, Convert.ToBoolean(value)); + // break; + // //case DataTypeEnum.Byte: + // // result = Instance.Write(address, Convert.ToByte(value)); + // // break; + // case DataTypeEnum.Int16: + // result = MelsecInstance2.Write(address, Convert.ToInt16(value)); + // break; + // case DataTypeEnum.UInt16: + // result = MelsecInstance2.Write(address, Convert.ToUInt16(value)); + // break; + // case DataTypeEnum.Int32: + // result = MelsecInstance2.Write(address, Convert.ToInt32(value)); + // break; + // case DataTypeEnum.UInt32: + // result = MelsecInstance2.Write(address, Convert.ToUInt32(value)); + // break; + // case DataTypeEnum.Int64: + // result = MelsecInstance2.Write(address, Convert.ToInt64(value)); + // break; + // case DataTypeEnum.UInt64: + // result = MelsecInstance2.Write(address, Convert.ToUInt64(value)); + // break; + // case DataTypeEnum.Float: + // result = MelsecInstance2.Write(address, Convert.ToSingle(value)); + // break; + // case DataTypeEnum.Double: + // result = MelsecInstance2.Write(address, Convert.ToDouble(value)); + // break; + // case DataTypeEnum.String: + // result = MelsecInstance2.Write(address, Convert.ToString(value)); + // break; + // default: + // throw new ArgumentException($"Unsupported data type: {type}"); + // } + // logHelper.PlcLog($"Read address:[{address}] value:[{value}] type:[{type.ToString()}] result:[{result.IsSuccess}]"); + // } + // catch (Exception ex) + // { + // logHelper.Error("PLC写入数据发生错误!", ex); + // } + // return result; + //} ///// ///// 字符串读取1 @@ -460,246 +460,246 @@ namespace HighWayIot.Plc // return result.Content; //} - /// - /// 字符串读取2 - /// - /// - /// - /// - public static string ReadString2(string address, ushort length) - { - OperateResult result = new OperateResult(); - try - { - result = MelsecInstance2.ReadString(address, length); - } - catch (Exception ex) - { - logHelper.Error($"PLC2读取String发生错误!address:[{address}]", ex); - return default; - } - if (!result.IsSuccess) - { - return default; - } - return result.Content; - } + ///// + ///// 字符串读取2 + ///// + ///// + ///// + ///// + //public static string ReadString2(string address, ushort length) + //{ + // OperateResult result = new OperateResult(); + // try + // { + // result = MelsecInstance2.ReadString(address, length); + // } + // catch (Exception ex) + // { + // logHelper.Error($"PLC2读取String发生错误!address:[{address}]", ex); + // return default; + // } + // if (!result.IsSuccess) + // { + // return default; + // } + // return result.Content; + //} - /// - /// 读取bool - /// - /// - /// - public static bool ReadBool2(string address) - { - OperateResult result = new OperateResult(); - try - { - result = MelsecInstance2.ReadBool(address); - } - catch (Exception ex) - { - logHelper.Error($"PLC读取Bool发生错误!address:[{address}]", ex); - return default; - } - if (!result.IsSuccess) - { - return default; - } - return result.Content; - } + ///// + ///// 读取bool + ///// + ///// + ///// + //public static bool ReadBool2(string address) + //{ + // OperateResult result = new OperateResult(); + // try + // { + // result = MelsecInstance2.ReadBool(address); + // } + // catch (Exception ex) + // { + // logHelper.Error($"PLC读取Bool发生错误!address:[{address}]", ex); + // return default; + // } + // if (!result.IsSuccess) + // { + // return default; + // } + // return result.Content; + //} - /// - /// 读取Int16 - /// - /// - /// - public static short ReadInt162(string address) - { - OperateResult result = new OperateResult(); - try - { - result = MelsecInstance2.ReadInt16(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 ReadInt162(string address) + //{ + // OperateResult result = new OperateResult(); + // try + // { + // result = MelsecInstance2.ReadInt16(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 ReadUInt162(string address) - { - OperateResult result = new OperateResult(); - try - { - result = MelsecInstance2.ReadUInt16(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 ReadUInt162(string address) + //{ + // OperateResult result = new OperateResult(); + // try + // { + // result = MelsecInstance2.ReadUInt16(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 ReadInt322(string address) - { - OperateResult result = new OperateResult(); - try - { - result = MelsecInstance2.ReadInt32(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 ReadInt322(string address) + //{ + // OperateResult result = new OperateResult(); + // try + // { + // result = MelsecInstance2.ReadInt32(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 ReadUInt322(string address) - { - OperateResult result = new OperateResult(); - try - { - result = MelsecInstance2.ReadUInt32(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 ReadUInt322(string address) + //{ + // OperateResult result = new OperateResult(); + // try + // { + // result = MelsecInstance2.ReadUInt32(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 ReadInt642(string address) - { - OperateResult result = new OperateResult(); - try - { - result = MelsecInstance2.ReadInt64(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 ReadInt642(string address) + //{ + // OperateResult result = new OperateResult(); + // try + // { + // result = MelsecInstance2.ReadInt64(address); + // } + // catch (Exception ex) + // { + // logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); + // return default; + // } + // if (!result.IsSuccess) + // { + // return default; + // } + // return result.Content; + //} - /// - /// 读取UInt64 - /// - /// - /// - public static ulong ReadUInt642(string address) - { - OperateResult result = new OperateResult(); - try - { - result = MelsecInstance2.ReadUInt64(address); - } - catch (Exception ex) - { - logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); - return default; - } - if (!result.IsSuccess) - { - return default; - } - return result.Content; - } + ///// + ///// 读取UInt64 + ///// + ///// + ///// + //public static ulong ReadUInt642(string address) + //{ + // OperateResult result = new OperateResult(); + // try + // { + // result = MelsecInstance2.ReadUInt64(address); + // } + // catch (Exception ex) + // { + // logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); + // return default; + // } + // if (!result.IsSuccess) + // { + // return default; + // } + // return result.Content; + //} - /// - /// 读取Float - /// - /// - /// - public static float ReadFloat2(string address) - { - OperateResult result = new OperateResult(); - try - { - result = MelsecInstance2.ReadFloat(address); - } - catch (Exception ex) - { - logHelper.Error($"PLC读取Float发生错误!address:[{address}]", ex); - return default; - } - if (!result.IsSuccess) - { - return default; - } - return result.Content; - } + ///// + ///// 读取Float + ///// + ///// + ///// + //public static float ReadFloat2(string address) + //{ + // OperateResult result = new OperateResult(); + // try + // { + // result = MelsecInstance2.ReadFloat(address); + // } + // catch (Exception ex) + // { + // logHelper.Error($"PLC读取Float发生错误!address:[{address}]", ex); + // return default; + // } + // if (!result.IsSuccess) + // { + // return default; + // } + // return result.Content; + //} - /// - /// 读取Double - /// - /// - /// - public static double ReadDouble2(string address) - { - OperateResult result = new OperateResult(); + ///// + ///// 读取Double + ///// + ///// + ///// + //public static double ReadDouble2(string address) + //{ + // OperateResult result = new OperateResult(); - try - { - result = MelsecInstance2.ReadDouble(address); - } - catch (Exception ex) - { - logHelper.Error($"PLC读取Double发生错误!address:[{address}]", ex); - return default; - } - if (!result.IsSuccess) - { - return default; - } - return result.Content; - } + // try + // { + // result = MelsecInstance2.ReadDouble(address); + // } + // catch (Exception ex) + // { + // logHelper.Error($"PLC读取Double发生错误!address:[{address}]", ex); + // return default; + // } + // if (!result.IsSuccess) + // { + // return default; + // } + // return result.Content; + //} } } \ No newline at end of file diff --git a/HighWayIot.Plc/PlcHelper/RecipeParaHelper.cs b/HighWayIot.Plc/PlcHelper/RecipeParaHelper.cs index 596d29a..d00c09f 100644 --- a/HighWayIot.Plc/PlcHelper/RecipeParaHelper.cs +++ b/HighWayIot.Plc/PlcHelper/RecipeParaHelper.cs @@ -24,7 +24,7 @@ namespace HighWayIot.Plc.PlcHelper //SPEC编号写入 try { - if (!PlcConnect.PlcWrite2("D206", uint.Parse(paraEntity.SpecCode), DataTypeEnum.UInt32).IsSuccess) + if (!PlcConnect.MelsecInstance2.Write("D206", uint.Parse(paraEntity.SpecCode)).IsSuccess) return false; } catch @@ -187,7 +187,7 @@ namespace HighWayIot.Plc.PlcHelper } //读取SPEC编号 - paraEntity.SpecCode = PlcConnect.ReadUInt322("D206").ToString(); + paraEntity.SpecCode = PlcConnect.MelsecInstance2.ReadUInt32("D206").ToString(); //读取SPEC名称 paraEntity.SpecName = PlcConnect.MelsecInstance2.ByteTransform.TransString(data, 0, 10, Encoding.ASCII); diff --git a/HighWayIot.Plc/PlcHelper/TransferSingal.cs b/HighWayIot.Plc/PlcHelper/TransferSingal.cs index 5d8496e..cca4105 100644 --- a/HighWayIot.Plc/PlcHelper/TransferSingal.cs +++ b/HighWayIot.Plc/PlcHelper/TransferSingal.cs @@ -25,19 +25,19 @@ namespace HighWayIot.Plc.PlcHelper switch (stationNo) { case 1: - result = PlcConnect.PlcWrite2("B201", true, DataTypeEnum.Bool).IsSuccess; + result = PlcConnect.MelsecInstance2.Write("B201", true).IsSuccess; break; case 2: - result = PlcConnect.PlcWrite2("B202", true, DataTypeEnum.Bool).IsSuccess; + result = PlcConnect.MelsecInstance2.Write("B202", true).IsSuccess; break; case 3: - result = PlcConnect.PlcWrite2("B203", true, DataTypeEnum.Bool).IsSuccess; + result = PlcConnect.MelsecInstance2.Write("B203", true).IsSuccess; break; case 4: - result = PlcConnect.PlcWrite2("B205", true, DataTypeEnum.Bool).IsSuccess; + result = PlcConnect.MelsecInstance2.Write("B205", true).IsSuccess; break; case 5: - result = PlcConnect.PlcWrite2("B207", true, DataTypeEnum.Bool).IsSuccess; + result = PlcConnect.MelsecInstance2.Write("B207", true).IsSuccess; break; default: break; @@ -59,19 +59,19 @@ namespace HighWayIot.Plc.PlcHelper switch (stationNo) { case 1: - result = PlcConnect.PlcWrite2("B211", true, DataTypeEnum.Bool).IsSuccess; + result = PlcConnect.MelsecInstance2.Write("B211", true).IsSuccess; break; case 2: - result = PlcConnect.PlcWrite2("B212", true, DataTypeEnum.Bool).IsSuccess; + result = PlcConnect.MelsecInstance2.Write("B212", true).IsSuccess; break; case 3: - result = PlcConnect.PlcWrite2("B213", true, DataTypeEnum.Bool).IsSuccess; + result = PlcConnect.MelsecInstance2.Write("B213", true).IsSuccess; break; case 4: - result = PlcConnect.PlcWrite2("B215", true, DataTypeEnum.Bool).IsSuccess; + result = PlcConnect.MelsecInstance2.Write("B215", true).IsSuccess; break; case 5: - result = PlcConnect.PlcWrite2("B217", true, DataTypeEnum.Bool).IsSuccess; + result = PlcConnect.MelsecInstance2.Write("B217", true).IsSuccess; break; default: break; diff --git a/HighWayIot.Plc/PlcHelper/WorkStationHelper.cs b/HighWayIot.Plc/PlcHelper/WorkStationHelper.cs index df58812..7eff691 100644 --- a/HighWayIot.Plc/PlcHelper/WorkStationHelper.cs +++ b/HighWayIot.Plc/PlcHelper/WorkStationHelper.cs @@ -29,7 +29,7 @@ namespace HighWayIot.Plc.PlcHelper //选择是小车的哪个点位 point += rgvStationNo - 1; - bool result = PlcConnect.PlcWrite2($"B{point.ToString("X")}", true, DataTypeEnum.Bool).IsSuccess; + bool result = PlcConnect.MelsecInstance2.Write($"B{point.ToString("X")}", true).IsSuccess; if (result) { LogHelper.Instance.Info($"B{point.ToString("X")} 点置True"); diff --git a/HighWayIot.Winform/Business/RecipeSendBusiness.cs b/HighWayIot.Winform/Business/RecipeSendBusiness.cs index e702782..d6ef5d3 100644 --- a/HighWayIot.Winform/Business/RecipeSendBusiness.cs +++ b/HighWayIot.Winform/Business/RecipeSendBusiness.cs @@ -71,7 +71,7 @@ namespace HighWayIot.Winform.Business } if (recipeParaHelper.UploadToPLC(recipeParaInfo, zxRecipePositionParaEntities)) { - PlcConnect.PlcWrite2($"B{(item.Key + 0x901).ToString("X")}", false, DataTypeEnum.Bool); + PlcConnect.MelsecInstance2.Write($"B{(item.Key + 0x901).ToString("X")}", false); int deviceNo = schedulingInfo.Single(x => x.Id == item.Key + 1).DeviceNo ?? 0; MonitorInsert(recipeNo, $"{deviceNo}上模"); } @@ -88,7 +88,7 @@ namespace HighWayIot.Winform.Business } if (recipeParaHelper.UploadToPLC(recipeParaInfo, zxRecipePositionParaEntities)) { - PlcConnect.PlcWrite2($"B{(item.Key + 0x941).ToString("X")}", false, DataTypeEnum.Bool); + PlcConnect.MelsecInstance2.Write($"B{(item.Key + 0x941).ToString("X")}", false); int deviceNo = schedulingInfo.Single(x => x.Id == item.Key + 1 - 62).DeviceNo ?? 0; MonitorInsert(recipeNo, $"{deviceNo}下模"); } diff --git a/HighWayIot.Winform/UserControlPages/RecipeConfigPages/RecipeConfigPage.cs b/HighWayIot.Winform/UserControlPages/RecipeConfigPages/RecipeConfigPage.cs index 4144ac0..8c88da3 100644 --- a/HighWayIot.Winform/UserControlPages/RecipeConfigPages/RecipeConfigPage.cs +++ b/HighWayIot.Winform/UserControlPages/RecipeConfigPages/RecipeConfigPage.cs @@ -140,9 +140,13 @@ namespace HighWayIot.Winform.UserControlPages if (res.IsSuccess) { //读取SPEC编号 - PlcSpecNoLabel.Text = PlcConnect.ReadUInt322("D206").ToString(); + PlcSpecNoLabel.Text = PlcConnect.MelsecInstance2.ReadUInt32("D206").ToString(); //读取SPEC名称 - PlcSpecNameLabel.Text = PlcConnect.ReadString2("D290", 10).ToString(); + PlcSpecNameLabel.Text = PlcConnect.MelsecInstance2.ReadString("D290", 10).ToString(); + } + else + { + } } catch diff --git a/HighWayIot.Winform/UserControlPages/TestPage.Designer.cs b/HighWayIot.Winform/UserControlPages/TestPage.Designer.cs index d65acdb..8cfafec 100644 --- a/HighWayIot.Winform/UserControlPages/TestPage.Designer.cs +++ b/HighWayIot.Winform/UserControlPages/TestPage.Designer.cs @@ -83,7 +83,6 @@ this.WriteButton.TabIndex = 3; this.WriteButton.Text = "写"; this.WriteButton.UseVisualStyleBackColor = true; - this.WriteButton.Click += new System.EventHandler(this.WriteButton_Click); // // PlcAddress // diff --git a/HighWayIot.Winform/UserControlPages/TestPage.cs b/HighWayIot.Winform/UserControlPages/TestPage.cs index 1d0876a..29a6362 100644 --- a/HighWayIot.Winform/UserControlPages/TestPage.cs +++ b/HighWayIot.Winform/UserControlPages/TestPage.cs @@ -168,23 +168,7 @@ namespace HighWayIot.Winform.UserControlPages /// private void WriteButton_Click(object sender, EventArgs e) { - if ((DataTypeEnum)Convert.ToInt32(PlcType.SelectedValue) != DataTypeEnum.String) - { - if (!decimal.TryParse(PlcValue.Text, out decimal value)) - { - MessageBox.Show("类型转换错误!"); - } - var result = PlcConnect.PlcWrite2(PlcAddress.Text, value, (DataTypeEnum)Convert.ToInt32(PlcType.SelectedValue)); - bool r = result.IsSuccess; - PlcShowValue.Text = r.ToString(); - } - else - { - string value = PlcValue.Text; - var result = PlcConnect.PlcWrite2(PlcAddress.Text, value, (DataTypeEnum)Convert.ToInt32(PlcType.SelectedValue)); - bool r = result.IsSuccess; - PlcShowValue.Text = r.ToString(); - } + } } }