Browse Source

添加返回数据变化时的验证

章政 6 years ago
parent
commit
a3afe09348
2 changed files with 41 additions and 2 deletions
  1. 21 0
      PLCDataReader/Main.cs
  2. 20 2
      PLCDataReader/PublicMethod/BaseUtil.cs

+ 21 - 0
PLCDataReader/Main.cs

@@ -29,6 +29,8 @@ namespace UAS_PLCDataReader
         DataHelper dh = SystemInf.dh;
         ModeBusTCPServer mbt = new ModeBusTCPServer();
         List<string> SQL = new List<string>();
+        //存放所有的设备返回的数据,用于比较
+        Dictionary<string, Dictionary<string, string>> ReturnData = new Dictionary<string, Dictionary<string, string>>();
 
         #region 初始化代码
         public Main()
@@ -423,6 +425,25 @@ namespace UAS_PLCDataReader
                 if (mbt.Returnvalue.ContainsKey(IP))
                 {
                     int ReceiveCommandByteSize = Encoding.Default.GetBytes(mbt.Returnvalue[IP].ToCharArray()).Length;
+                    Dictionary<string, string> ItemData = new Dictionary<string, string>();
+                    int[] Arr = BaseUtil.GetDecimalData(BaseUtil.ASCIIToString(mbt.Returnvalue[IP]), 8);
+                    for (int i = 0; i < Arr.Length; i++)
+                    {
+                        ItemData.Add("Item" + i, Arr[i].ToString());
+                    }
+                    //如果不包含该项数据则在键值对中添加
+                    if (!ReturnData.ContainsKey(Decode))
+                    {
+                        ReturnData.Add(Decode, ItemData);
+                    }
+                    else
+                    {
+                        //检测键值对是否发生变化,发生变化时赋予新值
+                        if ((BaseUtil.CheckDicDiff(ReturnData[Decode], ItemData)))
+                        {
+                            ReturnData[Decode] = ItemData;
+                        }
+                    }
                     mbt.Returnvalue.Remove(IP);
                     SQL.Clear();
                     //更新轮询状态

+ 20 - 2
PLCDataReader/PublicMethod/BaseUtil.cs

@@ -123,7 +123,7 @@ namespace UAS_PLCDataReader.PublicMethod
                     returnStr += Convert.ToString(b[i], 16);//ToString("X2") 为C#中的字符串格式控制符
                 }
             }
-            return returnStr;
+            return returnStr.ToUpper();
         }
 
         public static void CleanMemory()
@@ -168,6 +168,24 @@ namespace UAS_PLCDataReader.PublicMethod
             }
         }
 
+        /// <summary>
+        /// 检测键值对是否发生值的变化
+        /// </summary>
+        public static bool CheckDicDiff(Dictionary<string, string> A, Dictionary<string, string> B)
+        {
+            foreach (var item in B)
+            {
+                if (A.ContainsKey(item.Key))
+                {
+                    if (A[item.Key] != item.Value)
+                    {
+                        return true;
+                    }
+                }
+            }
+            return false;
+        }
+
         /// <summary>
         ///  传入控件的集合和DataTable,通过判断控件的名称和数据源的列的描述来匹配,支持单层的GroupBox和Panel
         /// </summary>
@@ -334,7 +352,7 @@ namespace UAS_PLCDataReader.PublicMethod
                     }
                 }
             }
-            return ReturnStr;
+            return ReturnStr.ToUpper();
         }
 
         public static int[] GetDecimalData(string HexStr, int DataSize)