PUSICANControl.cs
39.9 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TSA_V.Common;
namespace PUSICANLibrary
{
public class PUSICANControl
{
#region 方法定义
public delegate PUSIResult EventCall_T(CB_TYPE callbackType, IntPtr pData);
[DllImport("CanOpen.dll", EntryPoint = "PUSICO_Close", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
private static extern PUSIResult PUSICO_Close();
[DllImport("CanOpen.dll", EntryPoint = "PUSICO_AddNode", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern PUSIResult PUSICO_AddNode(uint nodeid);
[DllImport("CanOpen.dll", EntryPoint = "PUSICO_RemoveNode", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern PUSIResult PUSICO_RemoveNode(uint nodeid);
[DllImport("CanOpen.dll", EntryPoint = "PUSICO_SetEventCallback", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern PUSIResult PUSICO_SetEventCallback(EventCall_T callback);
[DllImport("CanOpen.dll", EntryPoint = "PUSICO_GetNodeStatus", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern PUSIResult PUSICO_GetNodeStatus(uint nodeid, ref SLAVE_STATUS status);
[DllImport("CanOpen.dll", EntryPoint = "PUSICO_ReadSDO", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern PUSIResult PUSICO_ReadSDO(uint nodeid, uint index, uint subindex,
byte[] data, DATA_TYPE datatype, ref uint recvlen, uint timeout);
[DllImport("CanOpen.dll", EntryPoint = "PUSICO_WriteSDO", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern PUSIResult PUSICO_WriteSDO(uint nodeid, uint index, uint subindex,
byte[] data, DATA_TYPE datatype, uint sendlen, uint timeout);
[DllImport("CanOpen.dll", EntryPoint = "PUSICO_SetDriver", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern PUSIResult PUSICO_SetDriver(string dllname, out IntPtr lib);
[DllImport("CanOpen.dll", EntryPoint = "PUSICO_Open", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern PUSIResult PUSICO_Open(string busname, string buadrate);
[DllImport("kernel32.dll")]
public extern static IntPtr GetProcAddress(IntPtr lib, string funcName);
[DllImport("CanOpen.dll", EntryPoint = "PUSICO_SetNodeState", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern PUSIResult PUSICO_SetNodeState(uint nodeid, SLAVE_MANAGEMENT slaveManagement);
#endregion
private static Dictionary<uint, Dictionary<uint, int>> sdoLastData = new Dictionary<uint, Dictionary<uint, int>>();
#region 读写SDO
private static void UpdateSDOData(uint nodeId, uint addr, int value)
{
if (sdoLastData.ContainsKey(nodeId))
{
if (sdoLastData[nodeId].ContainsKey(addr))
{
sdoLastData[nodeId][addr] = value;
}
else
{
sdoLastData[nodeId].Add(addr, value);
}
}
else
{
Dictionary<uint, int> data = new Dictionary<uint, int>();
data.Add(addr, value);
sdoLastData.Add(nodeId, data);
}
}
private static int GetSDOData(uint nodeId, uint addr)
{
if (sdoLastData.ContainsKey(nodeId))
{
if (sdoLastData[nodeId].ContainsKey(addr))
{
return sdoLastData[nodeId][addr];
}
}
return -1;
}
public static PUSIResult WriteSDO(uint nodeId, uint sdoAddr, int value)
{
PUSIResult result = PUSIResult.RET_READSDO_FAIL;
try
{
if (!CAN_Address.SDOAddrMap().ContainsKey(sdoAddr))
{
return PUSIResult.RET_WRITESDO_FAIL;
}
SDOAddrInfo addr = CAN_Address.SDOAddrMap()[sdoAddr];
byte[] data = new byte[10];
for (int i = 0; i < 10; i++)
{
data[i] = 0;
}
//相对运动
data = BitConverter.GetBytes(value);
result = PUSICANControl.PUSICO_WriteSDO(nodeId, addr.sdoAddr, addr.subIndex, data, addr.dataType, addr.dataLength, addr.timeOut);
if (PUSIResult.RET_SUCCESS == result)
{
Console.WriteLine("write "+ nodeId + " CAN_Address." + sdoAddr + " sdo ok.");
UpdateSDOData(nodeId, sdoAddr, value);
}
}
catch (Exception ex)
{
LogUtil.error("write "+ nodeId + " CAN_Address." + sdoAddr + " error ." + ex.ToString());
}
return result;
}
public static PUSIResult ReadSDO(uint nodeId, uint sdoAddr, out int outValue)
{
outValue = 0;
PUSIResult result = PUSIResult.RET_READSDO_FAIL;
try
{
if (!CAN_Address.SDOAddrMap().ContainsKey(sdoAddr))
{
return PUSIResult.RET_WRITESDO_FAIL;
}
SDOAddrInfo addr = CAN_Address.SDOAddrMap()[sdoAddr];
byte[] data = new byte[10];
for (int i = 0; i < 10; i++)
{
data[i] = 0;
}
//相对运动
uint length = addr.dataLength;
result = PUSICANControl.PUSICO_ReadSDO(nodeId, addr.sdoAddr, addr.subIndex, data, addr.dataType, ref length, addr.timeOut);
if (PUSIResult.RET_SUCCESS == result)
{
outValue = BitConverter.ToInt32(data, 0);
UpdateSDOData(nodeId, sdoAddr, outValue);
Console.WriteLine("read " + addr.sdoAddr + " sdo ok." + outValue);
}
}
catch (Exception ex)
{
LogUtil.error("read " + nodeId + " CAN_Address." + sdoAddr + " error ." + ex.ToString());
}
return result;
}
#endregion
public delegate void SetTcpClient_drvier(string ip, int nPort);
public delegate void SetSerial_drvier(int nPort, int nBaud);
public static bool ISOpen = false;
public static string Open()
{
if (ISOpen)
{
return "";
}
int adapterIndex = 4;
int baudIndex = 4;
string Ip = "192.168.0.178";
int port = 4001;
Ip = ConfigAppSettings.GetValue(Setting_Init.PUSICAN_Ip);
port = ConfigAppSettings.GetIntValue(Setting_Init.PUSICAN_Port);
return Open(adapterIndex, baudIndex, Ip, port);
}
public static void Close()
{
try
{
if (ISOpen)
{
PUSICO_Close();
ISOpen = false;
}
}
catch (Exception ex)
{
LogUtil.error("Close出错:" + ex.ToString());
}
}
public static string Open(int adapterIndex, int baudIndex, string Ip, int port)
{
try
{
string busname = "1";
string[] strDriver = { "can_lanma.dll", "can_ledia.dll", "can_yl9100.dll","can_ixxat.dll",
"can_gc.dll","can_zlg.dll","can_zlg.dll","can_zlg.dll","can_cx.dll","can_rs232.dll"};
string[] baudlist = { "10K", "20K", "50K", "100K", "125K", "250K", "500K", "800K", "1M" };
switch (adapterIndex) //周立功USBCAN-E-U
{
case 6:
busname = "3";
break;
case 7:
busname = "4";
break;
case 8:
busname = "2";
break;
}
IntPtr lib = new IntPtr();
if (PUSIResult.RET_SUCCESS == PUSICANControl.PUSICO_SetDriver(strDriver[adapterIndex], out lib))
{
Console.WriteLine("加载驱动成功.");
if (lib != null)
{
IntPtr api = PUSICANControl.GetProcAddress(lib, "canSetTcpClient_driver");
if (api != IntPtr.Zero)
{
SetTcpClient_drvier SetTcpClient = (SetTcpClient_drvier)Marshal.GetDelegateForFunctionPointer(api, typeof(SetTcpClient_drvier));
SetTcpClient(Ip, port);
}
IntPtr api1 = PUSICANControl.GetProcAddress(lib, "canSetSerial_driver");
if (api1 != IntPtr.Zero)
{
SetSerial_drvier SetSerial = (SetSerial_drvier)Marshal.GetDelegateForFunctionPointer(api1, typeof(SetSerial_drvier));
SetSerial(1, 115200); //端口,波特率
}
}
}
else
{
Console.WriteLine("加载驱动失败!");
ISOpen = false;
return "加载驱动失败!";
}
if (PUSIResult.RET_SUCCESS == PUSICANControl.PUSICO_Open(busname, baudlist[baudIndex]))
{
ISOpen = true;
Console.WriteLine("打开端口成功.");
}
else
{
Console.WriteLine("打开端口失败!");
ISOpen = false;
return "打开端口失败!";
}
}
catch (Exception ex)
{
LogUtil.error("Open出错:" + ex.ToString());
}
return "";
}
/// <summary>
/// 初始化节点
/// </summary>
public static bool InitNode(uint nodeid)
{
//添加节点
PUSIResult result = PUSICANControl.PUSICO_AddNode(Convert.ToUInt32(nodeid));
//启动节点
result = PUSICANControl.PUSICO_SetNodeState(nodeid, SLAVE_MANAGEMENT.SLAVE_START);
return SetOutStopType(nodeid);
}
public static bool InitRNodeConfig(uint nodeId, bool isLineSlv = false)
{
WriteOutStop(nodeId);
int addSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_AddSpeed, nodeId.ToString());
int delSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_DelSpeed, nodeId.ToString());
int maxPhase = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_MaxPhaseCurrent, nodeId.ToString());
int startSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_HomeStartSpeed, nodeId.ToString());
int stopSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_HomeStopSpeed, nodeId.ToString());
int maxSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_MaxSpeed, nodeId.ToString());
int microValue = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_MicroStepping, nodeId.ToString());
int delValue = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_DelPhaseCurrent, nodeId.ToString());
if (isLineSlv)
{
if (ConfigAppSettings.GetIntValue(Setting_Init.Line_HomeStartSpeed, nodeId.ToString()) != 0)
{
startSpeed = ConfigAppSettings.GetIntValue(Setting_Init.Line_HomeStartSpeed, nodeId.ToString());
stopSpeed = ConfigAppSettings.GetIntValue(Setting_Init.Line_HomeStopSpeed, nodeId.ToString());
maxSpeed = ConfigAppSettings.GetIntValue(Setting_Init.Line_MaxSpeed, nodeId.ToString());
}
if (ConfigAppSettings.GetIntValue(Setting_Init.Line_MaxPhaseCurrent, nodeId.ToString()) > 0)
{
addSpeed = ConfigAppSettings.GetIntValue(Setting_Init.Line_AddSpeed, nodeId.ToString());
delSpeed = ConfigAppSettings.GetIntValue(Setting_Init.Line_DelSpeed, nodeId.ToString());
maxPhase = ConfigAppSettings.GetIntValue(Setting_Init.Line_MaxPhaseCurrent, nodeId.ToString());
microValue = ConfigAppSettings.GetIntValue(Setting_Init.Line_MicroStepping, nodeId.ToString());
delValue = ConfigAppSettings.GetIntValue(Setting_Init.Line_DelPhaseCurrent, nodeId.ToString());
}
}
LogUtil.info("InitRNodeConfig NodeId【" + nodeId+ "】isLineSlv【"+ isLineSlv + "】SetAddSpeed【" + addSpeed + "】SetDelSpeed【" + delSpeed + "】SetStartSpeed【" + startSpeed
+ "】SetStopSpeed【" + stopSpeed + "】SetMaxSpeed【" + maxSpeed + "】SetMicroStepping【" + microValue + "】SetDelPhaseCurrent【" + delValue + "】SetMaxPhaseCurrent【" + maxPhase + "】");
PUSICANControl.SetAddSpeed(nodeId, addSpeed);
PUSICANControl.SetDelSpeed(nodeId, delSpeed);
PUSICANControl.SetStartSpeed(nodeId, startSpeed);
PUSICANControl.SetStopSpeed(nodeId, stopSpeed);
PUSICANControl.SetMaxSpeed(nodeId, maxSpeed);
PUSICANControl.SetMicroStepping(nodeId, microValue);
PUSICANControl.SetDelPhaseCurrent(nodeId, delValue);
PUSICANControl.SetMaxPhaseCurrent(nodeId, maxPhase);
if (isLineSlv)
{
for (int i = 1; i <= 3; i++)
{
int value = PUSICANControl.GetMaxPhaseCurrent(nodeId);
if (value.Equals(maxPhase))
{
LogUtil.info("InitRNodeConfig NodeId【" + nodeId + "】isLineSlv【" + isLineSlv + "】GetMaxPhaseCurrent "+i+"= " + value);
break;
}
else
{
LogUtil.info("InitRNodeConfig NodeId【" + nodeId + "】isLineSlv【" + isLineSlv + "】GetMaxPhaseCurrent " + i + "= " + value+ ",重新设置 SetMaxPhaseCurrent ="+maxPhase);
PUSICANControl.SetMaxPhaseCurrent(nodeId, maxPhase);
Thread.Sleep(10);
}
}
}
//清理驱动器状态
PUSIResult result = PUSICANControl.WriteSDO(nodeId, CAN_Address.ControlStatus, 1);
return true;
}
/// <summary>
/// 写外部停止信号,默认所有轴的停止1停止2都启动
/// </summary>
/// <param name="nodeid"></param>
/// <returns></returns>
public static bool WriteOutStop(uint nodeid)
{
PUSIResult result = PUSIResult.RET_SUCCESS;
uint sdoAddr = CAN_Address.OutStop;
if (!CAN_Address.SDOAddrMap().ContainsKey(sdoAddr))
{
return false;
}
SDOAddrInfo addr = CAN_Address.SDOAddrMap()[sdoAddr];
int value = 3;
try
{
byte[] data = new byte[10];
for (int i = 0; i < 10; i++)
{
data[i] = 0;
}
//相对运动
data = BitConverter.GetBytes(value);
result = PUSICANControl.PUSICO_WriteSDO(nodeid, addr.sdoAddr, 0x01, data, addr.dataType, addr.dataLength, addr.timeOut);
Thread.Sleep(100);
if (PUSIResult.RET_SUCCESS == result)
{
Console.WriteLine("write "+ nodeid + " CAN_Address.OutStop sdo ok.");
}
data = new byte[10];
value = 0;
for (int i = 0; i < 10; i++)
{
data[i] = 0;
}
//相对运动
data = BitConverter.GetBytes(value);
result = PUSICANControl.PUSICO_WriteSDO(nodeid, addr.sdoAddr, 0x02, data, addr.dataType, addr.dataLength, addr.timeOut);
Thread.Sleep(100);
if (PUSIResult.RET_SUCCESS == result)
{
Console.WriteLine("write "+ nodeid + " CAN_Address.OutStop sdo ok.");
}
}
catch (Exception ex)
{
LogUtil.error("write "+ nodeid + " CAN_Address.OutStop error:" + ex.ToString());
}
return true ;
}
public static bool SetOutStopType(uint nodeid)
{
PUSIResult result = PUSICANControl.WriteSDO(nodeid, CAN_Address.OutStop_ParamCount, 2);
result = PUSICANControl.WriteSDO(nodeid, CAN_Address.OutStop_Set, 3);
result = PUSICANControl.WriteSDO(nodeid, CAN_Address.OutStop_Type, 0);
return CheckWriteResult(result, "CAN_Address.SetOutStopType");
}
public static EventCall_T evencb = new EventCall_T(EventCall);
public static PUSIResult EventCall(CB_TYPE callbackType, IntPtr pData)
{
switch (callbackType)
{
case CB_TYPE.CBT_ConsoleWriteLine:
{
/*string strConsole.WriteLine = Marshal.PtrToStringAnsi(pData);
if (mainform != null)
mainform.Console.WriteLine(strConsole.WriteLine);*/
}
break;
case CB_TYPE.CBT_SLAVE_STATUS_CHANGE:
break;
case CB_TYPE.CBT_PDO_DATA:
break;
default:
break;
}
return PUSIResult.RET_SUCCESS;
}
public static int GetPosition(uint nodeid)
{
int value;
PUSIResult result = PUSICANControl.ReadSDO(nodeid, CAN_Address.CurrPosition, out value);
if (!PUSIResult.RET_SUCCESS.Equals(result))
{
LogUtil.error( " node ["+ nodeid + "] GetPosition失败,结果:" + result);
}
return value;
}
public static int GetMaxSpeed(uint nodeid)
{
int value;
PUSIResult result = PUSICANControl.ReadSDO(nodeid, CAN_Address.MaxSpeed, out value);
if (!PUSIResult.RET_SUCCESS.Equals(result))
{
LogUtil.error(" node [" + nodeid + "] GetMaxSpeed失败,结果:" + result);
}
return value;
}
public static int GetAddSpeed(uint nodeid)
{
int value;
PUSIResult result = PUSICANControl.ReadSDO(nodeid, CAN_Address.AddSpeed, out value);
if (!PUSIResult.RET_SUCCESS.Equals(result))
{
LogUtil.error(" node [" + nodeid + "] GetAddSpeed失败,结果:" + result);
}
return value;
}
public static int GetDelSpeed(uint nodeid)
{
int value;
PUSIResult result = PUSICANControl.ReadSDO(nodeid, CAN_Address.DelSpeed, out value);
if (!PUSIResult.RET_SUCCESS.Equals(result))
{
LogUtil.error(" node [" + nodeid + "] GetDelSpeed失败,结果:" + result);
}
return value;
}
public static int GetStartSpeed(uint nodeid)
{
int value;
PUSIResult result = PUSICANControl.ReadSDO(nodeid, CAN_Address.StartSpeed, out value);
if (!PUSIResult.RET_SUCCESS.Equals(result))
{
LogUtil.error(" node [" + nodeid + "] GetStartSpeed失败,结果:" + result);
}
return value;
}
public static int GetStopSpeed(uint nodeid)
{
int value;
PUSIResult result = PUSICANControl.ReadSDO(nodeid, CAN_Address.StopSpeed, out value);
if (!PUSIResult.RET_SUCCESS.Equals(result))
{
LogUtil.error(" node [" + nodeid + "] GetStopSpeed失败,结果:" + result);
}
return value;
}
/// <summary>
/// 获取细分数
/// </summary>
public static int GetMicroStepping(uint nodeid)
{
int value;
PUSIResult result = PUSICANControl.ReadSDO(nodeid, CAN_Address.MicroStepping, out value);
if (!PUSIResult.RET_SUCCESS.Equals(result))
{
LogUtil.error(" node [" + nodeid + "] GetMicroStepping失败,结果:" + result);
} return value;
}
/// <summary>
/// 获取峰值电流
/// </summary>
public static int GetMaxPhaseCurrent(uint nodeid)
{
int value;
PUSIResult result = PUSICANControl.ReadSDO(nodeid, CAN_Address.MaxPhaseCurrent, out value);
if (!PUSIResult.RET_SUCCESS.Equals(result))
{
LogUtil.error(" node [" + nodeid + "] GetMaxPhaseCurrent失败,结果:" + result);
} return value;
}
/// <summary>
/// 获取电流衰减
/// </summary>
public static int GetDelPhaseCurrent(uint nodeid)
{
int value;
PUSIResult result = PUSICANControl.ReadSDO(nodeid, CAN_Address.DelPhaseCurrent, out value);
if (!PUSIResult.RET_SUCCESS.Equals(result))
{
LogUtil.error(" node [" + nodeid + "] GetDelPhaseCurrent失败,结果:" + result);
} return value;
}
private static bool CheckWriteResult(PUSIResult result, string Addr)
{
if (result.Equals(PUSIResult.RET_SUCCESS))
{
return true;
}
else
{
LogUtil.error(" WriteSDO " + Addr + "失败,结果:" + result);
return false;
}
}
public static bool SetPosition(uint nodeid, int value)
{
PUSIResult result = PUSICANControl.WriteSDO(nodeid, CAN_Address.CurrPosition, value);
return CheckWriteResult(result, " node [" + nodeid + "] CAN_Address.CurrPosition");
}
public static bool SetMaxSpeed(uint nodeid, int value)
{
PUSIResult result = PUSICANControl.WriteSDO(nodeid, CAN_Address.MaxSpeed, value);
return CheckWriteResult(result, " node [" + nodeid + "] CAN_Address.MaxSpeed");
}
public static bool SetAddSpeed(uint nodeid, int value)
{
PUSIResult result = PUSICANControl.WriteSDO(nodeid, CAN_Address.AddSpeed, value);
return CheckWriteResult(result, " node [" + nodeid + "] CAN_Address.AddSpeed");
}
public static bool SetDelSpeed(uint nodeid, int value)
{
PUSIResult result = PUSICANControl.WriteSDO(nodeid, CAN_Address.DelSpeed, value);
return CheckWriteResult(result, " node [" + nodeid + "] CAN_Address.DelSpeed");
}
public static bool SetStartSpeed(uint nodeid, int value)
{
PUSIResult result = PUSICANControl.WriteSDO(nodeid, CAN_Address.StartSpeed, value);
return CheckWriteResult(result, " node [" + nodeid + "] CAN_Address.StartSpeed");
}
public static bool SetStopSpeed(uint nodeid, int value)
{
PUSIResult result = PUSICANControl.WriteSDO(nodeid, CAN_Address.StopSpeed, value);
return CheckWriteResult(result, " node [" + nodeid + "] CAN_Address.StopSpeed");
}
public static bool SetMicroStepping(uint nodeid, int value)
{
PUSIResult result = PUSICANControl.WriteSDO(nodeid, CAN_Address.MicroStepping, value);
return CheckWriteResult(result, " node [" + nodeid + "] CAN_Address.MicroStepping");
}
public static bool SetMaxPhaseCurrent(uint nodeid, int value)
{
PUSIResult result = PUSICANControl.WriteSDO(nodeid, CAN_Address.MaxPhaseCurrent, value);
return CheckWriteResult(result, " node [" + nodeid + "] CAN_Address.MaxPhaseCurrent");
}
public static bool SetDelPhaseCurrent(uint nodeid, int value)
{
PUSIResult result = PUSICANControl.WriteSDO(nodeid, CAN_Address.DelPhaseCurrent, value);
return CheckWriteResult(result, " node [" + nodeid + "] CAN_Address.DelPhaseCurrent");
}
public static bool AbsMove(uint nodeid, int position)
{
if (PUSICANControl.CheckStop2IsHigh(nodeid))
{
PUSICANControl.ClearStatus(nodeid);
Thread.Sleep(100);
}
//写模式
PUSIResult result;
//if (!(GetSDOData(nodeid, CAN_Address.MoveType).Equals(0)))
{
PUSICANControl.WriteSDO(nodeid, CAN_Address.MoveType, 0);
}
result = PUSICANControl.WriteSDO(nodeid, CAN_Address.AbsoluteMove, position);
return CheckWriteResult(result, " node [" + nodeid + "] CAN_Address.AbsoluteMove");
}
/// <summary>
/// 相对运动
/// </summary>
public static bool RelMove(uint nodeId, int position)
{
if (PUSICANControl.CheckStop2IsHigh(nodeId))
{
PUSICANControl.ClearStatus(nodeId);
Thread.Sleep(100);
}
//写模式
PUSIResult result;
if (!(GetSDOData(nodeId, CAN_Address.MoveType).Equals(0)))
{
PUSICANControl.WriteSDO(nodeId, CAN_Address.MoveType, 0);
}
if (position > 0)
{
PUSICANControl.WriteSDO(nodeId, CAN_Address.MoveDirection, 1);
}
else
{
PUSICANControl.WriteSDO(nodeId, CAN_Address.MoveDirection, 0);
}
int value = GetMaxSpeed(nodeId);
result = PUSICANControl.WriteSDO(nodeId, CAN_Address.RelativeMove, Math.Abs(position));
return CheckWriteResult(result, " node [" + nodeId + "] CAN_Address.RelativeMove");
}
/// <summary>
/// 匀速运动
/// </summary>
public static bool VolMove(uint nodeid, int speed)
{
//写模式
PUSIResult result;
PUSICANControl.WriteSDO(nodeid, CAN_Address.MoveType, 1);
PUSICANControl.WriteSDO(nodeid, CAN_Address.MoveDirection, 0);
SetMaxSpeed(nodeid, speed);
result = PUSICANControl.WriteSDO(nodeid, CAN_Address.RelativeMove, (speed));
return CheckWriteResult(result, " node [" + nodeid + "] CAN_Address.RelativeMove");
}
/// <summary>
/// 原点返回
/// </summary>
public static bool HomeMove(uint nodeid, int speed)
{
//写模式
PUSIResult result;
PUSICANControl.WriteSDO(nodeid, CAN_Address.MoveType, 1);
if (speed < 0)
{
PUSICANControl.WriteSDO(nodeid, CAN_Address.MoveDirection, 1);
}
else
{
PUSICANControl.WriteSDO(nodeid, CAN_Address.MoveDirection, 0);
}
SetMaxSpeed(nodeid, speed);
result = PUSICANControl.WriteSDO(nodeid, CAN_Address.RelativeMove, Math.Abs(speed));
return CheckWriteResult(result, " node [" + nodeid + "] CAN_Address.RelativeMove");
}
/// <summary>
/// 原点返回 ,旋转轴原点返回使用的方法
/// </summary>
public static bool HomeMove(uint nodeid,bool isLineSlv=false )
{
int speed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_HomeSpeed, nodeid.ToString());
if (isLineSlv)
{
speed = ConfigAppSettings.GetIntValue(Setting_Init.Line_HomeSpeed, nodeid.ToString());
}
//写模式
PUSIResult result;
PUSICANControl.WriteSDO(nodeid, CAN_Address.MoveType, 1);
if (speed < 0)
{
PUSICANControl.WriteSDO(nodeid, CAN_Address.MoveDirection, 1);
}
else
{
PUSICANControl.WriteSDO(nodeid, CAN_Address.MoveDirection, 0);
}
SetMaxSpeed(nodeid, (speed));
result = PUSICANControl.WriteSDO(nodeid, CAN_Address.RelativeMove, Math.Abs(speed));
return CheckWriteResult(result, " node [" + nodeid + "] CAN_Address.RelativeMove");
}
/// <summary>
/// 判断外部停止2是否亮
/// </summary>
/// <param name="nodeId"></param>
/// <returns></returns>
public static bool CheckStop2IsHigh(uint nodeId)
{
//判断 外部停止2=1
int value = 0;
//获取状态
if (PUSIResult.RET_SUCCESS == PUSICANControl.ReadSDO(nodeId, CAN_Address.ControlStatus, out value))
{
string stop2Str = Convert.ToString(value, 2).PadLeft(5, '0').Substring(3, 1);
if ( stop2Str.Equals("1"))
{
return true;
}
else
{
return false;
}
} return false;
}
public static void ClearStatus(uint nodeId)
{
//清理驱动器状态
PUSIResult result = PUSICANControl.WriteSDO(nodeId, CAN_Address.ControlStatus, 1);
}
public static bool RHomeMoveEnd(uint nodeId)
{
//清理驱动器状态
PUSIResult result = PUSICANControl.WriteSDO(nodeId, CAN_Address.ControlStatus, 1);
Thread.Sleep(100);
//重置位置
SetPosition(nodeId, 0);
////重置速度
//int startSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_StartSpeed, nodeId.ToString());
//PUSICANControl.SetStartSpeed(nodeId, startSpeed);
//int stopSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_StopSpeed, nodeId.ToString());
//PUSICANControl.SetStopSpeed(nodeId, stopSpeed);
//int maxSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_MaxSpeed, nodeId.ToString());
//PUSICANControl.SetMaxSpeed(nodeId, maxSpeed);
return true;
}
public static bool SetSpeed(uint nodeId, bool isLineSlv = false)
{
//重置速度
int startSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_StartSpeed, nodeId.ToString());
int stopSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_StopSpeed, nodeId.ToString());
int maxSpeed = ConfigAppSettings.GetIntValue(Setting_Init.RotateNode_MaxSpeed, nodeId.ToString());
if (isLineSlv)
{
if (ConfigAppSettings.GetIntValue(Setting_Init.Line_StartSpeed, nodeId.ToString()) > 0)
{
startSpeed = ConfigAppSettings.GetIntValue(Setting_Init.Line_StartSpeed, nodeId.ToString());
stopSpeed = ConfigAppSettings.GetIntValue(Setting_Init.Line_StopSpeed, nodeId.ToString());
maxSpeed = ConfigAppSettings.GetIntValue(Setting_Init.Line_MaxSpeed, nodeId.ToString());
}
}
PUSICANControl.SetStartSpeed(nodeId, startSpeed);
PUSICANControl.SetStopSpeed(nodeId, stopSpeed);
PUSICANControl.SetMaxSpeed(nodeId, maxSpeed);
return true;
}
public static bool StopMove(uint nodeid)
{
PUSIResult result = PUSICANControl.WriteSDO(nodeid, CAN_Address.StopMove, 0);
return CheckWriteResult(result, " node [" + nodeid + "] CAN_Address.StopMove");
}
public static string GetStatus(SLAVE_STATUS status)
{
string strStatus;
switch (status)
{
case 0x00:
strStatus = "Initialisation";
break;
case SLAVE_STATUS.SLAVESTATUS_INIT:
strStatus = "Disconnected";
break;
case SLAVE_STATUS.Connecting:
strStatus = "Connecting";
break;
case SLAVE_STATUS.SLAVESTATUS_STOP:
strStatus = "Stopped";
break;
case SLAVE_STATUS.SLAVESTATUS_WORK:
strStatus = "Operational";
break;
case SLAVE_STATUS.SLAVESTATUS_PREWORK:
strStatus = "Pre_operational";
break;
case SLAVE_STATUS.SLAVESTATUS_DISCONNECT:
strStatus = "Unknown_state";
break;
default:
strStatus = "error";
break;
}
return strStatus;
}
public static string GetStatus(uint status)
{
string strStatus;
switch (status)
{
case 0x00:
strStatus = "Initialisation";
break;
case 0x01:
strStatus = "Disconnected";
break;
case 0x02:
strStatus = "Connecting";
break;
case 0x04:
strStatus = "Stopped";
break;
case 0x05:
strStatus = "Operational";
break;
case 0x7f:
strStatus = "Pre_operational";
break;
case 0x0f:
strStatus = "Unknown_state";
break;
default:
strStatus = "error";
break;
}
return strStatus;
}
/// <summary>
/// 判断原点返回是否完成
/// </summary>
public static bool IsHomeEnd(uint nodeId)
{
//判断忙碌状态=0并且外部停止2=1
int value = 0;
//获取状态
if (PUSIResult.RET_SUCCESS == PUSICANControl.ReadSDO(nodeId, CAN_Address.ControlStatus, out value))
{
string busyStr = Convert.ToString(value, 2).PadLeft(5, '0').Substring(1, 1);
string stop2Str = Convert.ToString(value, 2).PadLeft(5, '0').Substring(3, 1);
string stop1Str = Convert.ToString(value, 2).PadLeft(5, '0').Substring(4, 1);
if (busyStr.Equals("0") && (stop2Str.Equals("1") || stop1Str.Equals("1")))
{
return true ;
}
else
{
return false ;
}
}
return false ;
}
public static void DefatutPosMove(uint node, int targetDefaultPosition, int movetype = 0,bool isLineSlv=false )
{
PUSICANControl.RHomeMoveEnd(node);
Thread.Sleep(10);
//设置为位置模式
PUSICANControl.WriteSDO(node, CAN_Address.MoveType, movetype);
Thread.Sleep(100);
bool isLineslv = node.Equals(isLineSlv);
PUSICANControl.SetSpeed(node);
Thread.Sleep(10);
//所有轴走到待机点
PUSICANControl.AbsMove(node, targetDefaultPosition);
}
/// <summary>
/// 运动是否结束
/// </summary>
public static bool MoveIsEnd(uint nodeId)
{
//判断忙碌状态=0 当前坐标=targetPosition
int value = 0;
//获取状态
if (PUSIResult.RET_SUCCESS == PUSICANControl.ReadSDO(nodeId, CAN_Address.ControlStatus, out value))
{
string busyStr = Convert.ToString(value, 2).PadLeft(5, '0').Substring(1, 1);
if (busyStr.Equals("1"))
{
return false;
}
else
{
return true;
}
}
return false;
}
/// <summary>
/// 运动是否结束
/// </summary>
public static bool IsDuZhuan(uint nodeId)
{
//判断忙碌状态=0 当前坐标=targetPosition
int value = 0;
//获取状态
if (PUSIResult.RET_SUCCESS == PUSICANControl.ReadSDO(nodeId, CAN_Address.ControlStatus, out value))
{
string str = Convert.ToString(value, 2).PadLeft(5, '0');
string busyStr = Convert.ToString(value, 2).PadLeft(5, '0').Substring(2, 1);
if (busyStr.Equals("1"))
{
return true ;
}
else
{
return false ;
}
}
return false;
}
/// <summary>
/// 运动是否结束
/// </summary>
public static bool MoveIsEnd(uint nodeId, int targetPosition,out string waitMsg)
{
//判断忙碌状态=0 当前坐标=targetPosition
int currPosition = GetPosition(nodeId);
int cha = Math.Abs(targetPosition - currPosition);
waitMsg = "当前坐标[" + currPosition + "]";
if (cha < 10)
{
int value = 0;
//获取状态
if (PUSIResult.RET_SUCCESS == PUSICANControl.ReadSDO(nodeId, CAN_Address.ControlStatus, out value))
{
string busyStr = Convert.ToString(value, 2).PadLeft(5, '0').Substring(1, 1);
waitMsg = waitMsg + "忙碌状态["+busyStr+"]";
if (busyStr.Equals("1"))
{
return false;
}
else
{
return true;
}
//lblControlStatus.Text = ("状态:外部停止3:[" + str.Substring(0, 1) + "],busy状态:[" + str.Substring(1, 1) + "],堵转状态:["
// + str.Substring(2, 1) + "],外部停止2:[" + str.Substring(3, 1) + "],外部停止1:[" + str.Substring(4, 1) + "]");
}
}
return false ;
}
public static List<string> getAdapterList()
{
List<string> list = new List<string>();
list.Add("蓝马电子A1+");
list.Add("乐的USB-CAN");
list.Add("YL9100");
list.Add("IXXAT");
list.Add("广成USBCAN-I");
list.Add("周立功USBCAN-I");
list.Add("周立功USBCAN-E-U");
list.Add("周立功CANET-TCPC");
list.Add("创芯CANalyst-II");
list.Add("周立功CANCOM-100IE");
return list;
}
public static List<string> getBoauList()
{
List<string> list = new List<string>();
list.Add("10Kbps");
list.Add("20Kbps");
list.Add("50Kbps");
list.Add("100Kbps");
list.Add("125Kbps");
list.Add("250Kbps");
list.Add("500Kbps");
list.Add("800Kbps");
list.Add("1000Kbps");
return list;
}
public static List<string> getXifenList()
{
List<string> list = new List<string>();
list.Add("0");
list.Add("2");
list.Add("4");
list.Add("8");
list.Add("16");
list.Add("32");
list.Add("64");
list.Add("128");
list.Add("256");
return list;
}
}
}