darra-ethercat-master 2.7.0

Commercial EtherCAT master protocol stack, real-time kernel driver integration, Windows and Linux support, multi-language SDKs, complex topology and hot-plug support.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290

use std::collections::HashMap;
use crate::data::error::{DarraError, Result};
use crate::data::types::SDOError;
use crate::utils::ffi;
use std::os::raw::c_int;

#[derive(Debug, Clone)]
pub struct PdoMapEntry {

    pub od_index: u16,

    pub sub_index: u8,

    pub bit_length: u8,

    pub byte_offset: u32,

    pub bit_offset: u8,

    pub is_output: bool,
}

impl PdoMapEntry {

    pub fn byte_size(&self) -> u32 {
        (self.bit_length as u32 + 7) / 8
    }
}

pub struct Cia402PdoMap {

    master_index: u16,

    slave_index: u16,

    pub(crate) output_map: HashMap<(u16, u8), PdoMapEntry>,

    pub(crate) input_map: HashMap<(u16, u8), PdoMapEntry>,

    out_ptr: *mut u8,

    out_size: i32,

    in_ptr: *mut u8,

    in_size: i32,
}

unsafe impl Send for Cia402PdoMap {}
unsafe impl Sync for Cia402PdoMap {}

impl Cia402PdoMap {

    pub fn scan(master_index: u16, slave_index: u16) -> Result<Self> {

        let mut out_size: c_int = 0;
        let mut out_ptr: *mut u8 = std::ptr::null_mut();
        let mut in_size: c_int = 0;
        let mut in_ptr: *mut u8 = std::ptr::null_mut();
        let io_ok = unsafe {
            ffi::GetIO(master_index, slave_index,
                       &mut out_size, &mut out_ptr,
                       &mut in_size, &mut in_ptr)
        };
        if io_ok == 0 {
            return Err(DarraError::PdoFailed("获取从站 IOmap 失败".into()));
        }

        let mut output_map = HashMap::new();
        let mut input_map = HashMap::new();

        let rxpdo_count = Self::read_u8_sdo(master_index, slave_index, 0x1C12, 0)
            .unwrap_or(0);

        let mut out_byte_offset: u32 = 0;
        for sm_idx in 1..=rxpdo_count {

            let pdo_idx = match Self::read_u16_sdo(master_index, slave_index, 0x1C12, sm_idx) {
                Ok(v) => v,
                Err(_) => continue,
            };

            let map_count = Self::read_u8_sdo(master_index, slave_index, pdo_idx, 0)
                .unwrap_or(0);
            for map_sub in 1..=map_count {
                let map_val = match Self::read_u32_sdo(master_index, slave_index, pdo_idx, map_sub) {
                    Ok(v) => v,
                    Err(_) => continue,
                };

                let obj_idx   = ((map_val >> 16) & 0xFFFF) as u16;
                let obj_sub   = ((map_val >> 8)  & 0xFF) as u8;
                let bit_len   = (map_val & 0xFF) as u8;
                if obj_idx == 0 || bit_len == 0 { continue; }
                let entry = PdoMapEntry {
                    od_index:    obj_idx,
                    sub_index:   obj_sub,
                    bit_length:  bit_len,
                    byte_offset: out_byte_offset,
                    bit_offset:  0,
                    is_output:   true,
                };
                out_byte_offset += (bit_len as u32 + 7) / 8;
                output_map.insert((obj_idx, obj_sub), entry);
            }
        }

        let txpdo_count = Self::read_u8_sdo(master_index, slave_index, 0x1C13, 0)
            .unwrap_or(0);

        let mut in_byte_offset: u32 = 0;
        for sm_idx in 1..=txpdo_count {
            let pdo_idx = match Self::read_u16_sdo(master_index, slave_index, 0x1C13, sm_idx) {
                Ok(v) => v,
                Err(_) => continue,
            };
            let map_count = Self::read_u8_sdo(master_index, slave_index, pdo_idx, 0)
                .unwrap_or(0);
            for map_sub in 1..=map_count {
                let map_val = match Self::read_u32_sdo(master_index, slave_index, pdo_idx, map_sub) {
                    Ok(v) => v,
                    Err(_) => continue,
                };
                let obj_idx   = ((map_val >> 16) & 0xFFFF) as u16;
                let obj_sub   = ((map_val >> 8)  & 0xFF) as u8;
                let bit_len   = (map_val & 0xFF) as u8;
                if obj_idx == 0 || bit_len == 0 { continue; }
                let entry = PdoMapEntry {
                    od_index:    obj_idx,
                    sub_index:   obj_sub,
                    bit_length:  bit_len,
                    byte_offset: in_byte_offset,
                    bit_offset:  0,
                    is_output:   false,
                };
                in_byte_offset += (bit_len as u32 + 7) / 8;
                input_map.insert((obj_idx, obj_sub), entry);
            }
        }

        Ok(Self {
            master_index,
            slave_index,
            output_map,
            input_map,
            out_ptr,
            out_size,
            in_ptr,
            in_size,
        })
    }

    pub fn read_u32(&self, od_index: u16, sub_index: u8) -> Result<u32> {
        if let Some(entry) = self.input_map.get(&(od_index, sub_index)) {

            let off = entry.byte_offset as usize;
            let sz  = entry.byte_size() as usize;
            if !self.in_ptr.is_null() && off + sz <= self.in_size as usize {
                let val = unsafe {
                    let ptr = self.in_ptr.add(off);
                    match sz {
                        1 => *ptr as u32,
                        2 => u16::from_le_bytes([*ptr, *ptr.add(1)]) as u32,
                        4 => u32::from_le_bytes([*ptr, *ptr.add(1), *ptr.add(2), *ptr.add(3)]),
                        _ => 0,
                    }
                };
                return Ok(val);
            }
        }

        Self::read_u32_sdo(self.master_index, self.slave_index, od_index, sub_index)
    }

