|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace BPASmart.Model
- {
- public static class ExpandMethod
- {
- /// <summary>
- /// 获取数据类型的大小
- /// </summary>
- /// <param name="eDataType"></param>
- /// <returns></returns>
- public static ushort GetEDataSize(this EDataType eDataType)
- {
- switch (eDataType)
- {
- case EDataType.Bool:
- case EDataType.Byte:
- case EDataType.Int:
- case EDataType.Word:
- return 1;
- case EDataType.Dint:
- case EDataType.Dword:
- case EDataType.Float:
- return 2;
- case EDataType.Double:
- break;
- case EDataType.String:
- break;
- default:
- break;
- }
- return 1;
- }
-
- /// <summary>
- /// 获取Modbus Tcp 连续变量数据的组对象
- /// </summary>
- /// <param name="variableInfos"></param>
- /// <param name="by"></param>
- /// <returns></returns>
- public static List<ReadDataModel> GetDataGroup(this IGrouping<string, VariableInfo> variableInfos, int by = 1)
- {
- List<ReadDataModel> ReturnValue = new List<ReadDataModel>();
- var res = variableInfos?.OrderBy(p => p.RealAddress).ToList();
- List<int> RealAddresss = new List<int>();
- variableInfos.ToList()?.ForEach(item => { if (int.TryParse(item.RealAddress, out int add)) RealAddresss.Add(add); });
- int count = 0;
- if (res != null)
- {
- int address = RealAddresss.Min();
- int startAddress = address;
- for (int i = 0; i < res.Count; i++)
- {
- if (int.TryParse(res.ElementAt(i).RealAddress, out int TempAddress))
- {
- if (TempAddress == address)
- {
- count++;
- address += by;
- }
- else
- {
- ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count });
- count = 1;
- address = TempAddress + by;
- startAddress = TempAddress;
- }
- }
-
- }
- ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count });
- }
- return ReturnValue;
- }
-
- public static Dictionary<EDataType, List<ReadDataModel>> GetReadDataModels(this ObservableCollection<VariableInfo> varialeInfos)
- {
- Dictionary<EDataType, List<ReadDataModel>> readDataModels = new Dictionary<EDataType, List<ReadDataModel>>();
- varialeInfos.GroupBy(p => p.DataType)?.ToList()?.ForEach(tempVar =>
- {
- if (tempVar.Key != null && tempVar.Key.Length > 0)
- {
- EDataType dataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.Key);
- switch (dataType)
- {
- case EDataType.Bool:
- case EDataType.Byte:
- case EDataType.Int:
- case EDataType.Word:
- if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, tempVar.GetDataGroup());
- break;
- case EDataType.Dint:
- case EDataType.Dword:
- case EDataType.Float:
- if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, tempVar.GetDataGroup(2));
- break;
- default:
- break;
- }
- }
- });
- return readDataModels;
- }
- }
- }
|