From 53d459641ef324915e0f0a6209234bd43f9dfa1a Mon Sep 17 00:00:00 2001 From: gongxh Date: Thu, 19 May 2022 13:02:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CleanMachineHMI/Utils/PlcOperationHelper.cs | 258 ++++++++++---------- 1 file changed, 131 insertions(+), 127 deletions(-) diff --git a/CleanMachineHMI/Utils/PlcOperationHelper.cs b/CleanMachineHMI/Utils/PlcOperationHelper.cs index cc3b2fc..b364b89 100644 --- a/CleanMachineHMI/Utils/PlcOperationHelper.cs +++ b/CleanMachineHMI/Utils/PlcOperationHelper.cs @@ -337,7 +337,7 @@ namespace CleanMachineHMI { if (PlcItemMd_M_List != null && PlcItemMd_M_List.Count > 0) { - foreach (PlcItem item in PlcItemMd_M_List) + foreach (PlcItem item in PlcItemMd_M_List.Values) { string value = ""; if (Convert.ToInt32(item.PlcItemPriAddress) >= 200) @@ -398,7 +398,7 @@ namespace CleanMachineHMI { if (PlcItemMd_D_List != null && PlcItemMd_D_List.Count > 0) { - foreach (PlcItem item in PlcItemMd_D_List) + foreach (PlcItem item in PlcItemMd_D_List.Values) { var value = ""; if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress)) @@ -892,33 +892,33 @@ namespace CleanMachineHMI try { #endif - List changeItems = new List(); - bool[] readBytArr_X = master.ReadInputs(slaveAddress, 1024, 50); - if (readBytArr_X.Length > 0) - //if(true) - { - if (PlcItemMd_X_List != null && PlcItemMd_X_List.Count > 0) + List changeItems = new List(); + bool[] readBytArr_X = master.ReadInputs(slaveAddress, 1024, 50); + if (readBytArr_X.Length > 0) + //if(true) { - foreach (PlcItem item in PlcItemMd_X_List) + if (PlcItemMd_X_List != null && PlcItemMd_X_List.Count > 0) { - string value = ""; - if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress)) + foreach (PlcItem item in PlcItemMd_X_List.Values) { - string PlcItemId = item.PlcItemId.ToString(); - string plcdatatype = item.PlcItemDataType.ToString(); - int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress); - value = readBytArr_X[plcItemDeviationAddress].ToString(); - if (value != null) + string value = ""; + if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress)) { - if (item.PlcItemValue != value) + string PlcItemId = item.PlcItemId.ToString(); + string plcdatatype = item.PlcItemDataType.ToString(); + int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress); + value = readBytArr_X[plcItemDeviationAddress].ToString(); + if (value != null) { - haschange = true; - item.PlcItemValue = value; - // this.DoShowX_Y_Status(item); - changeItems.Add(item); - // Global.ShowX_Y_Status(item, new EventArgs()); - Global.ShowSensorStatus(item, new EventArgs()); - } + if (item.PlcItemValue != value) + { + haschange = true; + item.PlcItemValue = value; + // this.DoShowX_Y_Status(item); + changeItems.Add(item); + // Global.ShowX_Y_Status(item, new EventArgs()); + Global.ShowSensorStatus(item, new EventArgs()); + } } @@ -953,22 +953,22 @@ namespace CleanMachineHMI #endif - List changeItems = new List(); - bool[] readBytArr_Y = master.ReadCoils(slaveAddress, 1280, 50); - if (readBytArr_Y.Length > 0) - //if(true) - { - if (PlcItemMd_Y_List != null && PlcItemMd_Y_List.Count > 0) + List changeItems = new List(); + bool[] readBytArr_Y = master.ReadCoils(slaveAddress, 1280, 50); + if (readBytArr_Y.Length > 0) + //if(true) { - foreach (PlcItem item in PlcItemMd_Y_List) + if (PlcItemMd_Y_List != null && PlcItemMd_Y_List.Count > 0) { - string value = ""; - if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress)) + foreach (PlcItem item in PlcItemMd_Y_List.Values) { - string PlcItemId = item.PlcItemId.ToString(); - string plcdatatype = item.PlcItemDataType.ToString(); - int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress); - value = readBytArr_Y[plcItemDeviationAddress].ToString(); + string value = ""; + if (!string.IsNullOrEmpty(item.PlcItemDeviationAddress)) + { + string PlcItemId = item.PlcItemId.ToString(); + string plcdatatype = item.PlcItemDataType.ToString(); + int plcItemDeviationAddress = Convert.ToInt32(item.PlcItemDeviationAddress); + value = readBytArr_Y[plcItemDeviationAddress].ToString(); if (value != null) { @@ -1061,79 +1061,82 @@ namespace CleanMachineHMI switch (itemType) { case PlcItemTypeDesc.M: + PlcItemMd_M_List.TryGetValue(plcItemCode, out var item_M); - PlcItem item_M = PlcItemMd_M_List.Find(x => x.PlcItemCode == plcItemCode); - if (item_M == null) - { - return null; - } - if (string.IsNullOrEmpty(item_M.PlcItemValue)) - { - retObj = false; - } - else - { - retObj = bool.Parse(item_M.PlcItemValue); - } - break; - case PlcItemTypeDesc.D: - PlcItem item_D = PlcItemMd_D_List.Find(x => x.PlcItemCode == plcItemCode); - if (item_D == null) - { - return null; - } - if (string.IsNullOrEmpty(item_D.PlcItemValue)) - { - retObj = 0; - } - else - { - if (item_D.PlcItemDataLength == 2) + if (item_M == null) { - retObj = int.Parse(item_D.PlcItemValue); + return null; + } + if (string.IsNullOrEmpty(item_M.PlcItemValue)) + { + retObj = false; } else { - retObj = short.Parse(item_D.PlcItemValue); + retObj = bool.Parse(item_M.PlcItemValue); } + break; + case PlcItemTypeDesc.D: + PlcItemMd_D_List.TryGetValue(plcItemCode, out var item_D); + // PlcItem item_D = PlcItemMd_D_List.Find(x => x.PlcItemCode == plcItemCode); + if (item_D == null) + { + return null; + } + if (string.IsNullOrEmpty(item_D.PlcItemValue)) + { + retObj = 0; + } + else + { + if (item_D.PlcItemDataLength == 2) + { + retObj = int.Parse(item_D.PlcItemValue); + } + else + { + retObj = short.Parse(item_D.PlcItemValue); + } - } - break; - case PlcItemTypeDesc.X: - PlcItem item_X = PlcItemMd_X_List.Find(x => x.PlcItemCode == plcItemCode); - if (item_X == null) - { - return null; - } - if (string.IsNullOrEmpty(item_X.PlcItemValue)) - { - retObj = false; - } - else - { - retObj = bool.Parse(item_X.PlcItemValue); - } - break; - case PlcItemTypeDesc.Y: - PlcItem item_Y = PlcItemMd_Y_List.Find(x => x.PlcItemCode == plcItemCode); - if (item_Y == null) - { - return null; - } - if (string.IsNullOrEmpty(item_Y.PlcItemValue)) - { - retObj = false; - } - else - { - retObj = bool.Parse(item_Y.PlcItemValue); - } - break; - default: - retObj = null; - break; - } - return retObj; + } + break; + case PlcItemTypeDesc.X: + PlcItemMd_X_List.TryGetValue(plcItemCode, out var item_X); + //PlcItem item_X = PlcItemMd_X_List.Find(x => x.PlcItemCode == plcItemCode); + if (item_X == null) + { + return null; + } + if (string.IsNullOrEmpty(item_X.PlcItemValue)) + { + retObj = false; + } + else + { + retObj = bool.Parse(item_X.PlcItemValue); + } + break; + case PlcItemTypeDesc.Y: + PlcItemMd_Y_List.TryGetValue(plcItemCode, out var item_Y); + // PlcItem item_Y = PlcItemMd_Y_List.Find(x => x.PlcItemCode == plcItemCode); + if (item_Y == null) + { + return null; + } + if (string.IsNullOrEmpty(item_Y.PlcItemValue)) + { + retObj = false; + } + else + { + retObj = bool.Parse(item_Y.PlcItemValue); + } + break; + default: + retObj = null; + break; + } + return retObj; #if RELEASE } catch (Exception ex) @@ -1151,30 +1154,31 @@ namespace CleanMachineHMI try { #endif - switch (itemType) - { - case PlcItemTypeDesc.M: - PlcItem item_M = PlcItemMd_M_List.Find(x => x.PlcItemCode == plcItemCode); - if (item_M == null) - { - return false; - } - master.WriteSingleCoil(slaveAddress, Convert.ToUInt16(item_M.PlcItemModbusAddress), bool.Parse(writeValue)); - return true; - case PlcItemTypeDesc.Y: - PlcItem item_Y = PlcItemMd_Y_List.Find(x => x.PlcItemCode == plcItemCode); - if (item_Y == null) - { - return false; - } - master.WriteSingleCoil(slaveAddress, Convert.ToUInt16(item_Y.PlcItemModbusAddress), bool.Parse(writeValue)); - return true; - case PlcItemTypeDesc.D: - PlcItem item_D = PlcItemMd_D_List.Find(x => x.PlcItemCode == plcItemCode); - if (item_D == null) - { - return false; - } + switch (itemType) + { + case PlcItemTypeDesc.M: + PlcItemMd_M_List.TryGetValue(plcItemCode, out var item_M); + if (item_M == null) + { + return false; + } + master.WriteSingleCoil(slaveAddress, Convert.ToUInt16(item_M.PlcItemModbusAddress), bool.Parse(writeValue)); + return true; + case PlcItemTypeDesc.Y: + PlcItemMd_Y_List.TryGetValue(plcItemCode, out var item_Y); + + if (item_Y == null) + { + return false; + } + master.WriteSingleCoil(slaveAddress, Convert.ToUInt16(item_Y.PlcItemModbusAddress), bool.Parse(writeValue)); + return true; + case PlcItemTypeDesc.D: + PlcItemMd_D_List.TryGetValue(plcItemCode, out var item_D); + if (item_D == null) + { + return false; + } if (item_D.PlcItemDataLength == 2)