EdsGeneratorTest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using CCDCount.DLL.CanBus;
  3. namespace CanTest
  4. {
  5. /// <summary>
  6. /// EDS文件生成测试
  7. /// </summary>
  8. class EdsGeneratorTest
  9. {
  10. static void Main(string[] args)
  11. {
  12. Console.WriteLine("=== EDS文件生成测试 ===\n");
  13. // 创建对象字典
  14. byte nodeId = 5;
  15. var objectDict = new ObjectDictionary(nodeId);
  16. // 生成EDS文件
  17. var edsGenerator = new EdsFileGenerator(nodeId);
  18. string edsPath = "Test_CanOpenSlave.eds";
  19. string edsContent = edsGenerator.Generate(objectDict, edsPath);
  20. Console.WriteLine($"\nEDS文件已生成: {edsPath}");
  21. // 验证EDS文件
  22. VerifyEdsFile(edsPath);
  23. Console.WriteLine("\n按任意键退出...");
  24. Console.ReadKey();
  25. }
  26. static void VerifyEdsFile(string edsPath)
  27. {
  28. Console.WriteLine("\n=== 验证EDS文件 ===");
  29. if (!System.IO.File.Exists(edsPath))
  30. {
  31. Console.WriteLine($"错误: EDS文件不存在: {edsPath}");
  32. return;
  33. }
  34. string content = System.IO.File.ReadAllText(edsPath);
  35. // 检查关键部分
  36. bool hasMandatoryObjects = content.Contains("[MandatoryObjects]");
  37. bool hasObjectList = content.Contains("[ObjectList]");
  38. bool has1000 = content.Contains("[0x1000]");
  39. bool has1001 = content.Contains("[0x1001]");
  40. bool has1018 = content.Contains("[0x1018]");
  41. Console.WriteLine($"[MandatoryObjects] 部分: {(hasMandatoryObjects ? "✓ 存在" : "✗ 缺失")}");
  42. Console.WriteLine($"[ObjectList] 部分: {(hasObjectList ? "✓ 存在" : "✗ 缺失")}");
  43. Console.WriteLine($"0x1000 (Device Type): {(has1000 ? "✓ 存在" : "✗ 缺失")}");
  44. Console.WriteLine($"0x1001 (Error Register): {(has1001 ? "✓ 存在" : "✗ 缺失")}");
  45. Console.WriteLine($"0x1018 (Identity Object): {(has1018 ? "✓ 存在" : "✗ 缺失")}");
  46. // 检查ObjectList中是否包含必选对象
  47. bool objectListHas1000 = false;
  48. bool objectListHas1001 = false;
  49. bool objectListHas1018 = false;
  50. int objectListStart = content.IndexOf("[ObjectList]");
  51. if (objectListStart >= 0)
  52. {
  53. int nextSection = content.IndexOf("[", objectListStart + 12);
  54. string objectListSection = nextSection > 0
  55. ? content.Substring(objectListStart, nextSection - objectListStart)
  56. : content.Substring(objectListStart);
  57. objectListHas1000 = objectListSection.Contains("0x1000");
  58. objectListHas1001 = objectListSection.Contains("0x1001");
  59. objectListHas1018 = objectListSection.Contains("0x1018");
  60. }
  61. Console.WriteLine($"\nObjectList中包含:");
  62. Console.WriteLine($" 0x1000: {(objectListHas1000 ? "✓" : "✗")}");
  63. Console.WriteLine($" 0x1001: {(objectListHas1001 ? "✓" : "✗")}");
  64. Console.WriteLine($" 0x1018: {(objectListHas1018 ? "✓" : "✗")}");
  65. // 检查PDO映射配置
  66. Console.WriteLine($"\n=== PDO映射验证 ===");
  67. CheckPdoMapping(content, "RPDO 1", 0x1600);
  68. CheckPdoMapping(content, "RPDO 2", 0x1601);
  69. CheckPdoMapping(content, "TPDO 1", 0x1A00);
  70. CheckPdoMapping(content, "TPDO 2", 0x1A01);
  71. if (hasMandatoryObjects && hasObjectList && has1000 && has1001 && has1018 &&
  72. objectListHas1000 && objectListHas1001 && objectListHas1018)
  73. {
  74. Console.WriteLine("\n✓✓✓ EDS文件验证通过! 所有必选对象都存在。");
  75. Console.WriteLine("现在可以尝试导入PLC了。");
  76. }
  77. else
  78. {
  79. Console.WriteLine("\n✗✗✗ EDS文件验证失败! 缺少必要的部分或对象。");
  80. }
  81. }
  82. static void CheckPdoMapping(string content, string pdoName, ushort index)
  83. {
  84. string sectionHeader = $"[0x{index:X4}]";
  85. int sectionStart = content.IndexOf(sectionHeader);
  86. if (sectionStart < 0)
  87. {
  88. Console.WriteLine($"{pdoName} (0x{index:X4}): ✗ 未找到");
  89. return;
  90. }
  91. // 找到下一个段落
  92. int nextSection = content.IndexOf("[", sectionStart + sectionHeader.Length);
  93. string section = nextSection > 0
  94. ? content.Substring(sectionStart, nextSection - sectionStart)
  95. : content.Substring(sectionStart);
  96. // 检查映射数量
  97. bool hasSub0 = section.Contains($"[{sectionHeader.Trim('[', ']')}sub0]");
  98. string mappingCount = "0";
  99. if (hasSub0)
  100. {
  101. int sub0Start = section.IndexOf($"[{sectionHeader.Trim('[', ']')}sub0]");
  102. int sub0End = section.IndexOf("[", sub0Start + 1);
  103. string sub0Section = sub0End > 0
  104. ? section.Substring(sub0Start, sub0End - sub0Start)
  105. : section.Substring(sub0Start);
  106. // 提取DefaultValue
  107. int defaultValuePos = sub0Section.IndexOf("DefaultValue=");
  108. if (defaultValuePos >= 0)
  109. {
  110. int valueStart = defaultValuePos + "DefaultValue=".Length;
  111. int valueEnd = sub0Section.IndexOf("\n", valueStart);
  112. mappingCount = valueEnd > 0
  113. ? sub0Section.Substring(valueStart, valueEnd - valueStart).Trim()
  114. : sub0Section.Substring(valueStart).Trim();
  115. }
  116. }
  117. Console.WriteLine($"{pdoName} (0x{index:X4}): ✓ 映射数量 = {mappingCount}");
  118. }
  119. }
  120. }