    pub fn write_u32(&self, od_index: u16, sub_index: u8, value: u32) -> Result<()> {
        if let Some(entry) = self.output_map.get(&(od_index, sub_index)) {
            let off = entry.byte_offset as usize;
            let sz  = entry.byte_size() as usize;
            if !self.out_ptr.is_null() && off + sz <= self.out_size as usize {
                unsafe {
                    let ptr = self.out_ptr.add(off);
                    let bytes = value.to_le_bytes();
                    std::ptr::copy_nonoverlapping(bytes.as_ptr(), ptr, sz.min(4));
                }
                return Ok(());
            }
        }

        let bytes = value.to_le_bytes();
        let ok = unsafe {
            ffi::SDOwrite_raw(self.master_index, self.slave_index, od_index, sub_index,
                              0, bytes.as_ptr(), 4)
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::SdoWriteFailed { index: od_index, subindex: sub_index, abort_code: None }) }
    }

    pub fn read_u16(&self, od_index: u16, sub_index: u8) -> Result<u16> {
        Ok(self.read_u32(od_index, sub_index)? as u16)
    }

    pub fn write_u16(&self, od_index: u16, sub_index: u8, value: u16) -> Result<()> {
        if let Some(entry) = self.output_map.get(&(od_index, sub_index)) {
            let off = entry.byte_offset as usize;
            if !self.out_ptr.is_null() && off + 2 <= self.out_size as usize {
                unsafe {
                    let bytes = value.to_le_bytes();
                    std::ptr::copy_nonoverlapping(bytes.as_ptr(), self.out_ptr.add(off), 2);
                }
                return Ok(());
            }
        }
        let bytes = value.to_le_bytes();
        let ok = unsafe {
            ffi::SDOwrite_raw(self.master_index, self.slave_index, od_index, sub_index,
                              0, bytes.as_ptr(), 2)
        };
        if ok != 0 { Ok(()) } else { Err(DarraError::SdoWriteFailed { index: od_index, subindex: sub_index, abort_code: None }) }
    }

    pub fn output_entries(&self) -> impl Iterator<Item = &PdoMapEntry> {
        self.output_map.values()
    }

    pub fn input_entries(&self) -> impl Iterator<Item = &PdoMapEntry> {
        self.input_map.values()
    }

    pub fn is_output_mapped(&self, od_index: u16, sub_index: u8) -> bool {
        self.output_map.contains_key(&(od_index, sub_index))
    }

    pub fn is_input_mapped(&self, od_index: u16, sub_index: u8) -> bool {
        self.input_map.contains_key(&(od_index, sub_index))
    }

    fn read_u8_sdo(master: u16, slave: u16, index: u16, sub: u8) -> Result<u8> {
        let mut size: c_int = 0;
        let ptr = unsafe { ffi::SDOread(master, slave, index, sub, 0, &mut size) };
        if ptr.is_null() || size < 1 {
            if !ptr.is_null() { unsafe { ffi::FreeMemory(ptr as *mut _) }; }
            return Err(DarraError::SdoReadFailed { index, subindex: sub, abort_code: None });
        }
        let val = unsafe { *ptr };
        unsafe { ffi::FreeMemory(ptr as *mut _) };
        Ok(val)
    }

    fn read_u16_sdo(master: u16, slave: u16, index: u16, sub: u8) -> Result<u16> {
        let mut size: c_int = 0;
        let ptr = unsafe { ffi::SDOread(master, slave, index, sub, 0, &mut size) };
        if ptr.is_null() || size < 2 {
            if !ptr.is_null() { unsafe { ffi::FreeMemory(ptr as *mut _) }; }
            return Err(DarraError::SdoReadFailed { index, subindex: sub, abort_code: None });
        }
        let val = unsafe { u16::from_le_bytes([*ptr, *ptr.add(1)]) };
        unsafe { ffi::FreeMemory(ptr as *mut _) };
        Ok(val)
    }

    fn read_u32_sdo(master: u16, slave: u16, index: u16, sub: u8) -> Result<u32> {
        let mut size: c_int = 0;
        let ptr = unsafe { ffi::SDOread(master, slave, index, sub, 0, &mut size) };
        if ptr.is_null() || size < 4 {
            if !ptr.is_null() { unsafe { ffi::FreeMemory(ptr as *mut _) }; }
            return Err(DarraError::SdoReadFailed { index, subindex: sub, abort_code: None });
        }
        let val = unsafe {
            u32::from_le_bytes([*ptr, *ptr.add(1), *ptr.add(2), *ptr.add(3)])
        };
        unsafe { ffi::FreeMemory(ptr as *mut _) };
        Ok(val)
    }
}

impl std::fmt::Debug for Cia402PdoMap {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Cia402PdoMap")
            .field("master_index", &self.master_index)
            .field("slave_index", &self.slave_index)
            .field("output_entries", &self.output_map.len())
            .field("input_entries", &self.input_map.len())
            .finish()
    }
}

pub const OD_CONTROLWORD: u16 = 0x6040;

pub const OD_STATUSWORD: u16 = 0x6041;

pub const OD_MODES_OF_OPERATION: u16 = 0x6060;

pub const OD_MODES_OF_OPERATION_DISPLAY: u16 = 0x6061;

pub const OD_TARGET_POSITION: u16 = 0x607A;

pub const OD_POSITION_ACTUAL: u16 = 0x6064;

pub const OD_TARGET_VELOCITY: u16 = 0x60FF;

