终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExpandMethod.cs 4.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace BPASmart.Model
  8. {
  9. public static class ExpandMethod
  10. {
  11. /// <summary>
  12. /// 获取数据类型的大小
  13. /// </summary>
  14. /// <param name="eDataType"></param>
  15. /// <returns></returns>
  16. public static ushort GetEDataSize(this EDataType eDataType)
  17. {
  18. switch (eDataType)
  19. {
  20. case EDataType.Bool:
  21. case EDataType.Byte:
  22. case EDataType.Int:
  23. case EDataType.Word:
  24. return 1;
  25. case EDataType.Dint:
  26. case EDataType.Dword:
  27. case EDataType.Float:
  28. return 2;
  29. case EDataType.Double:
  30. break;
  31. case EDataType.String:
  32. break;
  33. default:
  34. break;
  35. }
  36. return 1;
  37. }
  38. /// <summary>
  39. /// 获取Modbus Tcp 连续变量数据的组对象
  40. /// </summary>
  41. /// <param name="variableInfos"></param>
  42. /// <param name="by"></param>
  43. /// <returns></returns>
  44. public static List<ReadDataModel> GetDataGroup(this IGrouping<string, VariableInfo> variableInfos, int by = 1)
  45. {
  46. List<ReadDataModel> ReturnValue = new List<ReadDataModel>();
  47. var res = variableInfos?.OrderBy(p => p.RealAddress).ToList();
  48. List<int> RealAddresss = new List<int>();
  49. variableInfos.ToList()?.ForEach(item => { if (int.TryParse(item.RealAddress, out int add)) RealAddresss.Add(add); });
  50. int count = 0;
  51. if (res != null)
  52. {
  53. int address = RealAddresss.Min();
  54. int startAddress = address;
  55. for (int i = 0; i < res.Count; i++)
  56. {
  57. if (int.TryParse(res.ElementAt(i).RealAddress, out int TempAddress))
  58. {
  59. if (TempAddress == address)
  60. {
  61. count++;
  62. address += by;
  63. }
  64. else
  65. {
  66. ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count });
  67. count = 1;
  68. address = TempAddress + by;
  69. startAddress = TempAddress;
  70. }
  71. }
  72. }
  73. ReturnValue.Add(new ReadDataModel() { StartAddress = (ushort)startAddress, Length = (ushort)count });
  74. }
  75. return ReturnValue;
  76. }
  77. public static Dictionary<EDataType, List<ReadDataModel>> GetReadDataModels(this ObservableCollection<VariableInfo> varialeInfos)
  78. {
  79. Dictionary<EDataType, List<ReadDataModel>> readDataModels = new Dictionary<EDataType, List<ReadDataModel>>();
  80. varialeInfos.GroupBy(p => p.DataType)?.ToList()?.ForEach(tempVar =>
  81. {
  82. if (tempVar.Key != null && tempVar.Key.Length > 0)
  83. {
  84. EDataType dataType = (EDataType)Enum.Parse(typeof(EDataType), tempVar.Key);
  85. switch (dataType)
  86. {
  87. case EDataType.Bool:
  88. case EDataType.Byte:
  89. case EDataType.Int:
  90. case EDataType.Word:
  91. if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, tempVar.GetDataGroup());
  92. break;
  93. case EDataType.Dint:
  94. case EDataType.Dword:
  95. case EDataType.Float:
  96. if (!readDataModels.ContainsKey(dataType)) readDataModels.TryAdd(dataType, tempVar.GetDataGroup(2));
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. });
  103. return readDataModels;
  104. }
  105. }
  106. }