|
- 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;
- }
-
- public static string ToSystemData(this EDataType eData)
- {
- switch (eData)
- {
- case EDataType.Bool: return "bool";
- case EDataType.Byte: return "byte";
- case EDataType.Int: return "short";
- case EDataType.Word: return "ushort";
- case EDataType.Dint: return "int";
- case EDataType.Dword: return "uint";
- case EDataType.Float: return "float";
- case EDataType.Double: return "double";
- case EDataType.String: return "string";
- default:
- break;
- }
- return default;
- }
-
- /// <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;
- }
- }
- }
|