pub const OD_VELOCITY_ACTUAL: u16 = 0x606C;

pub const OD_TARGET_TORQUE: u16 = 0x6071;

pub const OD_TORQUE_ACTUAL: u16 = 0x6077;

pub const OD_MAX_PROFILE_VELOCITY: u16 = 0x6080;

pub const OD_PROFILE_VELOCITY: u16 = 0x6081;

pub const OD_PROFILE_ACCELERATION: u16 = 0x6083;

pub const OD_PROFILE_DECELERATION: u16 = 0x6084;

pub const OD_QUICK_STOP_DECELERATION: u16 = 0x6085;

pub const OD_HOMING_METHOD: u16 = 0x6098;

pub const OD_HOMING_SPEEDS: u16 = 0x6099;

pub const OD_HOME_OFFSET: u16 = 0x607C;

pub const OD_TORQUE_OFFSET: u16 = 0x60B2;

pub const OD_SOFTWARE_POSITION_LIMIT: u16 = 0x607D;

pub const OD_SUPPORTED_DRIVE_MODES: u16 = 0x6502;

pub const OD_POLARITY: u16 = 0x607E;

pub const OD_MOTION_PROFILE_TYPE: u16 = 0x6086;

pub const OD_TOUCH_PROBE_FUNCTION: u16 = 0x60B8;

pub const OD_TOUCH_PROBE_STATUS: u16 = 0x60B9;

pub const OD_TOUCH_PROBE_POS_EDGE: u16 = 0x60BA;

pub const OD_TOUCH_PROBE_NEG_EDGE: u16 = 0x60BB;

pub const OD_HOMING_ACCELERATION: u16 = 0x609A;

pub const OD_DIGITAL_INPUTS: u16 = 0x60FD;

pub const OD_DIGITAL_OUTPUTS: u16 = 0x60FE;

pub const OD_MAX_TORQUE: u16 = 0x6072;

pub const OD_MOTOR_RATED_TORQUE: u16 = 0x6076;

pub const OD_POSITION_OFFSET: u16 = 0x60B0;

pub const OD_VELOCITY_OFFSET: u16 = 0x60B1;

pub const OD_INTERPOLATION_TIME_PERIOD: u16 = 0x60C2;

pub const OD_QUICK_STOP_OPTION_CODE: u16 = 0x605A;

pub const OD_POSITIVE_TORQUE_LIMIT: u16 = 0x60E0;

pub const OD_NEGATIVE_TORQUE_LIMIT: u16 = 0x60E1;

pub const OD_SUPPORTED_HOMING_METHODS: u16 = 0x60E3;

pub const OD_TXPDO_DATA_INVALID: u16 = 0x603E;

pub const OD_SYNCHRONIZATION_SETTINGS: u16 = 0x60D9;

pub const OD_DRIVE_SYNC_STATUS: u16 = 0x60DA;

pub const OD_ERROR_CODE: u16 = 0x603F;

pub const OD_DRIVE_DATA: u16 = 0x6510;

pub const CW_SHUTDOWN: u16 = 0x06;

pub const CW_SWITCH_ON: u16 = 0x07;

pub const CW_ENABLE_OPERATION: u16 = 0x0F;

pub const CW_DISABLE_VOLTAGE: u16 = 0x00;

pub const CW_QUICK_STOP: u16 = 0x02;

pub const CW_FAULT_RESET: u16 = 0x80;

pub const CW_HALT: u16 = 0x0100;

pub const SW_READY_TO_SWITCH_ON: u16 = 0x0001;

pub const SW_SWITCHED_ON: u16 = 0x0002;

pub const SW_OPERATION_ENABLED: u16 = 0x0004;

pub const SW_FAULT: u16 = 0x0008;

pub const SW_VOLTAGE_ENABLED: u16 = 0x0010;

pub const SW_QUICK_STOP: u16 = 0x0020;

pub const SW_SWITCH_ON_DISABLED: u16 = 0x0040;

pub const SW_WARNING: u16 = 0x0080;

pub const SW_REMOTE: u16 = 0x0200;

pub const SW_TARGET_REACHED: u16 = 0x0400;

pub const SW_INTERNAL_LIMIT: u16 = 0x0800;

pub const SW_OP_MODE_SPECIFIC_1: u16 = 0x1000;

pub const SW_OP_MODE_SPECIFIC_2: u16 = 0x2000;

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StateCiA402 {

    NotReadyToSwitchOn = 0,

    SwitchOnDisabled = 1,

    ReadyToSwitchOn = 2,

    SwitchedOn = 3,

    OperationEnabled = 4,

    QuickStopActive = 5,

    FaultReactionActive = 6,

    Fault = 7,

    Unknown = 99,
}

impl StateCiA402 {

    pub fn from_statusword(sw: u16) -> Self {
        if (sw & SW_FAULT) != 0 {
            if (sw & 0x0F) == 0x0F {
                return Self::FaultReactionActive;
            }
            return Self::Fault;
        }
        let mask = sw & 0x006F;
        match mask {
            0x0000 => Self::NotReadyToSwitchOn,
            0x0040 => Self::SwitchOnDisabled,
            0x0021 => Self::ReadyToSwitchOn,
            0x0023 => Self::SwitchedOn,
            0x0027 => Self::OperationEnabled,
            0x0007 => Self::QuickStopActive,
            _ => Self::Unknown,
        }
    }

    pub fn description(&self) -> &'static str {
        match self {
            Self::NotReadyToSwitchOn => "初始化中",
            Self::SwitchOnDisabled => "驱动禁用",
            Self::ReadyToSwitchOn => "准备就绪",
            Self::SwitchedOn => "已开启",
            Self::OperationEnabled => "运行使能",
            Self::QuickStopActive => "快速停止",
            Self::FaultReactionActive => "故障反应中",
            Self::Fault => "故障",
            Self::Unknown => "未知",
        }
    }
}

