forked from robincornelius/libedssharp
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathCanOpenXDD.cs
More file actions
1331 lines (987 loc) · 59.8 KB
/
CanOpenXDD.cs
File metadata and controls
1331 lines (987 loc) · 59.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of libEDSsharp.
libEDSsharp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libEDSsharp is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libEDSsharp. If not, see <http://www.gnu.org/licenses/>.
Copyright(c) 2016 - 2019 Robin Cornelius <robin.cornelius@gmail.com>
based heavily on the files CO_OD.h and CO_OD.c from CANopenNode which are
Copyright(c) 2010 - 2016 Janez Paternoster
*/
using System;
using System.Xml.Serialization;
using System.IO;
using CanOpenXSD_1_0;
using System.Text.RegularExpressions; //and nope this is not anywhere near the xml parsing
using System.Collections.Generic;
namespace libEDSsharp
{
public class CanOpenXDD : IFileExporter
{
public ISO15745ProfileContainer dev;
/// <summary>
/// Fetches all the different fileexporter types the class supports
/// </summary>
/// <returns>List of the different exporters the class supports</returns>
public ExporterDescriptor[] GetExporters()
{
return new ExporterDescriptor[] {
new ExporterDescriptor("CanOpen XDD v1.0", new string[] { ".xdd" }, 0, delegate (string filepath, List<EDSsharp> edss)
{
var e = new CanOpenXDD();
e.writeXML(filepath,edss[0]);
}),
new ExporterDescriptor("CanOpen Network v1.0", new string[] { ".nxdd" }, ExporterDescriptor.ExporterFlags.MultipleNodeSupport, delegate (string filepath, List<EDSsharp> edss)
{
var e = new CanOpenXDD();
e.writeMultiXML(filepath,edss);
})
};
}
public EDSsharp readXML(string file)
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(ISO15745ProfileContainer));
StreamReader reader = new StreamReader(file);
dev = (ISO15745ProfileContainer)serializer.Deserialize(reader);
reader.Close();
}
catch (Exception)
{
return null;
}
return convert(dev);
}
public List<EDSsharp> readMultiXML(string file )
{
List<EDSsharp> edss = new List<EDSsharp>();
try
{
XmlSerializer serializer = new XmlSerializer(typeof(OpenEDSProject));
StreamReader reader = new StreamReader(file);
OpenEDSProject oep = (OpenEDSProject)serializer.Deserialize(reader);
foreach(ISO15745ProfileContainer cont in oep.ISO15745ProfileContainer)
{
edss.Add(convert(cont));
}
reader.Close();
return edss;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return null;
}
}
public void writeMultiXML(string file, List<EDSsharp> edss)
{
List<ISO15745ProfileContainer> devs = new List<ISO15745ProfileContainer>();
foreach (EDSsharp eds in edss)
{
ISO15745ProfileContainer dev = convert(eds);
devs.Add(dev);
}
OpenEDSProject oep = new OpenEDSProject();
oep.Version = "1.0";
oep.ISO15745ProfileContainer = devs;
XmlSerializer serializer = new XmlSerializer(typeof(OpenEDSProject));
StreamWriter writer = new StreamWriter(file);
serializer.Serialize(writer, oep);
writer.Close();
}
public void writeXML(string file, EDSsharp eds)
{
dev = convert(eds);
XmlSerializer serializer = new XmlSerializer(typeof(ISO15745ProfileContainer));
StreamWriter writer = new StreamWriter(file);
serializer.Serialize(writer, dev);
writer.Close();
}
public void fillparamater(parameter p, ODentry od)
{
if (od.parent == null)
{
p.uniqueID = string.Format("UID_PARAM_{0:x4}", od.Index);
}
else
{
p.uniqueID = string.Format("UID_PARAM_{0:x4}{1:x2}", od.parent.Index, od.Subindex);
}
switch (od.accesstype)
{
case EDSsharp.AccessType.rw:
p.access = parameterTemplateAccess.readWrite;
break;
case EDSsharp.AccessType.ro:
p.access = parameterTemplateAccess.read;
break;
case EDSsharp.AccessType.wo:
p.access = parameterTemplateAccess.write;
break;
case EDSsharp.AccessType.rwr:
p.access = parameterTemplateAccess.readWriteInput;
break;
case EDSsharp.AccessType.rww:
p.access = parameterTemplateAccess.readWriteOutput;
break;
case EDSsharp.AccessType.@const:
p.access = parameterTemplateAccess.@const;
break;
//fixme no access not handled
default:
p.access = parameterTemplateAccess.noAccess;
break;
}
p.Items = new object[2];
vendorTextLabel lab = new vendorTextLabel();
lab.lang = "en";
lab.Value = od.parameter_name;
p.Items[0] = lab;
//FIXME we are currently writing the denotation value to both the object and the parameterList section
//i'm not sure why two exist
denotation denot = new denotation();
vendorTextLabel lab2 = new vendorTextLabel();
lab2.lang = "en";
lab2.Value = od.denotation;
denot.Items = new object[1];
denot.Items[0] = lab2;
p.denotation = denot;
vendorTextDescription desc = new vendorTextDescription();
desc.lang = "en"; //fixme we could and should do better than just English
desc.Value = od.Description;
p.Items[1] = desc;
p.defaultValue = new defaultValue();
p.defaultValue.value = od.defaultvalue;
}
public ISO15745ProfileContainer convert(EDSsharp eds)
{
dev = new ISO15745ProfileContainer();
dev.ISO15745Profile = new ISO15745Profile[2];
//Profile 0 ProfileBody_Device_CANopen
dev.ISO15745Profile[0] = new ISO15745Profile();
dev.ISO15745Profile[0].ProfileHeader = new ProfileHeader_DataType();
dev.ISO15745Profile[0].ProfileHeader.ProfileIdentification = "CAN device profile";
dev.ISO15745Profile[0].ProfileHeader.ProfileRevision = "1";
dev.ISO15745Profile[0].ProfileHeader.ProfileClassID = ProfileClassID_DataType.Device;
dev.ISO15745Profile[0].ProfileHeader.ProfileName = "";
dev.ISO15745Profile[0].ProfileHeader.ProfileSource = "";
dev.ISO15745Profile[0].ProfileHeader.ISO15745Reference = new ISO15745Reference_DataType();
dev.ISO15745Profile[0].ProfileHeader.ISO15745Reference.ISO15745Part = "1";
dev.ISO15745Profile[0].ProfileHeader.ISO15745Reference.ISO15745Edition = "1";
dev.ISO15745Profile[0].ProfileHeader.ISO15745Reference.ProfileTechnology = "CANopen";
dev.ISO15745Profile[0].ProfileBody = new ProfileBody_Device_CANopen();
ProfileBody_Device_CANopen device = (ProfileBody_Device_CANopen)dev.ISO15745Profile[0].ProfileBody;
device.DeviceIdentity = new DeviceIdentity();
device.DeviceIdentity.vendorName = new vendorName();
device.DeviceIdentity.vendorName.Value = eds.di.VendorName;
device.DeviceIdentity.vendorName.readOnly = true;
device.DeviceIdentity.vendorID = new vendorID();
device.DeviceIdentity.vendorID.Value = eds.di.VendorNumber.ToHexString();
device.DeviceIdentity.vendorID.readOnly = true;
device.DeviceIdentity.deviceFamily = new deviceFamily();
device.DeviceIdentity.productFamily = new productFamily();
//device.DeviceIdentity.orderNumber =
device.fileCreationDate = eds.fi.CreationDateTime;
device.fileCreationTime = eds.fi.CreationDateTime;
device.fileCreationTimeSpecified = true;
device.fileModificationDate = eds.fi.ModificationDateTime;
device.fileModificationTime = eds.fi.ModificationDateTime;
device.fileModificationDateSpecified = true;
device.fileModificationTimeSpecified = true;
device.fileCreator = eds.fi.CreatedBy;
device.fileModifiedBy = eds.fi.ModifiedBy;
device.supportedLanguages = "en";
device.fileVersion = eds.fi.FileVersion.ToString();
device.fileName = Path.GetFileName(eds.projectFilename);
//device.DeviceIdentity.vendorText
//device.DeviceIdentity.deviceFamily
//device.DeviceIdentity.productFamily;
device.DeviceIdentity.productName = new productName();
device.DeviceIdentity.productName.Value = eds.di.ProductName;
device.DeviceIdentity.productName.readOnly = true;
device.DeviceIdentity.productID = new productID();
device.DeviceIdentity.productID.Value = eds.di.ProductNumber.ToHexString();
device.DeviceIdentity.productID.readOnly = true;
device.DeviceIdentity.productText = new productText();
device.DeviceIdentity.productText.Items = new object[1];
vendorTextDescription des = new vendorTextDescription();
des.lang = "en";
des.Value = String.Format("FileDescription={0}|EdsVersion={1}|FileRevision={2}|RevisionNum={3}",eds.fi.Description,eds.fi.EDSVersion,eds.fi.FileVersion,eds.fi.FileRevision);
device.DeviceIdentity.productText.Items[0] = des;
device.DeviceIdentity.productText.readOnly = true;
//device.DeviceIdentity.orderNumber
device.DeviceIdentity.version = new version[3];
device.DeviceIdentity.version[0] = new version();
device.DeviceIdentity.version[0].readOnly = true;
device.DeviceIdentity.version[0].versionType = versionVersionType.SW;
device.DeviceIdentity.version[1] = new version();
device.DeviceIdentity.version[1].readOnly = true;
device.DeviceIdentity.version[1].versionType = versionVersionType.FW;
device.DeviceIdentity.version[2] = new version();
device.DeviceIdentity.version[2].readOnly = true;
device.DeviceIdentity.version[2].versionType = versionVersionType.HW;
//device.DeviceIdentity.specificationRevision.value =
device.DeviceIdentity.specificationRevision = new specificationRevision();
device.DeviceIdentity.specificationRevision.readOnly = true;
device.DeviceIdentity.instanceName = new instanceName();
//device.DeviceIdentity.instanceName.Value = ;
device.DeviceIdentity.instanceName.readOnly = true;
device.DeviceManager = new DeviceManager();
device.DeviceManager.indicatorList = new indicatorList();
device.DeviceManager.indicatorList.LEDList = new LEDList();
device.DeviceManager.indicatorList.LEDList.LED = new LED[1]; //fixme
device.DeviceManager.indicatorList.LEDList.LED[0] = new LED();
device.DeviceManager.indicatorList.LEDList.LED[0].LEDcolors = LEDLEDcolors.monocolor;
device.DeviceManager.indicatorList.LEDList.LED[0].LEDtype = LEDLEDtype.device;
device.DeviceManager.indicatorList.LEDList.LED[0].Items = new object[1];
// LEDstate ls = new LEDstate();
// ls.uniqueID = "LED_State_1";
// ls.state = LEDstateState.off;
// ls.LEDcolor = LEDstateLEDcolor.green;
// device.DeviceManager.indicatorList.LEDList.LED[0].Items[0] = ls;
device.DeviceFunction = new DeviceFunction[1];
//fix me fll this in
device.ApplicationProcess = new ApplicationProcess[1];
device.ApplicationProcess[0] = new ApplicationProcess();
device.ApplicationProcess[0].parameterList = new parameter[eds.GetNoEnabledObjects(true)];
int ordinal = 0;
foreach (ODentry od in eds.ods.Values)
{
if (od.prop.CO_disabled)
continue;
parameter p = new parameter();
fillparamater(p, od);
device.ApplicationProcess[0].parameterList[ordinal] = p;
ordinal++;
foreach (ODentry sub in od.subobjects.Values)
{
p = new parameter();
fillparamater(p, sub);
device.ApplicationProcess[0].parameterList[ordinal] = p;
ordinal++;
}
}
//Profile 1 ProfileClassID_DataType.CommunicationNetwork
dev.ISO15745Profile[1] = new ISO15745Profile();
dev.ISO15745Profile[1].ProfileHeader = new ProfileHeader_DataType();
dev.ISO15745Profile[1].ProfileHeader.ProfileIdentification = "CAN comm net profile";
dev.ISO15745Profile[1].ProfileHeader.ProfileRevision = "1";
dev.ISO15745Profile[1].ProfileHeader.ProfileClassID = ProfileClassID_DataType.CommunicationNetwork;
dev.ISO15745Profile[1].ProfileHeader.ProfileName = "";
dev.ISO15745Profile[1].ProfileHeader.ProfileSource = "";
dev.ISO15745Profile[1].ProfileHeader.ISO15745Reference = new ISO15745Reference_DataType();
dev.ISO15745Profile[1].ProfileHeader.ISO15745Reference.ISO15745Part = "1";
dev.ISO15745Profile[1].ProfileHeader.ISO15745Reference.ISO15745Edition = "1";
dev.ISO15745Profile[1].ProfileHeader.ISO15745Reference.ProfileTechnology = "CANopen";
dev.ISO15745Profile[1].ProfileBody = new ProfileBody_CommunicationNetwork_CANopen();
ProfileBody_CommunicationNetwork_CANopen comnet = (ProfileBody_CommunicationNetwork_CANopen)dev.ISO15745Profile[1].ProfileBody;
comnet.Items = new object[3];
comnet.fileName = Path.GetFileName(eds.projectFilename);
comnet.fileCreator = eds.fi.CreatedBy; //etc
comnet.fileCreationDate = eds.fi.CreationDateTime;
comnet.fileCreationTime = eds.fi.CreationDateTime;
comnet.fileCreationTimeSpecified = true;
comnet.fileModificationDate = eds.fi.ModificationDateTime;
comnet.fileModificationTime = eds.fi.ModificationDateTime;
comnet.fileModificationDateSpecified = true;
comnet.fileVersion = eds.fi.FileVersion.ToString();
comnet.supportedLanguages = "en";
comnet.Items[0] = new ProfileBody_CommunicationNetwork_CANopenApplicationLayers();
ProfileBody_CommunicationNetwork_CANopenApplicationLayers AppLayer = (ProfileBody_CommunicationNetwork_CANopenApplicationLayers)comnet.Items[0];
comnet.Items[1] = new ProfileBody_CommunicationNetwork_CANopenTransportLayers();
ProfileBody_CommunicationNetwork_CANopenTransportLayers TransportLayer = (ProfileBody_CommunicationNetwork_CANopenTransportLayers)comnet.Items[1];
comnet.Items[2] = new ProfileBody_CommunicationNetwork_CANopenNetworkManagement();
ProfileBody_CommunicationNetwork_CANopenNetworkManagement NetworkManagement = (ProfileBody_CommunicationNetwork_CANopenNetworkManagement)comnet.Items[2];
AppLayer.CANopenObjectList = new CANopenObjectList();
AppLayer.CANopenObjectList.CANopenObject = new CANopenObjectListCANopenObject[eds.GetNoEnabledObjects()];
int count = 0;
foreach (KeyValuePair<UInt16,ODentry> kvp in eds.ods)
{
ODentry od = kvp.Value;
UInt16 subindex = kvp.Key;
if (od.prop.CO_disabled)
continue;
AppLayer.CANopenObjectList.CANopenObject[count] = new CANopenObjectListCANopenObject();
byte[] bytes = BitConverter.GetBytes((UInt16)od.Index);
Array.Reverse(bytes);
AppLayer.CANopenObjectList.CANopenObject[count].index = bytes;
AppLayer.CANopenObjectList.CANopenObject[count].name = od.parameter_name;
AppLayer.CANopenObjectList.CANopenObject[count].objectType = (byte)od.objecttype;
bytes = BitConverter.GetBytes((UInt16)od.datatype);
Array.Reverse(bytes);
// hack - special handling for rrw / rww access type
// https://github.com/robincornelius/libedssharp/issues/128
EDSsharp.AccessType accesstype = od.accesstype;
PDOMappingType PDOtype = od.PDOtype;
if (accesstype == EDSsharp.AccessType.rww) {
accesstype = EDSsharp.AccessType.rw;
// when optional, set it to the corresponding type
if (PDOtype == PDOMappingType.optional) {
PDOtype = PDOMappingType.RPDO;
}
}
if (accesstype == EDSsharp.AccessType.rwr) {
accesstype = EDSsharp.AccessType.rw;
// when optional, set it to the corresponding type
if (PDOtype == PDOMappingType.optional) {
PDOtype = PDOMappingType.TPDO;
}
}
if (od.objecttype != ObjectType.ARRAY && od.objecttype != ObjectType.RECORD)
{
//#209 don't set data type for array or rec objects, the subobjects hold
//the data type
AppLayer.CANopenObjectList.CANopenObject[count].dataType = bytes;
AppLayer.CANopenObjectList.CANopenObject[count].accessType = (CANopenObjectListCANopenObjectAccessType)Enum.Parse(typeof(CANopenObjectListCANopenObjectAccessType), accesstype.ToString());
AppLayer.CANopenObjectList.CANopenObject[count].accessTypeSpecified = true;
}
else
{
AppLayer.CANopenObjectList.CANopenObject[count].accessTypeSpecified = false;
}
AppLayer.CANopenObjectList.CANopenObject[count].PDOmapping = (CANopenObjectListCANopenObjectPDOmapping)Enum.Parse(typeof(CANopenObjectListCANopenObjectPDOmapping),PDOtype.ToString());
AppLayer.CANopenObjectList.CANopenObject[count].PDOmappingSpecified = true;
AppLayer.CANopenObjectList.CANopenObject[count].uniqueIDRef = String.Format("UID_PARAM_{0:x4}", od.Index);
AppLayer.CANopenObjectList.CANopenObject[count].denotation = od.denotation;
AppLayer.CANopenObjectList.CANopenObject[count].edseditor_extenstion_storagelocation = od.prop.CO_storageGroup;
AppLayer.CANopenObjectList.CANopenObject[count].edseditor_extension_notifyonchange = od.prop.CO_flagsPDO;
AppLayer.CANopenObjectList.CANopenObject[count].highLimit = od.HighLimit;
AppLayer.CANopenObjectList.CANopenObject[count].lowLimit = od.LowLimit;
AppLayer.CANopenObjectList.CANopenObject[count].actualValue = od.actualvalue;
if (od.subobjects != null && od.subobjects.Count > 0)
{
AppLayer.CANopenObjectList.CANopenObject[count].subNumber = (byte)od.subobjects.Count;
AppLayer.CANopenObjectList.CANopenObject[count].subNumberSpecified = true;
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject = new CANopenObjectListCANopenObjectCANopenSubObject[od.subobjects.Count];
int subcount = 0;
foreach ( KeyValuePair<UInt16,ODentry> kvp2 in od.subobjects)
{
ODentry subod = kvp2.Value;
UInt16 subindex2 = kvp2.Key;
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject[subcount] = new CANopenObjectListCANopenObjectCANopenSubObject();
bytes = BitConverter.GetBytes((UInt16)kvp2.Key);
Array.Reverse(bytes);
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject[subcount].subIndex = bytes;
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject[subcount].name = subod.parameter_name;
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject[subcount].objectType = (byte)subod.objecttype;
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject[subcount].denotation = subod.denotation;
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject[subcount].edseditor_extension_notifyonchange = subod.prop.CO_flagsPDO;
bytes = (subod.datatype == DataType.UNKNOWN && od.parent != null) ? BitConverter.GetBytes((UInt16)od.parent.datatype) : BitConverter.GetBytes((UInt16)subod.datatype);
Array.Reverse(bytes);
// hack - special handling for rrw / rww access type
// https://github.com/robincornelius/libedssharp/issues/128
accesstype = subod.accesstype;
PDOtype = subod.PDOtype;
if (accesstype == EDSsharp.AccessType.rww) {
accesstype = EDSsharp.AccessType.rw;
// when optional is set,
if (PDOtype == PDOMappingType.optional) {
PDOtype = PDOMappingType.RPDO;
}
}
if (accesstype == EDSsharp.AccessType.rwr) {
accesstype = EDSsharp.AccessType.rw;
// when optional is set,
if (PDOtype == PDOMappingType.optional) {
PDOtype = PDOMappingType.TPDO;
}
}
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject[subcount].dataType = bytes;
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject[subcount].PDOmapping = (CANopenObjectListCANopenObjectCANopenSubObjectPDOmapping)Enum.Parse(typeof(CANopenObjectListCANopenObjectCANopenSubObjectPDOmapping),PDOtype.ToString());
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject[subcount].PDOmappingSpecified = true;
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject[subcount].uniqueIDRef = String.Format("UID_PARAM_{0:x4}{1:x2}", od.Index, subindex2);
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject[subcount].accessType = (CANopenObjectListCANopenObjectCANopenSubObjectAccessType)Enum.Parse(typeof(CANopenObjectListCANopenObjectCANopenSubObjectAccessType), accesstype.ToString());
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject[subcount].accessTypeSpecified = true;
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject[subcount].highLimit = subod.HighLimit;
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject[subcount].lowLimit = subod.LowLimit;
AppLayer.CANopenObjectList.CANopenObject[count].CANopenSubObject[subcount].actualValue = subod.actualvalue;
subcount++;
}
}
count++;
}
AppLayer.dummyUsage = new ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummy[7];
for (int x = 0; x < 7; x++)
{
AppLayer.dummyUsage[x] = new ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummy();
}
//FIX ME this is terrible
AppLayer.dummyUsage[0].entry = (ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummyEntry)Enum.Parse(typeof(ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummyEntry), string.Format("Dummy0001{0}", eds.du.Dummy0001 == true ? "1" : "0"));
AppLayer.dummyUsage[1].entry = (ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummyEntry)Enum.Parse(typeof(ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummyEntry), string.Format("Dummy0002{0}", eds.du.Dummy0002 == true ? "1" : "0"));
AppLayer.dummyUsage[2].entry = (ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummyEntry)Enum.Parse(typeof(ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummyEntry), string.Format("Dummy0003{0}", eds.du.Dummy0003 == true ? "1" : "0"));
AppLayer.dummyUsage[3].entry = (ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummyEntry)Enum.Parse(typeof(ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummyEntry), string.Format("Dummy0004{0}", eds.du.Dummy0004 == true ? "1" : "0"));
AppLayer.dummyUsage[4].entry = (ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummyEntry)Enum.Parse(typeof(ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummyEntry), string.Format("Dummy0005{0}", eds.du.Dummy0005 == true ? "1" : "0"));
AppLayer.dummyUsage[5].entry = (ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummyEntry)Enum.Parse(typeof(ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummyEntry), string.Format("Dummy0006{0}", eds.du.Dummy0006 == true ? "1" : "0"));
AppLayer.dummyUsage[6].entry = (ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummyEntry)Enum.Parse(typeof(ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummyEntry), string.Format("Dummy0007{0}", eds.du.Dummy0007 == true ? "1" : "0"));
TransportLayer.PhysicalLayer = new ProfileBody_CommunicationNetwork_CANopenTransportLayersPhysicalLayer();
//FIX me this is worse than above
int bauds = 0;
if (eds.di.BaudRate_10 == true)
bauds++;
if (eds.di.BaudRate_20 == true)
bauds++;
if (eds.di.BaudRate_50 == true)
bauds++;
if (eds.di.BaudRate_125 == true)
bauds++;
if (eds.di.BaudRate_250 == true)
bauds++;
if (eds.di.BaudRate_500 == true)
bauds++;
if (eds.di.BaudRate_800 == true)
bauds++;
if (eds.di.BaudRate_1000 == true)
bauds++;
//Fixme auto baudrate needs adding to system
TransportLayer.PhysicalLayer.baudRate = new ProfileBody_CommunicationNetwork_CANopenTransportLayersPhysicalLayerBaudRate();
TransportLayer.PhysicalLayer.baudRate.supportedBaudRate = new ProfileBody_CommunicationNetwork_CANopenTransportLayersPhysicalLayerBaudRateSupportedBaudRate[bauds];
for (int x = 0; x < bauds; x++)
{
TransportLayer.PhysicalLayer.baudRate.supportedBaudRate[x] = new ProfileBody_CommunicationNetwork_CANopenTransportLayersPhysicalLayerBaudRateSupportedBaudRate();
}
bauds = 0;
if (eds.di.BaudRate_10 == true)
{
TransportLayer.PhysicalLayer.baudRate.supportedBaudRate[bauds].value = ProfileBody_CommunicationNetwork_CANopenTransportLayersPhysicalLayerBaudRateSupportedBaudRateValue.Item10Kbps;
bauds++;
}
if (eds.di.BaudRate_20 == true)
{
TransportLayer.PhysicalLayer.baudRate.supportedBaudRate[bauds].value = ProfileBody_CommunicationNetwork_CANopenTransportLayersPhysicalLayerBaudRateSupportedBaudRateValue.Item20Kbps;
bauds++;
}
if (eds.di.BaudRate_50 == true)
{
TransportLayer.PhysicalLayer.baudRate.supportedBaudRate[bauds].value = ProfileBody_CommunicationNetwork_CANopenTransportLayersPhysicalLayerBaudRateSupportedBaudRateValue.Item50Kbps;
bauds++;
}
if (eds.di.BaudRate_125 == true)
{
TransportLayer.PhysicalLayer.baudRate.supportedBaudRate[bauds].value = ProfileBody_CommunicationNetwork_CANopenTransportLayersPhysicalLayerBaudRateSupportedBaudRateValue.Item125Kbps;
bauds++;
}
if (eds.di.BaudRate_250 == true)
{
TransportLayer.PhysicalLayer.baudRate.supportedBaudRate[bauds].value = ProfileBody_CommunicationNetwork_CANopenTransportLayersPhysicalLayerBaudRateSupportedBaudRateValue.Item250Kbps;
bauds++;
}
if (eds.di.BaudRate_500 == true)
{
TransportLayer.PhysicalLayer.baudRate.supportedBaudRate[bauds].value = ProfileBody_CommunicationNetwork_CANopenTransportLayersPhysicalLayerBaudRateSupportedBaudRateValue.Item500Kbps;
bauds++;
}
if (eds.di.BaudRate_800 == true)
{
TransportLayer.PhysicalLayer.baudRate.supportedBaudRate[bauds].value = ProfileBody_CommunicationNetwork_CANopenTransportLayersPhysicalLayerBaudRateSupportedBaudRateValue.Item800Kbps;
bauds++;
}
if (eds.di.BaudRate_1000 == true)
{
TransportLayer.PhysicalLayer.baudRate.supportedBaudRate[bauds].value = ProfileBody_CommunicationNetwork_CANopenTransportLayersPhysicalLayerBaudRateSupportedBaudRateValue.Item1000Kbps;
bauds++;
}
NetworkManagement.CANopenGeneralFeatures = new ProfileBody_CommunicationNetwork_CANopenNetworkManagementCANopenGeneralFeatures();
NetworkManagement.CANopenGeneralFeatures.bootUpSlave = eds.di.SimpleBootUpSlave;
//NetworkManagment.CANopenGeneralFeatures.dynamicChannels = eds.di.DynamicChannelsSupported; //fix me count of dynamic channels not handled yet eds only has bool
NetworkManagement.CANopenGeneralFeatures.granularity = eds.di.Granularity;
NetworkManagement.CANopenGeneralFeatures.groupMessaging = eds.di.GroupMessaging;
NetworkManagement.CANopenGeneralFeatures.ngMaster = eds.di.NG_Master;
NetworkManagement.CANopenGeneralFeatures.ngSlave = eds.di.NG_Slave;
NetworkManagement.CANopenGeneralFeatures.NrOfNG_MonitoredNodes = eds.di.NrOfNG_MonitoredNodes;
NetworkManagement.CANopenGeneralFeatures.layerSettingServiceSlave = eds.di.LSS_Supported;
NetworkManagement.CANopenGeneralFeatures.nrOfRxPDO = eds.di.NrOfRXPDO;
NetworkManagement.CANopenGeneralFeatures.nrOfTxPDO = eds.di.NrOfTXPDO;
//extra items
//NetworkManagment.CANopenGeneralFeatures.SDORequestingDevice;
//NetworkManagment.CANopenGeneralFeatures.selfStartingDevice;
NetworkManagement.CANopenMasterFeatures = new ProfileBody_CommunicationNetwork_CANopenNetworkManagementCANopenMasterFeatures();
NetworkManagement.CANopenMasterFeatures.bootUpMaster = eds.di.SimpleBootUpMaster;
//Extra items
//NetworkManagment.CANopenMasterFeatures.configurationManager;
//NetworkManagment.CANopenMasterFeatures.flyingMaster;
NetworkManagement.CANopenMasterFeatures.layerSettingServiceMaster = eds.di.LSS_Master;
//NetworkManagment.CANopenMasterFeatures.SDOManager;
NetworkManagement.deviceCommissioning = new ProfileBody_CommunicationNetwork_CANopenNetworkManagementDeviceCommissioning();
NetworkManagement.deviceCommissioning.actualBaudRate = eds.dc.Baudrate.ToString();
NetworkManagement.deviceCommissioning.NodeID = eds.dc.NodeID;
NetworkManagement.deviceCommissioning.networkName = eds.dc.NetworkName;
NetworkManagement.deviceCommissioning.networkNumber = eds.dc.NetNumber;
NetworkManagement.deviceCommissioning.CANopenManager = eds.dc.CANopenManager;
//fixme unconnected
//eds.dc.LSS_SerialNumber;
return dev;
}
public EDSsharp convert(ISO15745ProfileContainer container)
{
EDSsharp eds = new EDSsharp();
//Find Object Dictionary entries
//fixme??
// ProfileBody_DataType dt;
ProfileBody_CommunicationNetwork_CANopen body_network = null;
ProfileBody_Device_CANopen body_device = null;
foreach (ISO15745Profile dev in container.ISO15745Profile)
{
if (dev.ProfileBody.GetType() == typeof(ProfileBody_CommunicationNetwork_CANopen))
{
body_network = (ProfileBody_CommunicationNetwork_CANopen)dev.ProfileBody;
}
if (dev.ProfileBody.GetType() == typeof(ProfileBody_Device_CANopen))
{
body_device = (ProfileBody_Device_CANopen)dev.ProfileBody;
}
}
//ProfileBody_CommunicationNetwork_CANopen
if (body_network != null)
{
ProfileBody_CommunicationNetwork_CANopen obj = body_network;
ProfileBody_CommunicationNetwork_CANopenApplicationLayers ApplicationLayers = null;
ProfileBody_CommunicationNetwork_CANopenTransportLayers TransportLayers = null;
ProfileBody_CommunicationNetwork_CANopenNetworkManagement NetworkManagment = null;
foreach (object obj2 in obj.Items)
{
if (obj2.GetType() == typeof(ProfileBody_CommunicationNetwork_CANopenApplicationLayers))
ApplicationLayers = (ProfileBody_CommunicationNetwork_CANopenApplicationLayers)obj2;
if (obj2.GetType() == typeof(ProfileBody_CommunicationNetwork_CANopenTransportLayers))
TransportLayers = (ProfileBody_CommunicationNetwork_CANopenTransportLayers)obj2;
if (obj2.GetType() == typeof(ProfileBody_CommunicationNetwork_CANopenNetworkManagement))
NetworkManagment = (ProfileBody_CommunicationNetwork_CANopenNetworkManagement)obj2;
}
if (ApplicationLayers != null)
{
string vendorID = "";
//fixme
//string deviceFamily = "";
string productID = "";
//fixme
//string version = "";
DateTime buildDate;
string specificationRevision = "";
if (ApplicationLayers.identity != null)
{
if (ApplicationLayers.identity.vendorID != null)
{
vendorID = ApplicationLayers.identity.vendorID.Value;
}
if (ApplicationLayers.identity.deviceFamily != null)
{
//deviceFamily = ApplicationLayers.identity.deviceFamily.Items[]
//not really sure how to handle this. its a list of g_labels
//these contain label, description, language, URi etc could do with a simple class
//to wrap these in as they are used in a number of places
}
if (ApplicationLayers.identity.productID != null)
{
productID = ApplicationLayers.identity.productID.Value;
}
if (ApplicationLayers.identity.buildDate != default)
{
buildDate = ApplicationLayers.identity.buildDate;
}
if (ApplicationLayers.identity.specificationRevision != null)
{
specificationRevision = ApplicationLayers.identity.specificationRevision.Value;
}
}
if (ApplicationLayers.dummyUsage != null)
{
foreach (ProfileBody_CommunicationNetwork_CANopenApplicationLayersDummy dummy in ApplicationLayers.dummyUsage)
{
string pat = @"Dummy([0-9]{4})([0-1])";
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
Match m = r.Match(dummy.entry.ToString());
if (m.Success)
{
int index = int.Parse(m.Groups[1].Value);
switch (index)
{
case 1:
eds.du.Dummy0001 = int.Parse(m.Groups[2].Value) == 1;
break;
case 2:
eds.du.Dummy0002 = int.Parse(m.Groups[2].Value) == 1;
break;
case 3:
eds.du.Dummy0003 = int.Parse(m.Groups[2].Value) == 1;
break;
case 4:
eds.du.Dummy0004 = int.Parse(m.Groups[2].Value) == 1;
break;
case 5:
eds.du.Dummy0005 = int.Parse(m.Groups[2].Value) == 1;
break;
case 6:
eds.du.Dummy0006 = int.Parse(m.Groups[2].Value) == 1;
break;
case 7:
eds.du.Dummy0007 = int.Parse(m.Groups[2].Value) == 1;
break;
}
}
}
} //dummyusage != null
if (ApplicationLayers.dynamicChannels != null)
{
}
if (ApplicationLayers.conformanceClass != null)
{
}
if (ApplicationLayers.communicationEntityType != null)
{
}
} //application layer
if (TransportLayers != null)
{
if(TransportLayers.PhysicalLayer!=null)
{
if (TransportLayers.PhysicalLayer.baudRate != null)
{
if (TransportLayers.PhysicalLayer.baudRate.supportedBaudRate != null)
{
foreach (ProfileBody_CommunicationNetwork_CANopenTransportLayersPhysicalLayerBaudRateSupportedBaudRate baud in TransportLayers.PhysicalLayer.baudRate.supportedBaudRate)
{
if (baud.value.ToString() == "Item10Kbps")
eds.di.BaudRate_10 = true;
if (baud.value.ToString() == "Item20Kbps")
eds.di.BaudRate_20 = true;
if (baud.value.ToString() == "Item50Kbps")
eds.di.BaudRate_50 = true;
if (baud.value.ToString() == "Item125Kbps")
eds.di.BaudRate_125 = true;
if (baud.value.ToString() == "Item250Kbps")
eds.di.BaudRate_250 = true;
if (baud.value.ToString() == "Item500Kbps")
eds.di.BaudRate_500 = true;
if (baud.value.ToString() == "Item800Kbps")
eds.di.BaudRate_800 = true;
if (baud.value.ToString() == "Item1000Kbps")
eds.di.BaudRate_1000 = true;
//fixme "auto-baudRate" is a valid identifier here as well
}
}
}
}
} //Transport layer
eds.di.LSS_Supported = false;
eds.di.LSS_Master = false;
if (NetworkManagment != null)
{
if (NetworkManagment.CANopenMasterFeatures != null)
{
eds.di.SimpleBootUpMaster = NetworkManagment.CANopenMasterFeatures.bootUpMaster;
//fixme Extra items
//NetworkManagment.CANopenMasterFeatures.configurationManager;
//NetworkManagment.CANopenMasterFeatures.flyingMaster;
//Fix me if Client and Server are set in XDD i can't deal with this and will default to Server
if (NetworkManagment.CANopenMasterFeatures.layerSettingServiceMaster)
{
eds.di.LSS_Master = true;
}
//NetworkManagment.CANopenMasterFeatures.SDOManager;
}
if (NetworkManagment.CANopenGeneralFeatures != null)
{
eds.di.SimpleBootUpSlave = NetworkManagment.CANopenGeneralFeatures.bootUpSlave;
eds.di.DynamicChannelsSupported = NetworkManagment.CANopenGeneralFeatures.dynamicChannels > 0;
//fix me count of dynamic channels not handled yet eds only has bool
eds.di.Granularity = NetworkManagment.CANopenGeneralFeatures.granularity;
eds.di.GroupMessaging = NetworkManagment.CANopenGeneralFeatures.groupMessaging;
//Fix me if Client and Server are set in XDD i can't deal with this and will default to Server
if (NetworkManagment.CANopenGeneralFeatures.layerSettingServiceSlave)
{
eds.di.LSS_Supported = true;
}
eds.di.NG_Master = NetworkManagment.CANopenGeneralFeatures.ngMaster;
eds.di.NG_Slave = NetworkManagment.CANopenGeneralFeatures.ngSlave;
eds.di.NrOfNG_MonitoredNodes = NetworkManagment.CANopenGeneralFeatures.NrOfNG_MonitoredNodes;
eds.di.NrOfRXPDO = NetworkManagment.CANopenGeneralFeatures.nrOfRxPDO;
eds.di.NrOfTXPDO = NetworkManagment.CANopenGeneralFeatures.nrOfTxPDO;
//fixme extra items
//NetworkManagment.CANopenGeneralFeatures.SDORequestingDevice;
//NetworkManagment.CANopenGeneralFeatures.selfStartingDevice;
}
if (NetworkManagment.deviceCommissioning != null)
{
eds.dc.NodeID = NetworkManagment.deviceCommissioning.NodeID;
eds.dc.Baudrate = Convert.ToUInt16(NetworkManagment.deviceCommissioning.actualBaudRate);
eds.dc.CANopenManager = NetworkManagment.deviceCommissioning.CANopenManager;
eds.dc.NetworkName = NetworkManagment.deviceCommissioning.networkName;
eds.dc.NetNumber = Convert.ToUInt16(NetworkManagment.deviceCommissioning.networkNumber);
eds.dc.NodeName = NetworkManagment.deviceCommissioning.nodeName;
}
}
if (ApplicationLayers.CANopenObjectList.CANopenObject != null)
foreach (CanOpenXSD_1_0.CANopenObjectListCANopenObject obj3 in ApplicationLayers.CANopenObjectList.CANopenObject)
{
ODentry entry = new ODentry();
UInt16 index;
if (obj3.index != null)
{
index = (UInt16)EDSsharp.ConvertToUInt16(obj3.index);
entry.Index = index;
}
else
continue; //unparseable
if (obj3.name != null)
entry.parameter_name = obj3.name;
entry.objecttype = (ObjectType)obj3.objectType;
if (obj3.dataType != null)
entry.datatype = (DataType)EDSsharp.ConvertToUInt16(obj3.dataType);
if (obj3.defaultValue != null)
entry.defaultvalue = obj3.defaultValue;
if (obj3.highLimit != null)
entry.HighLimit = obj3.highLimit;
if (obj3.lowLimit != null)
entry.LowLimit = obj3.lowLimit;
if (obj3.actualValue != null)
entry.actualvalue = obj3.actualValue;
if (obj3.denotation != null)
entry.denotation = obj3.denotation;