终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

111 rindas
4.2 KiB

  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. }