impl std::fmt::Display for StateCiA402 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.description())
    }
}

#[repr(i8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ModeCiA402 {

    PP = 1,

    VL = 2,

    PV = 3,

    PT = 4,

    HM = 6,

    IP = 7,

    CSP = 8,

    CSV = 9,

    CST = 10,

    CSTCA = 11,
}

impl ModeCiA402 {

    pub fn supported_bit(&self) -> Option<u32> {
        match self {
            Self::PP => Some(0),
            Self::VL => Some(1),
            Self::PV => Some(2),
            Self::PT => Some(3),
            Self::HM => Some(5),
            Self::IP => Some(6),
            Self::CSP => Some(7),
            Self::CSV => Some(8),
            Self::CST => Some(9),
            Self::CSTCA => Some(10),
        }
    }

    pub fn description(&self) -> &'static str {
        match self {
            Self::PP => "轮廓位置 (PP)",
            Self::VL => "速度 (VL)",
            Self::PV => "轮廓速度 (PV)",
            Self::PT => "轮廓转矩 (PT)",
            Self::HM => "回零 (HM)",
            Self::IP => "插补位置 (IP)",
            Self::CSP => "周期同步位置 (CSP)",
            Self::CSV => "周期同步速度 (CSV)",
            Self::CST => "周期同步转矩 (CST)",
            Self::CSTCA => "周期同步转矩加速度 (CSTCA)",
        }
    }
}

impl std::fmt::Display for ModeCiA402 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.description())
    }
}

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Cia402ReadStatus {

    NotAttempted = 0,

    Success = 1,

    NotSupported = 2,

    ReadFailed = 3,
}

impl Cia402ReadStatus {

    pub fn classify_abort(abort_code: u32) -> Self {
        match SDOError::from_u32(abort_code) {
            SDOError::ObjectDoesNotExist | SDOError::SubindexDoesNotExist => Self::NotSupported,
            _ => Self::ReadFailed,
        }
    }

    pub fn description(&self) -> &'static str {
        match self {
            Self::NotAttempted => "未读取",
            Self::Success => "读取成功",
            Self::NotSupported => "从站不支持",
            Self::ReadFailed => "读取失败",
        }
    }
}

impl std::fmt::Display for Cia402ReadStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.description())
    }
}

#[derive(Debug, Clone, Copy)]
pub struct Cia402ReadResult<T> {

    pub value: T,

    pub status: Cia402ReadStatus,

    pub abort_code: u32,
}

impl<T> Cia402ReadResult<T> {

    pub fn new(value: T, status: Cia402ReadStatus, abort_code: u32) -> Self {
        Self { value, status, abort_code }
    }

    pub fn ok(&self) -> bool {
        self.status == Cia402ReadStatus::Success
    }

    pub fn not_supported(&self) -> bool {
        self.status == Cia402ReadStatus::NotSupported
    }
}

pub trait Cia402Readable: Copy + Default {

    fn from_le_slice(data: &[u8]) -> Self;
}

macro_rules! impl_cia402_readable {
    ($($t:ty),+ $(,)?) => {$(
        impl Cia402Readable for $t {
            fn from_le_slice(data: &[u8]) -> Self {
                const N: usize = std::mem::size_of::<$t>();
                let mut buf = [0u8; N];
                let n = data.len().min(N);
                buf[..n].copy_from_slice(&data[..n]);
                <$t>::from_le_bytes(buf)
            }
        }
    )+};
}
impl_cia402_readable!(u8, i8, u16, i16, u32, i32, u64, i64);

pub struct CiA402Instance {

    master_index: u16,

    slave_index: u16,

    pdo_map: Cia402PdoMap,
}

impl CiA402Instance {

    pub fn new(master_index: u16, slave_index: u16, pdo_map: Cia402PdoMap) -> Self {
        Self { master_index, slave_index, pdo_map }
    }

    pub fn slave_index(&self) -> u16 {
        self.slave_index
    }

    pub fn pdo_map(&self) -> &Cia402PdoMap {
        &self.pdo_map
    }

    pub fn initialize_pdo_offsets(&mut self) -> Result<()> {
        self.pdo_map = Cia402PdoMap::scan(self.master_index, self.slave_index)?;
        Ok(())
    }

    pub fn reset_pdo_offsets(&mut self) {
        self.pdo_map.output_map.clear();
        self.pdo_map.input_map.clear();
    }

    fn sdo_read_u16(&self, index: u16, sub: u8) -> u16 {
        self.pdo_map.read_u16(index, sub).unwrap_or(0)
    }

    fn sdo_read_i32(&self, index: u16, sub: u8) -> i32 {
        self.pdo_map.read_u32(index, sub).unwrap_or(0) as i32
    }

    fn sdo_read_u32(&self, index: u16, sub: u8) -> u32 {
        self.pdo_map.read_u32(index, sub).unwrap_or(0)
    }

    fn sdo_read_u8(&self, index: u16, sub: u8) -> u8 {
        self.pdo_map.read_u32(index, sub).unwrap_or(0) as u8
    }

    fn sdo_read_i8(&self, index: u16, sub: u8) -> i8 {
        self.pdo_map.read_u32(index, sub).unwrap_or(0) as i8
    }

    fn sdo_read_i16(&self, index: u16, sub: u8) -> i16 {
        self.pdo_map.read_u16(index, sub).unwrap_or(0) as i16
    }

    fn sdo_write_u16(&self, index: u16, sub: u8, val: u16) {
        let _ = self.pdo_map.write_u16(index, sub, val);
    }

