EdsFileGenerator.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace CCDCount.DLL.CanBus
  6. {
  7. /// <summary>
  8. /// EDS文件生成器
  9. /// 用于生成PLC可识别的CANopen从站配置文件
  10. /// </summary>
  11. public class EdsFileGenerator
  12. {
  13. private byte m_nodeId;
  14. public EdsFileGenerator(byte nodeId)
  15. {
  16. m_nodeId = nodeId;
  17. }
  18. /// <summary>
  19. /// 生成EDS文件
  20. /// </summary>
  21. public string Generate(ObjectDictionary objectDict, string filePath = null)
  22. {
  23. StringBuilder sb = new StringBuilder();
  24. // 文件头
  25. sb.AppendLine("[FileInfo]");
  26. sb.AppendLine("FileName=CanOpenSlave.eds");
  27. sb.AppendLine("FileVersion=1");
  28. sb.AppendLine("FileRevision=1");
  29. sb.AppendLine($"CreationDate={DateTime.Now:MM-dd-yyyy}");
  30. sb.AppendLine($"CreationTime={DateTime.Now:HH:mm:ss}");
  31. sb.AppendLine("CreatedBy=CCDCount CANopen Slave Generator");
  32. sb.AppendLine("");
  33. // 设备信息
  34. sb.AppendLine("[DeviceInfo]");
  35. sb.AppendLine("VendorName=Custom");
  36. sb.AppendLine("ProductName=CCDCount CANopen Slave Device");
  37. sb.AppendLine("OrderCode=COS-001");
  38. sb.AppendLine("VendorNumber=0x00000133");
  39. sb.AppendLine("ProductNumber=0x00000001");
  40. sb.AppendLine("RevisionNo=0x00010001");
  41. sb.AppendLine($"SerialNumber=0x{m_nodeId:X8}");
  42. sb.AppendLine("BaudRate_10=1");
  43. sb.AppendLine("BaudRate_20=1");
  44. sb.AppendLine("BaudRate_50=1");
  45. sb.AppendLine("BaudRate_125=1");
  46. sb.AppendLine("BaudRate_250=1");
  47. sb.AppendLine("BaudRate_500=1");
  48. sb.AppendLine("BaudRate_800=1");
  49. sb.AppendLine("BaudRate_1000=1");
  50. sb.AppendLine("SimpleBootUpMaster=0");
  51. sb.AppendLine("SimpleBootUpSlave=1");
  52. sb.AppendLine("GroupMessaging=0");
  53. sb.AppendLine("NrOfRXPDO=4");
  54. sb.AppendLine("NrOfTXPDO=4");
  55. sb.AppendLine("LSS_Supported=0");
  56. sb.AppendLine("");
  57. // dummy usage
  58. sb.AppendLine("[DummyUsage]");
  59. sb.AppendLine("Dummy0001=0");
  60. sb.AppendLine("Dummy0002=0");
  61. sb.AppendLine("Dummy0003=0");
  62. sb.AppendLine("Dummy0004=0");
  63. sb.AppendLine("Dummy0005=1");
  64. sb.AppendLine("Dummy0006=0");
  65. sb.AppendLine("Dummy0007=0");
  66. sb.AppendLine("");
  67. // 评论
  68. sb.AppendLine("[Comments]");
  69. sb.AppendLine("Lines=2");
  70. sb.AppendLine("Line1=CANopen Slave Device");
  71. sb.AppendLine($"Line2=Node ID: {m_nodeId}");
  72. sb.AppendLine("");
  73. // 强制对象
  74. sb.AppendLine("[MandatoryObjects]");
  75. sb.AppendLine("SupportedObjects=3");
  76. sb.AppendLine("1=0x1000");
  77. sb.AppendLine("2=0x1001");
  78. sb.AppendLine("3=0x1018");
  79. sb.AppendLine("");
  80. // 制造商特定对象
  81. sb.AppendLine("[ManufacturerObjects]");
  82. sb.AppendLine("SupportedObjects=0");
  83. sb.AppendLine("");
  84. // 可选对象列表(不包含必选对象)
  85. var allObjects = objectDict.GetAllObjects();
  86. List<ushort> optionalIndexes = new List<ushort>();
  87. foreach (var index in allObjects.Keys)
  88. {
  89. // 排除必选对象(0x1000、0x1001、0x1018)
  90. if (index == 0x1000 || index == 0x1001 || index == 0x1018)
  91. continue;
  92. if ((index >= 0x1000 && index <= 0x1FFF) ||
  93. (index >= 0x2000 && index <= 0x5FFF) ||
  94. (index >= 0x1400 && index <= 0x15FF) ||
  95. (index >= 0x1600 && index <= 0x17FF) ||
  96. (index >= 0x1800 && index <= 0x19FF) ||
  97. (index >= 0x1A00 && index <= 0x1BFF))
  98. {
  99. if (!optionalIndexes.Contains(index))
  100. optionalIndexes.Add(index);
  101. }
  102. }
  103. // 排序可选对象
  104. optionalIndexes.Sort();
  105. sb.AppendLine("[OptionalObjects]");
  106. sb.AppendLine($"SupportedObjects={optionalIndexes.Count}");
  107. for (int i = 0; i < optionalIndexes.Count; i++)
  108. {
  109. sb.AppendLine($"{i + 1}=0x{optionalIndexes[i]:X4}");
  110. }
  111. sb.AppendLine("");
  112. // 添加ObjectList部分(CANopen标准要求的完整对象列表)
  113. // ObjectList应该包含所有对象: 必选 + 可选 + 制造商特定
  114. List<ushort> allObjectIndexes = new List<ushort>();
  115. // 首先添加必选对象(InoProShop要求必选对象在前)
  116. allObjectIndexes.Add(0x1000);
  117. allObjectIndexes.Add(0x1001);
  118. allObjectIndexes.Add(0x1018);
  119. // 添加其他对象(已排除必选对象)
  120. allObjectIndexes.AddRange(optionalIndexes);
  121. sb.AppendLine("[ObjectList]");
  122. sb.AppendLine($"SupportedObjects={allObjectIndexes.Count}");
  123. for (int i = 0; i < allObjectIndexes.Count; i++)
  124. {
  125. sb.AppendLine($"{i + 1}=0x{allObjectIndexes[i]:X4}");
  126. }
  127. sb.AppendLine("");
  128. // 生成每个对象的详细信息
  129. Console.WriteLine($"开始生成对象详细信息,共有 {allObjects.Count} 个对象");
  130. foreach (var kvp in allObjects)
  131. {
  132. ushort index = kvp.Key;
  133. var subObjects = kvp.Value;
  134. // 只处理标准范围内的对象
  135. if (!((index >= 0x1000 && index <= 0x1FFF) ||
  136. (index >= 0x2000 && index <= 0x5FFF) ||
  137. (index >= 0x1400 && index <= 0x15FF) ||
  138. (index >= 0x1600 && index <= 0x17FF) ||
  139. (index >= 0x1800 && index <= 0x19FF) ||
  140. (index >= 0x1A00 && index <= 0x1BFF)))
  141. continue;
  142. Console.WriteLine($"生成对象 0x{index:X4}, 子对象数量: {subObjects.Count}");
  143. GenerateObjectDetails(sb, index, subObjects, objectDict);
  144. }
  145. // 确保必选对象一定存在(0x1000, 0x1001, 0x1018)
  146. Console.WriteLine("检查必选对象...");
  147. EnsureMandatoryObjectsExist(sb, allObjects, objectDict);
  148. string edsContent = sb.ToString();
  149. // 如果指定了文件路径,则保存到文件
  150. if (!string.IsNullOrEmpty(filePath))
  151. {
  152. File.WriteAllText(filePath, edsContent, Encoding.ASCII);
  153. Console.WriteLine($"EDS文件已保存到: {filePath}");
  154. }
  155. return edsContent;
  156. }
  157. private void GenerateObjectDetails(StringBuilder sb, ushort index, Dictionary<byte, object> subObjects, ObjectDictionary objectDict)
  158. {
  159. sb.AppendLine($"[0x{index:X4}]");
  160. sb.AppendLine($"ParameterName={GetParameterName(index)}");
  161. // 判断是否为复合类型(RECORD)
  162. // PDO通信参数、PDO映射参数、身份对象等都是复合类型
  163. bool isRecordType = (index >= 0x1400 && index <= 0x15FF) || // RPDO通信参数
  164. (index >= 0x1800 && index <= 0x19FF) || // TPDO通信参数
  165. (index >= 0x1600 && index <= 0x17FF) || // RPDO映射参数 - 修正为RECORD类型
  166. (index >= 0x1A00 && index <= 0x1BFF) || // TPDO映射参数 - 修正为RECORD类型
  167. (index == 0x1018) || // 身份对象
  168. (subObjects.Count > 1); // 其他有多个子对象的
  169. byte objectTypeCode = isRecordType ? (byte)0x09 : (byte)0x07;
  170. sb.AppendLine($"ObjectType=0x{objectTypeCode:X2}");
  171. if (isRecordType)
  172. {
  173. // 复合类型(有子索引)
  174. byte maxSubIndex = 0;
  175. foreach (var subIdx in subObjects.Keys)
  176. {
  177. if (subIdx > maxSubIndex)
  178. maxSubIndex = subIdx;
  179. }
  180. sb.AppendLine("SubNumber=" + (maxSubIndex + 1));
  181. sb.AppendLine("");
  182. // 生成每个子对象
  183. for (byte subIdx = 0; subIdx <= maxSubIndex; subIdx++)
  184. {
  185. if (subObjects.ContainsKey(subIdx))
  186. {
  187. sb.AppendLine($"[0x{index:X4}sub{subIdx}]");
  188. sb.AppendLine($"ParameterName={GetSubParameterName(index, subIdx)}");
  189. sb.AppendLine($"ObjectType=0x07");
  190. var value = subObjects[subIdx];
  191. DataTypeEnum dataType = GetDataType(value);
  192. sb.AppendLine($"DataType=0x{(int)dataType:X4}");
  193. sb.AppendLine($"AccessType={GetAccessType(index, subIdx)}");
  194. sb.AppendLine($"DefaultValue={FormatValue(value, dataType)}");
  195. sb.AppendLine("PDOMapping=0");
  196. sb.AppendLine("LowLimit=0");
  197. sb.AppendLine("HighLimit=0");
  198. sb.AppendLine("");
  199. }
  200. }
  201. }
  202. else
  203. {
  204. // 简单类型
  205. var value = subObjects.ContainsKey(0) ? subObjects[0] : null;
  206. if (value != null)
  207. {
  208. DataTypeEnum dataType = GetDataType(value);
  209. sb.AppendLine($"DataType=0x{(int)dataType:X4}");
  210. sb.AppendLine($"AccessType={GetAccessType(index, 0)}");
  211. sb.AppendLine($"DefaultValue={FormatValue(value, dataType)}");
  212. sb.AppendLine("PDOMapping=0");
  213. sb.AppendLine("");
  214. }
  215. }
  216. }
  217. private string GetParameterName(ushort index)
  218. {
  219. switch (index)
  220. {
  221. case 0x1000: return "Device Type";
  222. case 0x1001: return "Error Register";
  223. case 0x1005: return "COB-ID SYNC";
  224. case 0x1006: return "Sync Window Time";
  225. case 0x1007: return "Sync Overflow Cycle";
  226. case 0x1008: return "Manufacturer Device Name";
  227. case 0x1009: return "Manufacturer Hardware Version";
  228. case 0x100A: return "Manufacturer Software Version";
  229. case 0x1017: return "Producer Heartbeat Time";
  230. case 0x1018: return "Identity Object";
  231. default:
  232. if (index >= 0x1400 && index <= 0x1403)
  233. return $"RPDO {index - 0x1400 + 1} Communication Parameter";
  234. if (index >= 0x1600 && index <= 0x1603)
  235. return $"RPDO {index - 0x1600 + 1} Mapping Parameter";
  236. if (index >= 0x1800 && index <= 0x1803)
  237. return $"TPDO {index - 0x1800 + 1} Communication Parameter";
  238. if (index >= 0x1A00 && index <= 0x1A03)
  239. return $"TPDO {index - 0x1A00 + 1} Mapping Parameter";
  240. if (index >= 0x6000 && index <= 0x600F)
  241. return "Digital Input";
  242. if (index >= 0x6200 && index <= 0x620F)
  243. return "Digital Output";
  244. if (index >= 0x6400 && index <= 0x640F)
  245. return "Analog Input";
  246. if (index >= 0x6410 && index <= 0x641F)
  247. return "Analog Output";
  248. return $"Unknown_0x{index:X4}";
  249. }
  250. }
  251. private string GetSubParameterName(ushort index, byte subIndex)
  252. {
  253. if (subIndex == 0)
  254. return "Highest sub-index supported";
  255. switch (index)
  256. {
  257. case 0x1018:
  258. switch (subIndex)
  259. {
  260. case 1: return "Vendor ID";
  261. case 2: return "Product Code";
  262. case 3: return "Revision Number";
  263. case 4: return "Serial Number";
  264. }
  265. break;
  266. case 0x1400: case 0x1401: case 0x1402: case 0x1403:
  267. switch (subIndex)
  268. {
  269. case 1: return "COB-ID used by RPDO";
  270. case 2: return "Transmission Type";
  271. }
  272. break;
  273. case 0x1800: case 0x1801: case 0x1802: case 0x1803:
  274. switch (subIndex)
  275. {
  276. case 1: return "COB-ID used by TPDO";
  277. case 2: return "Transmission Type";
  278. case 3: return "Inhibit Time";
  279. case 5: return "Event Timer";
  280. case 6: return "SYNC start value";
  281. }
  282. break;
  283. case 0x1600: case 0x1601: case 0x1602: case 0x1603:
  284. case 0x1A00: case 0x1A01: case 0x1A02: case 0x1A03:
  285. return $"PDO mapping entry {subIndex}";
  286. case 0x6000: case 0x6200:
  287. return $"Bit {subIndex}";
  288. case 0x6400: case 0x6410:
  289. return $"Channel {subIndex}";
  290. }
  291. return $"SubIndex_{subIndex}";
  292. }
  293. private byte GetObjectTypeCode(ushort index, ObjectDictionary objectDict)
  294. {
  295. var allObjects = objectDict.GetAllObjects();
  296. if (allObjects.ContainsKey(index))
  297. {
  298. return (byte)(allObjects[index].Count > 1 ? 0x09 : 0x07);
  299. }
  300. return 0x07;
  301. }
  302. private DataTypeEnum GetDataType(object value)
  303. {
  304. if (value is byte) return DataTypeEnum.Unsigned8;
  305. if (value is sbyte) return DataTypeEnum.Integer8;
  306. if (value is ushort) return DataTypeEnum.Unsigned16;
  307. if (value is short) return DataTypeEnum.Integer16;
  308. if (value is uint) return DataTypeEnum.Unsigned32;
  309. if (value is int) return DataTypeEnum.Integer32;
  310. if (value is string) return DataTypeEnum.VisibleString;
  311. return DataTypeEnum.Unsigned32;
  312. }
  313. private string GetAccessType(ushort index, byte subIndex)
  314. {
  315. // 心跳时间可读写
  316. if (index == 0x1017 && subIndex == 0)
  317. return "rw";
  318. // PDO通信参数可读写
  319. if ((index >= 0x1400 && index <= 0x1403) ||
  320. (index >= 0x1800 && index <= 0x1803))
  321. return "rw";
  322. // PDO映射参数可读写
  323. if ((index >= 0x1600 && index <= 0x1603) ||
  324. (index >= 0x1A00 && index <= 0x1A03))
  325. return "rw";
  326. // 错误寄存器只读
  327. if (index == 0x1001)
  328. return "ro";
  329. // 设备信息只读
  330. if (index >= 0x1008 && index <= 0x100A)
  331. return "ro";
  332. if (index == 0x1018)
  333. return "ro";
  334. // 默认读写
  335. return "rw";
  336. }
  337. private string FormatValue(object value, DataTypeEnum dataType)
  338. {
  339. if (value == null)
  340. return "0";
  341. switch (dataType)
  342. {
  343. case DataTypeEnum.Unsigned8:
  344. return $"{Convert.ToByte(value)}";
  345. case DataTypeEnum.Unsigned16:
  346. return $"{Convert.ToUInt16(value)}";
  347. case DataTypeEnum.Unsigned32:
  348. return $"0x{Convert.ToUInt32(value):X8}";
  349. case DataTypeEnum.Integer8:
  350. return $"{Convert.ToSByte(value)}";
  351. case DataTypeEnum.Integer16:
  352. return $"{Convert.ToInt16(value)}";
  353. case DataTypeEnum.Integer32:
  354. return $"{Convert.ToInt32(value)}";
  355. case DataTypeEnum.VisibleString:
  356. return $"\"{value}\"";
  357. default:
  358. return value.ToString();
  359. }
  360. }
  361. private void EnsureMandatoryObjectsExist(StringBuilder sb, Dictionary<ushort, Dictionary<byte, object>> allObjects, ObjectDictionary objectDict)
  362. {
  363. // 必选对象列表: 0x1000, 0x1001, 0x1018
  364. ushort[] mandatoryIndexes = { 0x1000, 0x1001, 0x1018 };
  365. foreach (ushort index in mandatoryIndexes)
  366. {
  367. // 如果该对象还没有被生成,则强制生成
  368. if (!allObjects.ContainsKey(index))
  369. {
  370. // 创建一个空的子对象字典,从objectDict中读取值
  371. var subObjects = new Dictionary<byte, object>();
  372. // 尝试从objectDict中获取该对象的值
  373. try
  374. {
  375. if (index == 0x1018)
  376. {
  377. // 0x1018是复合类型,需要所有子索引
  378. for (byte subIdx = 0; subIdx <= 4; subIdx++)
  379. {
  380. var value = objectDict.Read(index, subIdx);
  381. if (value != null)
  382. subObjects[subIdx] = value;
  383. }
  384. }
  385. else
  386. {
  387. // 0x1000和0x1001是简单类型
  388. var value = objectDict.Read(index, 0);
  389. if (value != null)
  390. subObjects[0] = value;
  391. }
  392. }
  393. catch
  394. {
  395. // 如果读取失败,使用默认值
  396. if (index == 0x1000)
  397. subObjects[0] = (uint)0x00000190; // 设备类型
  398. else if (index == 0x1001)
  399. subObjects[0] = (byte)0x00; // 错误寄存器
  400. else if (index == 0x1018)
  401. {
  402. subObjects[0] = (byte)4;
  403. subObjects[1] = (uint)0x00000133; // Vendor ID
  404. subObjects[2] = (uint)0x00000001; // Product Code
  405. subObjects[3] = (uint)0x00010001; // Revision Number
  406. subObjects[4] = (uint)m_nodeId; // Serial Number
  407. }
  408. }
  409. // 生成该对象的详细信息
  410. if (subObjects.Count > 0)
  411. {
  412. GenerateObjectDetails(sb, index, subObjects, objectDict);
  413. }
  414. }
  415. }
  416. }
  417. }
  418. /// <summary>
  419. /// 数据类型枚举
  420. /// </summary>
  421. public enum DataTypeEnum
  422. {
  423. Boolean = 0x0001,
  424. Integer8 = 0x0002,
  425. Integer16 = 0x0003,
  426. Integer32 = 0x0004,
  427. Unsigned8 = 0x0005,
  428. Unsigned16 = 0x0006,
  429. Unsigned32 = 0x0007,
  430. Real32 = 0x0008,
  431. VisibleString = 0x0009,
  432. OctetString = 0x000A,
  433. UnicodeString = 0x000B,
  434. TimeOfDay = 0x000C,
  435. TimeDifference = 0x000D
  436. }
  437. }