From 9f728c289da765f87e5de38d6dc534725976706a Mon Sep 17 00:00:00 2001 From: SoulStar Date: Mon, 28 Apr 2025 13:49:34 +0800 Subject: [PATCH] =?UTF-8?q?fix=20-=20=E9=85=8D=E6=96=B9=E5=86=99=E5=85=A5P?= =?UTF-8?q?LC=E6=95=B4=E5=9D=97=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HighWayIot.Plc/PlcHelper/RecipeParaHelper.cs | 91 +++++++++++--------- 1 file changed, 50 insertions(+), 41 deletions(-) diff --git a/HighWayIot.Plc/PlcHelper/RecipeParaHelper.cs b/HighWayIot.Plc/PlcHelper/RecipeParaHelper.cs index f92766a..7e00e48 100644 --- a/HighWayIot.Plc/PlcHelper/RecipeParaHelper.cs +++ b/HighWayIot.Plc/PlcHelper/RecipeParaHelper.cs @@ -21,7 +21,7 @@ namespace HighWayIot.Plc.PlcHelper /// public bool UploadToPLC(ZxRecipeParaEntity paraEntity, List positionParaEntity) { - //byte[] bytes = new byte[240]; + byte[] bytes = new byte[240]; //SPEC编号写入 try { @@ -34,14 +34,15 @@ namespace HighWayIot.Plc.PlcHelper } //SPEC名称写入 - if (!PlcConnect.PlcWrite2("D290", paraEntity.SpecName, DataTypeEnum.String).IsSuccess) - return false; + var specNameBytes = Encoding.ASCII.GetBytes(paraEntity.SpecName); + Array.Copy(specNameBytes, 0, bytes, 0, specNameBytes.Length); + //if (!PlcConnect.PlcWrite2("D290", paraEntity.SpecName, DataTypeEnum.String).IsSuccess) + // return false; //工位参数写入 foreach (ZxRecipePositionParaEntity e in positionParaEntity) { - if (!SelectSetPositionPara(e)) - return false; + bytes = SelectSetPositionPara(e, bytes); } //公共参数写入 @@ -56,23 +57,34 @@ namespace HighWayIot.Plc.PlcHelper bitDatas[1] = bitDatas[1].SetBoolByIndex(0, paraEntity.S9 ?? false); ushort bitData = bitDatas.FromBytes(); - if (!PlcConnect.PlcWrite2("D390", bitData, DataTypeEnum.UInt16).IsSuccess) - return false; + //if (!PlcConnect.PlcWrite2("D390", bitData, DataTypeEnum.UInt16).IsSuccess) + // return false; - if (!PlcConnect.PlcWrite2("D391", paraEntity.RimInch ?? 0, DataTypeEnum.UInt16).IsSuccess) - return false; - if (!PlcConnect.PlcWrite2("D392", paraEntity.LightWidth ?? 0, DataTypeEnum.UInt16).IsSuccess) - return false; - if (!PlcConnect.PlcWrite2("D393", paraEntity.SlowDistance ?? 0, DataTypeEnum.UInt16).IsSuccess) - return false; - if (!PlcConnect.PlcWrite2("D394", paraEntity.StopDistance ?? 0, DataTypeEnum.UInt16).IsSuccess) - return false; - if (!PlcConnect.PlcWrite2("D398", paraEntity.TireWeight ?? 0, DataTypeEnum.Float).IsSuccess) - return false; + //if (!PlcConnect.PlcWrite2("D391", paraEntity.RimInch ?? 0, DataTypeEnum.UInt16).IsSuccess) + // return false; + //if (!PlcConnect.PlcWrite2("D392", paraEntity.LightWidth ?? 0, DataTypeEnum.UInt16).IsSuccess) + // return false; + //if (!PlcConnect.PlcWrite2("D393", paraEntity.SlowDistance ?? 0, DataTypeEnum.UInt16).IsSuccess) + // return false; + //if (!PlcConnect.PlcWrite2("D394", paraEntity.StopDistance ?? 0, DataTypeEnum.UInt16).IsSuccess) + // return false; + //if (!PlcConnect.PlcWrite2("D398", paraEntity.TireWeight ?? 0, DataTypeEnum.Float).IsSuccess) + // return false; + var bytes1 = BitConverter.GetBytes(bitData); + var bytes2 = BitConverter.GetBytes((ushort?)paraEntity.RimInch ?? 0); + var bytes3 = BitConverter.GetBytes((ushort?)paraEntity.LightWidth ?? 0); + var bytes4 = BitConverter.GetBytes((ushort?)paraEntity.SlowDistance ?? 0); + var bytes5 = BitConverter.GetBytes((ushort?)paraEntity.StopDistance ?? 0); + var bytes6 = BitConverter.GetBytes(paraEntity.TireWeight ?? 0); + Array.Copy(bytes1, 0, bytes, (390 - 290) * 2, bytes1.Length); + Array.Copy(bytes2, 0, bytes, (391 - 290) * 2, bytes1.Length); + Array.Copy(bytes3, 0, bytes, (392 - 290) * 2, bytes1.Length); + Array.Copy(bytes4, 0, bytes, (393 - 290) * 2, bytes1.Length); + Array.Copy(bytes5, 0, bytes, (394 - 290) * 2, bytes1.Length); + Array.Copy(bytes6, 0, bytes, (398 - 290) * 2, bytes1.Length); - //return PlcConnect.MelsecInstance2.Write("D290", bytes).IsSuccess; - return true; + return PlcConnect.MelsecInstance2.Write("D290", bytes).IsSuccess; } /// @@ -80,38 +92,32 @@ namespace HighWayIot.Plc.PlcHelper /// /// /// - private bool SelectSetPositionPara(ZxRecipePositionParaEntity entity) + private byte[] SelectSetPositionPara(ZxRecipePositionParaEntity entity, byte[] bytes) { if (entity.Position == 1) { - if (!SetPositionPara(entity, 310)) - return false; + return SetPositionPara(entity, 310, bytes); } else if (entity.Position == 2) { - if (!SetPositionPara(entity, 320)) - return false; + return SetPositionPara(entity, 320, bytes); } else if (entity.Position == 3) { - if (!SetPositionPara(entity, 330)) - return false; + return SetPositionPara(entity, 330, bytes); } else if (entity.Position == 4) { - if (!SetPositionPara(entity, 350)) - return false; + return SetPositionPara(entity, 350, bytes); } else if (entity.Position == 5) { - if (!SetPositionPara(entity, 370)) - return false; + return SetPositionPara(entity, 370, bytes); } else { - return false; + return null; } - return true; } /// @@ -120,14 +126,14 @@ namespace HighWayIot.Plc.PlcHelper /// /// /// - private bool SetPositionPara(ZxRecipePositionParaEntity entity, int add) + private byte[] SetPositionPara(ZxRecipePositionParaEntity entity, int add, byte[] bytes) { for (int i = 1; i <= 10; i++) { var prop = entity.GetType().GetProperty($"E{i}"); // 检查属性是否存在 if (prop == null) - return false; + return null; // 获取属性值 object value = prop.GetValue(entity); @@ -136,11 +142,12 @@ namespace HighWayIot.Plc.PlcHelper if (value == null) { // 根据业务需求选择:返回false或使用默认值(例如0) - if (!PlcConnect.PlcWrite2($"D{add}", 0, DataTypeEnum.UInt16).IsSuccess) - return false; + continue; + //if (PlcConnect.PlcWrite2($"D{add}", 0, DataTypeEnum.UInt16).IsSuccess) + // return false; } - // 尝试转换值 + // 尝试转换值 ushort valueToWrite; try { @@ -149,16 +156,18 @@ namespace HighWayIot.Plc.PlcHelper catch { // 转换失败(如类型不匹配) - return false; + continue; } // 写入PLC并检查结果 - if (!PlcConnect.PlcWrite2($"D{add}", valueToWrite, DataTypeEnum.UInt16).IsSuccess) - return false; + var resultbytes = BitConverter.GetBytes(valueToWrite); + Array.Copy(resultbytes, 0, bytes, (add - 290) * 2, resultbytes.Length); + //if (!PlcConnect.PlcWrite2($"D{add}", valueToWrite, DataTypeEnum.UInt16).IsSuccess) + // return false; add++; } - return true; + return bytes; } ///