    fn sdo_write_u32(&self, index: u16, sub: u8, val: u32) {
        let _ = self.pdo_map.write_u32(index, sub, val);
    }

    fn sdo_write_i32(&self, index: u16, sub: u8, val: i32) {
        let _ = self.pdo_map.write_u32(index, sub, val as u32);
    }

    fn sdo_write_u8(&self, index: u16, sub: u8, val: u8) {

        let bytes = [val];
        unsafe {
            ffi::SDOwrite_raw(self.master_index, self.slave_index, index, sub,
                              0, bytes.as_ptr(), 1);
        }
    }

    fn sdo_write_i8(&self, index: u16, sub: u8, val: i8) {
        self.sdo_write_u8(index, sub, val as u8);
    }

    fn sdo_write_i16(&self, index: u16, sub: u8, val: i16) {
        self.sdo_write_u16(index, sub, val as u16);
    }

    pub fn statusword(&self) -> u16 {
        self.sdo_read_u16(OD_STATUSWORD, 0)
    }

    pub fn controlword(&self) -> u16 {
        self.sdo_read_u16(OD_CONTROLWORD, 0)
    }

    pub fn set_controlword(&self, value: u16) {
        self.sdo_write_u16(OD_CONTROLWORD, 0, value);
    }

    pub fn state_drive(&self) -> StateCiA402 {
        StateCiA402::from_statusword(self.statusword())
    }

    pub fn target_reached(&self) -> bool {
        (self.statusword() & SW_TARGET_REACHED) != 0
    }

    pub fn has_fault(&self) -> bool {
        (self.statusword() & SW_FAULT) != 0
    }

    pub fn has_warning(&self) -> bool {
        (self.statusword() & SW_WARNING) != 0
    }

    pub fn is_remote(&self) -> bool {
        (self.statusword() & SW_REMOTE) != 0
    }

    pub fn operation_mode(&self) -> i8 {
        self.sdo_read_i8(OD_MODES_OF_OPERATION_DISPLAY, 0)
    }

    pub fn set_operation_mode(&self, mode: ModeCiA402) {
        self.sdo_write_i8(OD_MODES_OF_OPERATION, 0, mode as i8);
    }

    pub fn enable(&self, max_retries: i32) -> bool {

        unsafe { ffi::CiA402_Enable(self.master_index, self.slave_index, max_retries) != 0 }
    }

    pub fn enable_default(&self) -> bool {
        self.enable(10)
    }

    pub fn disable_operation(&self) {
        self.set_controlword(CW_SWITCH_ON);
    }

    pub fn disable(&self) {
        self.set_controlword(CW_DISABLE_VOLTAGE);
    }

    pub fn quick_stop(&self) {
        self.set_controlword(CW_QUICK_STOP);
    }

    pub fn fault_reset(&self) {

        self.set_controlword(0x00);

        std::thread::sleep(std::time::Duration::from_millis(1));

        self.set_controlword(CW_FAULT_RESET);
    }

    pub fn position_actual(&self) -> i32 {
        self.sdo_read_i32(OD_POSITION_ACTUAL, 0)
    }

    pub fn velocity_actual(&self) -> i32 {
        self.sdo_read_i32(OD_VELOCITY_ACTUAL, 0)
    }

    pub fn torque_actual(&self) -> i16 {
        self.sdo_read_i16(OD_TORQUE_ACTUAL, 0)
    }

    pub fn set_target_position(&self, value: i32) {
        self.sdo_write_i32(OD_TARGET_POSITION, 0, value);
    }

    pub fn target_position(&self) -> i32 {
        self.sdo_read_i32(OD_TARGET_POSITION, 0)
    }

    pub fn set_target_velocity(&self, value: i32) {
        self.sdo_write_i32(OD_TARGET_VELOCITY, 0, value);
    }

    pub fn target_velocity(&self) -> i32 {
        self.sdo_read_i32(OD_TARGET_VELOCITY, 0)
    }

    pub fn set_target_torque(&self, value: i16) {
        self.sdo_write_i16(OD_TARGET_TORQUE, 0, value);
    }

    pub fn target_torque(&self) -> i16 {
        self.sdo_read_i16(OD_TARGET_TORQUE, 0)
    }

    pub fn profile_velocity(&self) -> u32 {
        self.sdo_read_u32(OD_PROFILE_VELOCITY, 0)
    }

    pub fn set_profile_velocity(&self, value: u32) {
        self.sdo_write_u32(OD_PROFILE_VELOCITY, 0, value);
    }

    pub fn profile_acceleration(&self) -> u32 {
        self.sdo_read_u32(OD_PROFILE_ACCELERATION, 0)
    }

    pub fn set_profile_acceleration(&self, value: u32) {
        self.sdo_write_u32(OD_PROFILE_ACCELERATION, 0, value);
    }

    pub fn profile_deceleration(&self) -> u32 {
        self.sdo_read_u32(OD_PROFILE_DECELERATION, 0)
    }

    pub fn set_profile_deceleration(&self, value: u32) {
        self.sdo_write_u32(OD_PROFILE_DECELERATION, 0, value);
    }

    pub fn quick_stop_deceleration(&self) -> u32 {
        self.sdo_read_u32(OD_QUICK_STOP_DECELERATION, 0)
    }

    pub fn set_quick_stop_deceleration(&self, value: u32) {
        self.sdo_write_u32(OD_QUICK_STOP_DECELERATION, 0, value);
    }

    pub fn polarity(&self) -> u8 {
        self.sdo_read_u8(OD_POLARITY, 0)
    }

    pub fn set_polarity(&self, value: u8) {
        self.sdo_write_u8(OD_POLARITY, 0, value);
    }

