XmlVariable.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. public class TypeBase
  7. {
  8. public string Name { get; set; }
  9. public int Size { get; set; }//占用的字节数
  10. public string TypeClass { get; set; }
  11. }
  12. public class TypeSimple:TypeBase
  13. {
  14. public int SwapSize { get; set; }
  15. public string IecName { get; set; }
  16. public int BitOffset { get; set; }
  17. }
  18. public class TypeArray : TypeBase
  19. {
  20. public int NativeSize { get; set; }
  21. public string BaseType { get; set; }
  22. public string IecName { get; set; }
  23. public int[]MinRange; //数组的行值
  24. public int[]MaxRange;
  25. public int Rank { get; set; }//数组的维数
  26. }
  27. public class UserDefElement
  28. {
  29. public string IecName { get; set; }
  30. public string Type { get; set; }
  31. public uint ByteOffset { get; set; }
  32. public string VarType { get; set; }
  33. public string Access { get; set; }
  34. }
  35. public class TypeUserDef : TypeBase
  36. {
  37. private List<UserDefElement> userDefElements;
  38. public TypeUserDef()
  39. {
  40. userDefElements=new List<UserDefElement>();
  41. }
  42. public int NativeSize { get; set; }
  43. public string PouClass { get; set; }
  44. public string IecName { get; set; }
  45. public List<UserDefElement> UserDefElements { get => userDefElements; set => userDefElements = value; }
  46. }
  47. public class XmlVariable
  48. {
  49. public string Name { get; set; }
  50. public string DisplayName
  51. {
  52. get
  53. {
  54. string s = Name;
  55. if (!string.IsNullOrEmpty(Comment))
  56. {
  57. s += $" <{Comment}>";
  58. }
  59. if (!string.IsNullOrEmpty(Type))
  60. {
  61. s += $" ({ParseTypeString(Type)})";
  62. }
  63. return s;
  64. }
  65. }
  66. public string Type { get; set; }
  67. public string Access { get; set; }
  68. public string Comment { get; set; }
  69. public bool IsLeaf { get; set; }//是否是叶子节点(没有子项)
  70. public List<XmlVariable> Children { get; set; } = new List<XmlVariable>();
  71. private static string ParseTypeString(string input)
  72. {
  73. if (!input.StartsWith("T_"))
  74. return input; // 不符合格式,原样返回
  75. string typePart = input.Substring(2); // 去掉 T_
  76. if (!typePart.StartsWith("ARRAY__"))
  77. return typePart; // 简单类型,如 BOOL、INT 等
  78. // 分解数组部分和元素类型
  79. int ofIndex = typePart.IndexOf("__OF_");
  80. if (ofIndex == -1)
  81. return typePart; // 格式错误,原样返回
  82. string arrayPart = typePart.Substring(0, ofIndex);
  83. string elementType = typePart.Substring(ofIndex + 5); // "OF_".Length == 3, "__OF_" 是 5 个字符
  84. // 解析数组维度
  85. string[] ranges = arrayPart.Split(new string[] { "__" },
  86. StringSplitOptions.RemoveEmptyEntries);
  87. if ((ranges.Length-1) % 2 != 0)
  88. return typePart; // 维度解析失败,原样返回
  89. List<string> dimensions = new List<string>();
  90. for (int i = 1; i < ranges.Length; i += 2)
  91. {
  92. string lower = ranges[i];
  93. string upper = ranges[i + 1];
  94. dimensions.Add($"{lower}..{upper}");
  95. }
  96. string dimensionStr = string.Join(",", dimensions);
  97. return $"ARRAY[{dimensionStr}] OF {elementType}";
  98. }
  99. }