| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using System;
- using CCDCount.DLL.CanBus;
- namespace CanTest
- {
- /// <summary>
- /// EDS文件生成测试
- /// </summary>
- class EdsGeneratorTest
- {
- static void Main(string[] args)
- {
- Console.WriteLine("=== EDS文件生成测试 ===\n");
-
- // 创建对象字典
- byte nodeId = 5;
- var objectDict = new ObjectDictionary(nodeId);
-
- // 生成EDS文件
- var edsGenerator = new EdsFileGenerator(nodeId);
- string edsPath = "Test_CanOpenSlave.eds";
-
- string edsContent = edsGenerator.Generate(objectDict, edsPath);
-
- Console.WriteLine($"\nEDS文件已生成: {edsPath}");
-
- // 验证EDS文件
- VerifyEdsFile(edsPath);
-
- Console.WriteLine("\n按任意键退出...");
- Console.ReadKey();
- }
-
- static void VerifyEdsFile(string edsPath)
- {
- Console.WriteLine("\n=== 验证EDS文件 ===");
-
- if (!System.IO.File.Exists(edsPath))
- {
- Console.WriteLine($"错误: EDS文件不存在: {edsPath}");
- return;
- }
-
- string content = System.IO.File.ReadAllText(edsPath);
-
- // 检查关键部分
- bool hasMandatoryObjects = content.Contains("[MandatoryObjects]");
- bool hasObjectList = content.Contains("[ObjectList]");
- bool has1000 = content.Contains("[0x1000]");
- bool has1001 = content.Contains("[0x1001]");
- bool has1018 = content.Contains("[0x1018]");
-
- Console.WriteLine($"[MandatoryObjects] 部分: {(hasMandatoryObjects ? "✓ 存在" : "✗ 缺失")}");
- Console.WriteLine($"[ObjectList] 部分: {(hasObjectList ? "✓ 存在" : "✗ 缺失")}");
- Console.WriteLine($"0x1000 (Device Type): {(has1000 ? "✓ 存在" : "✗ 缺失")}");
- Console.WriteLine($"0x1001 (Error Register): {(has1001 ? "✓ 存在" : "✗ 缺失")}");
- Console.WriteLine($"0x1018 (Identity Object): {(has1018 ? "✓ 存在" : "✗ 缺失")}");
-
- // 检查ObjectList中是否包含必选对象
- bool objectListHas1000 = false;
- bool objectListHas1001 = false;
- bool objectListHas1018 = false;
-
- int objectListStart = content.IndexOf("[ObjectList]");
- if (objectListStart >= 0)
- {
- int nextSection = content.IndexOf("[", objectListStart + 12);
- string objectListSection = nextSection > 0
- ? content.Substring(objectListStart, nextSection - objectListStart)
- : content.Substring(objectListStart);
-
- objectListHas1000 = objectListSection.Contains("0x1000");
- objectListHas1001 = objectListSection.Contains("0x1001");
- objectListHas1018 = objectListSection.Contains("0x1018");
- }
-
- Console.WriteLine($"\nObjectList中包含:");
- Console.WriteLine($" 0x1000: {(objectListHas1000 ? "✓" : "✗")}");
- Console.WriteLine($" 0x1001: {(objectListHas1001 ? "✓" : "✗")}");
- Console.WriteLine($" 0x1018: {(objectListHas1018 ? "✓" : "✗")}");
-
- // 检查PDO映射配置
- Console.WriteLine($"\n=== PDO映射验证 ===");
- CheckPdoMapping(content, "RPDO 1", 0x1600);
- CheckPdoMapping(content, "RPDO 2", 0x1601);
- CheckPdoMapping(content, "TPDO 1", 0x1A00);
- CheckPdoMapping(content, "TPDO 2", 0x1A01);
-
- if (hasMandatoryObjects && hasObjectList && has1000 && has1001 && has1018 &&
- objectListHas1000 && objectListHas1001 && objectListHas1018)
- {
- Console.WriteLine("\n✓✓✓ EDS文件验证通过! 所有必选对象都存在。");
- Console.WriteLine("现在可以尝试导入PLC了。");
- }
- else
- {
- Console.WriteLine("\n✗✗✗ EDS文件验证失败! 缺少必要的部分或对象。");
- }
- }
-
- static void CheckPdoMapping(string content, string pdoName, ushort index)
- {
- string sectionHeader = $"[0x{index:X4}]";
- int sectionStart = content.IndexOf(sectionHeader);
-
- if (sectionStart < 0)
- {
- Console.WriteLine($"{pdoName} (0x{index:X4}): ✗ 未找到");
- return;
- }
-
- // 找到下一个段落
- int nextSection = content.IndexOf("[", sectionStart + sectionHeader.Length);
- string section = nextSection > 0
- ? content.Substring(sectionStart, nextSection - sectionStart)
- : content.Substring(sectionStart);
-
- // 检查映射数量
- bool hasSub0 = section.Contains($"[{sectionHeader.Trim('[', ']')}sub0]");
- string mappingCount = "0";
-
- if (hasSub0)
- {
- int sub0Start = section.IndexOf($"[{sectionHeader.Trim('[', ']')}sub0]");
- int sub0End = section.IndexOf("[", sub0Start + 1);
- string sub0Section = sub0End > 0
- ? section.Substring(sub0Start, sub0End - sub0Start)
- : section.Substring(sub0Start);
-
- // 提取DefaultValue
- int defaultValuePos = sub0Section.IndexOf("DefaultValue=");
- if (defaultValuePos >= 0)
- {
- int valueStart = defaultValuePos + "DefaultValue=".Length;
- int valueEnd = sub0Section.IndexOf("\n", valueStart);
- mappingCount = valueEnd > 0
- ? sub0Section.Substring(valueStart, valueEnd - valueStart).Trim()
- : sub0Section.Substring(valueStart).Trim();
- }
- }
-
- Console.WriteLine($"{pdoName} (0x{index:X4}): ✓ 映射数量 = {mappingCount}");
- }
- }
- }
|