    pub fn motion_profile_type(&self) -> i16 {
        self.sdo_read_i16(OD_MOTION_PROFILE_TYPE, 0)
    }

    pub fn set_motion_profile_type(&self, value: i16) {
        self.sdo_write_i16(OD_MOTION_PROFILE_TYPE, 0, value);
    }

    pub fn software_position_limit_min(&self) -> i32 {
        self.sdo_read_i32(OD_SOFTWARE_POSITION_LIMIT, 1)
    }

    pub fn set_software_position_limit_min(&self, value: i32) {
        self.sdo_write_i32(OD_SOFTWARE_POSITION_LIMIT, 1, value);
    }

    pub fn software_position_limit_max(&self) -> i32 {
        self.sdo_read_i32(OD_SOFTWARE_POSITION_LIMIT, 2)
    }

    pub fn set_software_position_limit_max(&self, value: i32) {
        self.sdo_write_i32(OD_SOFTWARE_POSITION_LIMIT, 2, value);
    }

    pub fn home_offset(&self) -> i32 {
        self.sdo_read_i32(OD_HOME_OFFSET, 0)
    }

    pub fn set_home_offset(&self, value: i32) {
        self.sdo_write_i32(OD_HOME_OFFSET, 0, value);
    }

    pub fn homing_speed_search(&self) -> u32 {
        self.sdo_read_u32(OD_HOMING_SPEEDS, 1)
    }

    pub fn set_homing_speed_search(&self, value: u32) {
        self.sdo_write_u32(OD_HOMING_SPEEDS, 1, value);
    }

    pub fn homing_speed_zero(&self) -> u32 {
        self.sdo_read_u32(OD_HOMING_SPEEDS, 2)
    }

    pub fn set_homing_speed_zero(&self, value: u32) {
        self.sdo_write_u32(OD_HOMING_SPEEDS, 2, value);
    }

    pub fn homing_acceleration(&self) -> u32 {
        self.sdo_read_u32(OD_HOMING_ACCELERATION, 0)
    }

    pub fn set_homing_acceleration(&self, value: u32) {
        self.sdo_write_u32(OD_HOMING_ACCELERATION, 0, value);
    }

    pub fn configure_touch_probe(&self, function: u16) {
        self.sdo_write_u16(OD_TOUCH_PROBE_FUNCTION, 0, function);
    }

    pub fn touch_probe_status(&self) -> u16 {
        self.sdo_read_u16(OD_TOUCH_PROBE_STATUS, 0)
    }

    pub fn touch_probe_positive_edge(&self) -> i32 {
        self.sdo_read_i32(OD_TOUCH_PROBE_POS_EDGE, 0)
    }

    pub fn touch_probe_negative_edge(&self) -> i32 {
        self.sdo_read_i32(OD_TOUCH_PROBE_NEG_EDGE, 0)
    }

    pub fn digital_inputs(&self) -> u32 {
        self.sdo_read_u32(OD_DIGITAL_INPUTS, 0)
    }

    pub fn digital_outputs(&self) -> u32 {
        self.sdo_read_u32(OD_DIGITAL_OUTPUTS, 1)
    }

    pub fn set_digital_outputs(&self, value: u32) {
        self.sdo_write_u32(OD_DIGITAL_OUTPUTS, 1, value);
    }

    pub fn max_torque(&self) -> u16 {
        self.sdo_read_u16(OD_MAX_TORQUE, 0)
    }

    pub fn set_max_torque(&self, value: u16) {
        self.sdo_write_u16(OD_MAX_TORQUE, 0, value);
    }

    pub fn motor_rated_torque(&self) -> u32 {
        self.sdo_read_u32(OD_MOTOR_RATED_TORQUE, 0)
    }

    pub fn set_motor_rated_torque(&self, value: u32) {
        self.sdo_write_u32(OD_MOTOR_RATED_TORQUE, 0, value);
    }

    pub fn position_offset(&self) -> i32 {
        self.sdo_read_i32(OD_POSITION_OFFSET, 0)
    }

    pub fn set_position_offset(&self, value: i32) {
        self.sdo_write_i32(OD_POSITION_OFFSET, 0, value);
    }

    pub fn velocity_offset(&self) -> i32 {
        self.sdo_read_i32(OD_VELOCITY_OFFSET, 0)
    }

    pub fn set_velocity_offset(&self, value: i32) {
        self.sdo_write_i32(OD_VELOCITY_OFFSET, 0, value);
    }

    pub fn torque_offset(&self) -> i16 {
        self.sdo_read_i16(OD_TORQUE_OFFSET, 0)
    }

    pub fn set_torque_offset(&self, value: i16) {
        self.sdo_write_i16(OD_TORQUE_OFFSET, 0, value);
    }

    pub fn interpolation_time_period_value(&self) -> u8 {
        self.sdo_read_u8(OD_INTERPOLATION_TIME_PERIOD, 1)
    }

    pub fn set_interpolation_time_period_value(&self, value: u8) {
        self.sdo_write_u8(OD_INTERPOLATION_TIME_PERIOD, 1, value);
    }

    pub fn interpolation_time_period_index(&self) -> i8 {
        self.sdo_read_i8(OD_INTERPOLATION_TIME_PERIOD, 2)
    }

    pub fn set_interpolation_time_period_index(&self, value: i8) {
        self.sdo_write_i8(OD_INTERPOLATION_TIME_PERIOD, 2, value);
    }

    pub fn quick_stop_option_code(&self) -> i16 {
        self.sdo_read_i16(OD_QUICK_STOP_OPTION_CODE, 0)
    }

    pub fn set_quick_stop_option_code(&self, value: i16) {
        self.sdo_write_i16(OD_QUICK_STOP_OPTION_CODE, 0, value);
    }

    pub fn positive_torque_limit(&self) -> u16 {
        self.sdo_read_u16(OD_POSITIVE_TORQUE_LIMIT, 0)
    }

    pub fn set_positive_torque_limit(&self, value: u16) {
        self.sdo_write_u16(OD_POSITIVE_TORQUE_LIMIT, 0, value);
    }

    pub fn negative_torque_limit(&self) -> u16 {
        self.sdo_read_u16(OD_NEGATIVE_TORQUE_LIMIT, 0)
    }

    pub fn set_negative_torque_limit(&self, value: u16) {
        self.sdo_write_u16(OD_NEGATIVE_TORQUE_LIMIT, 0, value);
    }

    pub fn supported_homing_methods(&self) -> Vec<i8> {
        let count = self.sdo_read_u8(OD_SUPPORTED_HOMING_METHODS, 0);
        let mut methods = Vec::new();
        for i in 1..=count {
            methods.push(self.sdo_read_i8(OD_SUPPORTED_HOMING_METHODS, i));
        }
        methods
    }

    pub fn txpdo_data_invalid(&self) -> bool {
        self.sdo_read_u8(OD_TXPDO_DATA_INVALID, 0) != 0
    }

    pub fn synchronization_settings(&self) -> u16 {
        self.sdo_read_u16(OD_SYNCHRONIZATION_SETTINGS, 1)
    }

    pub fn set_synchronization_settings(&self, value: u16) {
        self.sdo_write_u16(OD_SYNCHRONIZATION_SETTINGS, 1, value);
    }

    pub fn drive_sync_status(&self) -> u16 {
        self.sdo_read_u16(OD_DRIVE_SYNC_STATUS, 0)
    }

    pub fn new_setpoint(&self, position: i32, relative: bool) {
        self.set_target_position(position);
        let mut cw = CW_ENABLE_OPERATION | 0x10;
        if relative { cw |= 0x40; }
        self.set_controlword(cw);
    }

    pub fn clear_new_setpoint(&self) {
        self.set_controlword(CW_ENABLE_OPERATION);
    }

    pub fn start_homing(&self) {
        self.set_controlword(CW_ENABLE_OPERATION | 0x10);
    }

    pub fn homing_attained(&self) -> bool {
        (self.statusword() & SW_OP_MODE_SPECIFIC_1) != 0
    }

    pub fn homing_error(&self) -> bool {
        (self.statusword() & SW_OP_MODE_SPECIFIC_2) != 0
    }

    pub fn validate_profile_parameters(&self, configured_mode: Option<i8>) -> Vec<String> {
        let mut warnings = Vec::new();

        let mode = configured_mode.unwrap_or_else(|| self.sdo_read_i8(OD_MODES_OF_OPERATION, 0));

        if mode != 1 && mode != 3 {
            return warnings;
        }

        let mode_name = if mode == 1 { "PP" } else { "PV" };
        let checks: [(u16, &str); 3] = [
            (OD_MAX_PROFILE_VELOCITY, "Max Profile Velocity (0x6080)"),
            (OD_PROFILE_ACCELERATION, "Profile Acceleration (0x6083)"),
            (OD_PROFILE_DECELERATION, "Profile Deceleration (0x6084)"),
        ];

        for (index, name) in &checks {
            let val = self.sdo_read_u32(*index, 0);
            if val == 0 {
                warnings.push(format!(
                    "{} 模式: {} = 0, 驱动器无法生成运动曲线, 请在 Startup 中配置此参数",
                    mode_name, name
                ));
            }
        }

        warnings
    }

    fn try_read_attribute<T: Cia402Readable>(&self, index: u16, subindex: u8)
        -> (T, Cia402ReadStatus, u32)
    {
        let mut size: c_int = 0;
        let ptr = unsafe {
            ffi::SDOread(self.master_index, self.slave_index, index, subindex, 0, &mut size)
        };
        if !ptr.is_null() && size > 0 {

            let data = unsafe { std::slice::from_raw_parts(ptr, size as usize).to_vec() };
            unsafe { ffi::FreeMemory(ptr as *mut _) };
            return (T::from_le_slice(&data), Cia402ReadStatus::Success, 0);
        }

        if !ptr.is_null() { unsafe { ffi::FreeMemory(ptr as *mut _) }; }
        let abort_code = if size < 0 { (-size) as u32 } else { 0 };
        let status = if abort_code != 0 {
            Cia402ReadStatus::classify_abort(abort_code)
        } else {
            Cia402ReadStatus::ReadFailed
        };
        (T::default(), status, abort_code)
    }

    pub fn try_read_object<T: Cia402Readable>(&self, index: u16, subindex: u8)
        -> Cia402ReadResult<T>
    {
        let (value, status, abort) = self.try_read_attribute::<T>(index, subindex);
        Cia402ReadResult::new(value, status, abort)
    }

    pub fn try_read_error_code(&self) -> Cia402ReadResult<u16> {
        self.try_read_object::<u16>(OD_ERROR_CODE, 0)
    }

    pub fn try_read_supported_drive_modes(&self) -> Cia402ReadResult<u32> {
        self.try_read_object::<u32>(OD_SUPPORTED_DRIVE_MODES, 0)
    }

    pub fn try_read_digital_inputs(&self) -> Cia402ReadResult<u32> {
        self.try_read_object::<u32>(OD_DIGITAL_INPUTS, 0)
    }

    pub fn try_read_operation_mode_display(&self) -> Cia402ReadResult<i8> {
        self.try_read_object::<i8>(OD_MODES_OF_OPERATION_DISPLAY, 0)
    }

    pub fn try_read_drive_data(&self, subindex: u8) -> Cia402ReadResult<u32> {
        self.try_read_object::<u32>(OD_DRIVE_DATA, subindex)
    }

    pub fn try_read_statusword(&self) -> Cia402ReadResult<u16> {
        self.try_read_object::<u16>(OD_STATUSWORD, 0)
    }

    pub fn try_read_position_actual(&self) -> Cia402ReadResult<i32> {
        self.try_read_object::<i32>(OD_POSITION_ACTUAL, 0)
    }

    pub fn try_read_torque_actual(&self) -> Cia402ReadResult<i16> {
        self.try_read_object::<i16>(OD_TORQUE_ACTUAL, 0)
    }

    pub fn supported_drive_modes_readable(&self) -> bool {
        self.try_read_supported_drive_modes().ok()
    }

    pub fn transition_command(current: StateCiA402, target: StateCiA402) -> u16 {
        unsafe { ffi::CiA402_GetTransitionCommand(current as c_int, target as c_int) }
    }

    pub fn request_transition(&self, target: StateCiA402) -> bool {
        unsafe {
            ffi::CiA402_RequestTransition(self.master_index, self.slave_index, target as c_int) != 0
        }
    }

    pub fn fault_reset_step(&self, step: i32) -> bool {
        unsafe {
            ffi::CiA402_FaultResetStep(self.master_index, self.slave_index, step) != 0
        }
    }

    pub fn supported_drive_modes(&self) -> u32 {
        unsafe { ffi::CiA402_GetSupportedDriveModes(self.master_index, self.slave_index) }
    }

    pub fn is_mode_supported(&self, mode: ModeCiA402) -> bool {
        unsafe {
            ffi::CiA402_IsModeSupported(self.master_index, self.slave_index, mode as c_int) != 0
        }
    }

    pub fn drive_error_code(&self) -> u16 {
        unsafe { ffi::CiA402_GetErrorCode(self.master_index, self.slave_index) }
    }

    pub fn error_history(&self) -> Vec<u32> {
        const MAX: usize = 64;
        let mut buf = [0u32; MAX];
        let n = unsafe {
            ffi::CiA402_GetErrorHistory(
                self.master_index, self.slave_index, buf.as_mut_ptr(), MAX as c_int,
            )
        };
        if n <= 0 { return Vec::new(); }
        buf[..(n as usize).min(MAX)].to_vec()
    }

    pub fn clear_error_history(&self) -> bool {
        unsafe { ffi::CiA402_ClearErrorHistory(self.master_index, self.slave_index) != 0 }
    }

    pub fn set_homing_method(&self, method: i8) -> bool {
        unsafe { ffi::CiA402_SetHomingMethod(self.master_index, self.slave_index, method) != 0 }
    }

    pub fn homing_method(&self) -> i8 {
        unsafe { ffi::CiA402_GetHomingMethod(self.master_index, self.slave_index) }
    }

    pub fn supported_homing_methods_native(&self) -> Vec<i8> {
        const MAX: usize = 64;
        let mut buf = [0i8; MAX];
        let n = unsafe {
            ffi::CiA402_GetSupportedHomingMethods(
                self.master_index, self.slave_index, buf.as_mut_ptr(), MAX as c_int,
            )
        };
        if n <= 0 { return Vec::new(); }
        buf[..(n as usize).min(MAX)].to_vec()
    }

    pub fn is_standard_homing_method(method: i8) -> bool {
        unsafe { ffi::CiA402_IsStandardHomingMethod(method) != 0 }
    }

    pub fn quick_stop_option_native(&self) -> i16 {
        unsafe { ffi::CiA402_GetQuickStopOption(self.master_index, self.slave_index) }
    }

    pub fn set_quick_stop_option_native(&self, value: i16) -> bool {
        unsafe { ffi::CiA402_SetQuickStopOption(self.master_index, self.slave_index, value) != 0 }
    }

    pub fn shutdown_option(&self) -> i16 {
        unsafe { ffi::CiA402_GetShutdownOption(self.master_index, self.slave_index) }
    }

    pub fn set_shutdown_option(&self, value: i16) -> bool {
        unsafe { ffi::CiA402_SetShutdownOption(self.master_index, self.slave_index, value) != 0 }
    }

    pub fn disable_operation_option(&self) -> i16 {
        unsafe { ffi::CiA402_GetDisableOperationOption(self.master_index, self.slave_index) }
    }

    pub fn set_disable_operation_option(&self, value: i16) -> bool {
        unsafe {
            ffi::CiA402_SetDisableOperationOption(self.master_index, self.slave_index, value) != 0
        }
    }

    pub fn halt_option(&self) -> i16 {
        unsafe { ffi::CiA402_GetHaltOption(self.master_index, self.slave_index) }
    }

    pub fn set_halt_option(&self, value: i16) -> bool {
        unsafe { ffi::CiA402_SetHaltOption(self.master_index, self.slave_index, value) != 0 }
    }

    pub fn fault_reaction_option(&self) -> i16 {
        unsafe { ffi::CiA402_GetFaultReactionOption(self.master_index, self.slave_index) }
    }

    pub fn set_fault_reaction_option(&self, value: i16) -> bool {
        unsafe {
            ffi::CiA402_SetFaultReactionOption(self.master_index, self.slave_index, value) != 0
        }
    }
}

impl std::fmt::Display for CiA402Instance {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let sw = self.statusword();
        let state = StateCiA402::from_statusword(sw);
        let mode = self.operation_mode();
        write!(f, "从站 {}: CiA402 [{}, 模式={}, SW=0x{:04X}]",
            self.slave_index, state, crate::statics::print::cia402_mode_description(mode), sw)
    }
}