forked from hykym0301/GSYS2013_b6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdlTrig.bas
2181 lines (1802 loc) · 60.5 KB
/
mdlTrig.bas
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
Attribute VB_Name = "mdlTrig"
Const OVERTIME_SEC As Integer = 5 '0.5
Const ERROR_INPUT_CHANNEL As String = "Wrong input channel number."
Const DEFAULT_COUNTPER_UM As Double = 0.3125 '0.0003125mm
Const MINIMIZE_FIRE_PITCH_MM As Double = 0.002
Const RESOLUTION_TRIGGER_BORAD_TIME_NS As Double = (10 ^ 8)
Public Const Trig_Mode_OnePulse_Select_Bit As Integer = 0
Public Const Trig_Mode_Encoder_Select_Bit As Integer = 1
Public Const Trig_Mode_Frequnecy_Select_Bit As Integer = 2
Public Enum eARM_ADDRESS
'// Common
ENC_DIR_REG = 0
FILTER_VALUE_REG
STROBE_MODE_EN_REG
STROBE_VALUE_REG
'// Channel 1
TRIG_START1
TRIG_STOP1
INT_MODE_EN1
TIME_VALUE_REG1 'Freq INT Trigger
PULSE_WIDTH_REG1 'Freq INT Trigger
STROBE_ONTIME_REG1
STROBE_DELAY_REG1
STROBE_BRIGHT_VALUE_REG1
STROBO_DOT_COUNTS1
ENC_POS_REG1
ENC_START_POS_REG1
ENC_INC_END_POS_REG1
ENC_INT_ADD_REG1
ENC_PITCH_REG1
ENC_STOP_POS_REG1
'// Channel 2
TRIG_START2
TRIG_STOP2
INT_MODE_EN2
TIME_VALUE_REG2
PULSE_WIDTH_REG2
STROBE_ONTIME_REG2
STROBE_DELAY_REG2
STROBE_BRIGHT_VALUE_REG2
STROBO_DOT_COUNTS2
ENC_POS_REG2
ENC_START_POS_REG2
ENC_INC_END_POS_REG2
ENC_INT_ADD_REG2
ENC_PITCH_REG2
ENC_STOP_POS_REG2
'// Channel 3
TRIG_START3
TRIG_STOP3
INT_MODE_EN3
TIME_VALUE_REG3
PULSE_WIDTH_REG3
STROBE_ONTIME_REG3
STROBE_DELAY_REG3
STROBE_BRIGHT_VALUE_REG3
STROBO_DOT_COUNTS3
ENC_POS_REG3
ENC_START_POS_REG3
ENC_INC_END_POS_REG3
ENC_INT_ADD_REG3
ENC_PITCH_REG3
ENC_STOP_POS_REG3
FPGA_STATE
TRIG_Mode1 'Ch0 Trigger Mode
TRIG_Mode2 'Ch1 Trigger Mode
TRIG_Mode3 'Ch2 Trigger Mode
ENC_RevStartPOS_REG1
ENC_RevStopPOS_REG1
ENC_RevStartPOS_REG2
ENC_RevStopPOS_REG2
ENC_RevStartPOS_REG3
ENC_RevStopPOS_REG3
End Enum
Public Enum eFPGA_STATUS_BIT
TRIG_START1 = 0
TRIG_STOP1
INT_MODE_EN1
NC3
NC4
NC5
NC6
NC7
TRIG_START2
TRIG_STOP2
INT_MODE_EN2
NC11
NC12
NC13
NC14
NC15
TRIG_START3
TRIG_STOP3
INT_MODE_EN3
End Enum
Public Enum eENC_DIR
Forward = 1
Backward = 0
End Enum
Public Enum eCHANNEL
ch1 = 0
CH2
CH3
End Enum
Public Enum eNOISE_FILTER
OPTION_0 = 0
OPTION_1 = 1
OPTION_2 = 2
OPTION_3 = 3
End Enum
Public Enum eBitNo
'// 1Byte: 8Bit
Bit0 = 0
Bit1
Bit2
Bit3
Bit4
Bit5
Bit6
Bit7
'// 2Byte: 16Bit
Bit8 = 8
Bit9
Bit10
Bit11
Bit12
Bit13
Bit14
Bit15
'// 3Byte: 24Bit
Bit16 = 16
Bit17
Bit18
Bit19
Bit20
Bit21
Bit22
Bit23
'// 4Byte: 32Bit
Bit24 = 24
Bit25
Bit26
Bit27
Bit28
Bit29
Bit30
Bit31
End Enum
Public Type ST_FPGA
bTRIG_START(eCHANNEL.CH3) As Boolean
bTRIG_STOP(eCHANNEL.CH3) As Boolean
bINT_MODE_EN(eCHANNEL.CH3) As Boolean
End Type
Dim fwsk As Winsock
Dim fbConnected As Boolean
Private fbyteSendBuf() As Byte
Private fbyteReadBuf() As Byte
Private fstrARM_Adr() As String
Private fbDataArrival As Boolean
Private fdCountPerUm(2) As Double
Public gFPGASts As ST_FPGA
Public glngValue As Long
Public gdEncPos_Mm(3) As Double
Public Trig_Connected As Boolean
Sub InitVar()
On Error GoTo syserr
fbDataArrival = False
fbConnected = False
ReDim fstrARM_Adr(eARM_ADDRESS.ENC_DIR_REG To eARM_ADDRESS.ENC_RevStopPOS_REG3) 'eARM_ADDRESS.FPGA_STATE
fstrARM_Adr(eARM_ADDRESS.ENC_DIR_REG) = "26000000" '---------> use
fstrARM_Adr(eARM_ADDRESS.STROBE_BRIGHT_VALUE_REG1) = "26000010" '---------> use
fstrARM_Adr(eARM_ADDRESS.TRIG_Mode1) = "26000020" '---------> use
fstrARM_Adr(eARM_ADDRESS.TRIG_START1) = "26000104" '---------> use
fstrARM_Adr(eARM_ADDRESS.TRIG_STOP1) = "26000108" '---------> use
fstrARM_Adr(eARM_ADDRESS.INT_MODE_EN1) = "2600010C" '---------> use
fstrARM_Adr(eARM_ADDRESS.TIME_VALUE_REG1) = "26000110" '---------> use
fstrARM_Adr(eARM_ADDRESS.PULSE_WIDTH_REG1) = "26000114" '---------> use
fstrARM_Adr(eARM_ADDRESS.STROBE_ONTIME_REG1) = "2600011C" '---------> use
fstrARM_Adr(eARM_ADDRESS.STROBE_DELAY_REG1) = "26000118" '---------> use
fstrARM_Adr(eARM_ADDRESS.ENC_POS_REG1) = "26000120" '---------> use
fstrARM_Adr(eARM_ADDRESS.ENC_PITCH_REG1) = "26000130" '---------> use
fstrARM_Adr(eARM_ADDRESS.ENC_STOP_POS_REG1) = "26000134" '---------> use
fstrARM_Adr(eARM_ADDRESS.ENC_START_POS_REG1) = "26000124" '---------> use
fstrARM_Adr(eARM_ADDRESS.ENC_DIR_REG) = "26000000" '---------> use
fstrARM_Adr(eARM_ADDRESS.FILTER_VALUE_REG) = "26000004"
fstrARM_Adr(eARM_ADDRESS.STROBE_MODE_EN_REG) = "26000008"
fstrARM_Adr(eARM_ADDRESS.STROBE_VALUE_REG) = "2600000C"
fstrARM_Adr(eARM_ADDRESS.TRIG_START1) = "26000104" '---------> use
fstrARM_Adr(eARM_ADDRESS.TRIG_STOP1) = "26000108" '---------> use
fstrARM_Adr(eARM_ADDRESS.INT_MODE_EN1) = "2600010C" '---------> use
fstrARM_Adr(eARM_ADDRESS.TIME_VALUE_REG1) = "26000110" '---------> use
fstrARM_Adr(eARM_ADDRESS.PULSE_WIDTH_REG1) = "26000114" '---------> use
fstrARM_Adr(eARM_ADDRESS.STROBE_ONTIME_REG1) = "2600011C" '---------> use
fstrARM_Adr(eARM_ADDRESS.STROBE_DELAY_REG1) = "26000118" '---------> use
fstrARM_Adr(eARM_ADDRESS.STROBE_BRIGHT_VALUE_REG1) = "26000010" '---------> use
fstrARM_Adr(eARM_ADDRESS.STROBO_DOT_COUNTS1) = "26000150"
fstrARM_Adr(eARM_ADDRESS.ENC_POS_REG1) = "26000120" '---------> use
fstrARM_Adr(eARM_ADDRESS.ENC_START_POS_REG1) = "26000124" '---------> use
fstrARM_Adr(eARM_ADDRESS.ENC_INC_END_POS_REG1) = "26000128"
fstrARM_Adr(eARM_ADDRESS.ENC_INT_ADD_REG1) = "2600012C"
fstrARM_Adr(eARM_ADDRESS.ENC_PITCH_REG1) = "26000130" 'Trig_Mode_Encoder_Select_Bit 일때는 엔코더 피치로 '---------> use
'Trig_Mode_Frequnecy_Select_Bit 일때는 프리시
fstrARM_Adr(eARM_ADDRESS.ENC_STOP_POS_REG1) = "26000134" '---------> use
fstrARM_Adr(eARM_ADDRESS.TRIG_START2) = "26000204"
fstrARM_Adr(eARM_ADDRESS.TRIG_STOP2) = "26000208"
fstrARM_Adr(eARM_ADDRESS.INT_MODE_EN2) = "2600020C"
fstrARM_Adr(eARM_ADDRESS.TIME_VALUE_REG2) = "26000210"
fstrARM_Adr(eARM_ADDRESS.PULSE_WIDTH_REG2) = "26000214"
fstrARM_Adr(eARM_ADDRESS.STROBE_ONTIME_REG2) = "2600021C"
fstrARM_Adr(eARM_ADDRESS.STROBE_DELAY_REG2) = "26000218"
fstrARM_Adr(eARM_ADDRESS.STROBE_BRIGHT_VALUE_REG2) = "26000014"
fstrARM_Adr(eARM_ADDRESS.STROBO_DOT_COUNTS2) = "26000250"
fstrARM_Adr(eARM_ADDRESS.ENC_POS_REG2) = "26000220"
fstrARM_Adr(eARM_ADDRESS.ENC_START_POS_REG2) = "26000224"
fstrARM_Adr(eARM_ADDRESS.ENC_INC_END_POS_REG2) = "26000228"
fstrARM_Adr(eARM_ADDRESS.ENC_INT_ADD_REG2) = "2600022C"
fstrARM_Adr(eARM_ADDRESS.ENC_PITCH_REG2) = "26000230"
fstrARM_Adr(eARM_ADDRESS.ENC_STOP_POS_REG2) = "26000234"
fstrARM_Adr(eARM_ADDRESS.TRIG_START3) = "26000304"
fstrARM_Adr(eARM_ADDRESS.TRIG_STOP3) = "26000308"
fstrARM_Adr(eARM_ADDRESS.INT_MODE_EN3) = "2600030C"
fstrARM_Adr(eARM_ADDRESS.TIME_VALUE_REG3) = "26000310"
fstrARM_Adr(eARM_ADDRESS.PULSE_WIDTH_REG3) = "26000314"
fstrARM_Adr(eARM_ADDRESS.STROBE_ONTIME_REG3) = "2600031C"
fstrARM_Adr(eARM_ADDRESS.STROBE_DELAY_REG3) = "26000318"
fstrARM_Adr(eARM_ADDRESS.STROBE_BRIGHT_VALUE_REG3) = "26000018"
fstrARM_Adr(eARM_ADDRESS.STROBO_DOT_COUNTS3) = "26000350"
fstrARM_Adr(eARM_ADDRESS.ENC_POS_REG3) = "26000320"
fstrARM_Adr(eARM_ADDRESS.ENC_START_POS_REG3) = "26000324"
fstrARM_Adr(eARM_ADDRESS.ENC_INC_END_POS_REG3) = "26000328"
fstrARM_Adr(eARM_ADDRESS.ENC_INT_ADD_REG3) = "2600032C"
fstrARM_Adr(eARM_ADDRESS.ENC_PITCH_REG3) = "26000330"
fstrARM_Adr(eARM_ADDRESS.ENC_STOP_POS_REG3) = "26000334"
fstrARM_Adr(eARM_ADDRESS.FPGA_STATE) = "26000F00"
fstrARM_Adr(eARM_ADDRESS.TRIG_Mode1) = "26000020" '---------> use
fstrARM_Adr(eARM_ADDRESS.TRIG_Mode2) = "26000024"
fstrARM_Adr(eARM_ADDRESS.TRIG_Mode3) = "26000028"
fstrARM_Adr(eARM_ADDRESS.ENC_RevStartPOS_REG1) = "26000164"
fstrARM_Adr(eARM_ADDRESS.ENC_RevStopPOS_REG1) = "26000174"
fstrARM_Adr(eARM_ADDRESS.ENC_RevStartPOS_REG2) = "26000264"
fstrARM_Adr(eARM_ADDRESS.ENC_RevStopPOS_REG2) = "26000274"
fstrARM_Adr(eARM_ADDRESS.ENC_RevStartPOS_REG3) = "26000364"
fstrARM_Adr(eARM_ADDRESS.ENC_RevStopPOS_REG3) = "26000374"
fdCountPerUm(0) = DEFAULT_COUNTPER_UM
fdCountPerUm(1) = DEFAULT_COUNTPER_UM
fdCountPerUm(2) = DEFAULT_COUNTPER_UM
Exit Sub
syserr:
Call MsgBox(Err.Description, vbExclamation)
End Sub
Public Sub SetUmPerCount(ipChNo As Integer, ByVal i_dUmPerCount As Double)
On Error GoTo syserr
fdCountPerUm(ipChNo) = i_dUmPerCount
Exit Sub
syserr:
End Sub
Public Function GetWinsockStatus() As Boolean
On Error GoTo syserr
If fwsk.State = sckConnected Then
GetWinsockStatus = True
Else
GetWinsockStatus = False
End If
Exit Function
syserr:
GetWinsockStatus = False
End Function
Public Function Connect(i_objWSock As Winsock, i_strIP As String, i_lngPortNo As Long) As Boolean
On Error GoTo syserr
Set fwsk = i_objWSock
Trig_Connected = False
If GetWinsockStatus() = True Then
fwsk.Close
Call CheckTimer(True)
Do
DoEvents
If CheckTimer(False) > OVERTIME_SEC Then
Call CheckTimer(True)
If fwsk.State <> sckClosed Then
fwsk.Close
End If
Connect = False
Exit Function
End If
Loop Until fwsk.State = sckClosed
End If
Call InitVar
fwsk.RemoteHost = i_strIP
fwsk.RemotePort = i_lngPortNo
fwsk.Connect
Call CheckTimer(True)
Do
DoEvents
If CheckTimer(False) > OVERTIME_SEC Then
Call CheckTimer(True)
If fwsk.State <> sckClosed Then
fwsk.Close
End If
Connect = False
Exit Function
End If
Loop Until fwsk.State = sckConnected
Call CheckTimer(True)
Connect = True
Trig_Connected = True
Exit Function
syserr:
Call MsgBox(Err.Description, vbExclamation)
Connect = False
End Function
Public Sub Disconnect()
On Error GoTo syserr
fwsk.Close
Exit Sub
syserr:
Call MsgBox(Err.Description, vbExclamation)
End Sub
Private Function SendData(i_pOutBuf() As Byte) As Boolean
On Error GoTo syserr
If GetWinsockStatus() = False Then
Exit Function
End If
fbDataArrival = False
Call fwsk.SendData(i_pOutBuf)
SendData = True
Exit Function
syserr:
Call MsgBox(Err.Description, vbExclamation)
SendData = False
End Function
Function ReceiveData(i_lngBufSize As Long) As Boolean
On Error GoTo syserr
ReDim fbyteReadBuf(i_lngBufSize - 1)
Call fwsk.GetData(fbyteReadBuf, vbArray + vbByte)
fbDataArrival = True
ReceiveData = True
Exit Function
syserr:
Call MsgBox(Err.Description, vbExclamation)
ReceiveData = False
End Function
Function CheckTimer(i_bClearAll As Boolean) As Double
On Error GoTo syserr
Static bStart As Boolean
Static dDuStart As Double
Static dDurationTime As Double
If i_bClearAll = False Then
If bStart = False Then
dDuStart = GetTickCount / (10 ^ 3)
bStart = True
Else
dDurationTime = Abs((GetTickCount / (10 ^ 3)) - dDuStart)
CheckTimer = dDurationTime
End If
Else
bStart = False
dDuStart = 0
dDurationTime = 0
CheckTimer = 0
End If
Exit Function
syserr:
Call MsgBox(Err.Description, vbExclamation)
End Function
Function ParshHextoString(i_strHex As String, i_nQuantity As Integer) As String
On Error GoTo syserr
Dim strHex As String
Dim i As Integer
strHex = i_strHex
For i = 1 To (i_nQuantity - Len(i_strHex)) 'Step -1
strHex = "0" & strHex
Next i
ParshHextoString = strHex
Exit Function
syserr:
Call MsgBox(Err.Description, vbExclamation)
End Function
Function ParshHexToDec(ByVal strHex As String) As String
On Error GoTo syserr
Dim i As Long
Dim strDigit As String
Dim lngDigitVal As Long
Dim lngCalcVal As Long
Dim bMinusF As Boolean
For i = 1 To Len(strHex)
strDigit = Mid(strHex, i, 1)
Select Case strDigit
Case "A"
lngDigitVal = 10
Case "B"
lngDigitVal = 11
Case "C"
lngDigitVal = 12
Case "D"
lngDigitVal = 13
Case "E"
lngDigitVal = 14
Case "F"
lngDigitVal = 15
Case Else
lngDigitVal = Val(strDigit)
End Select
If i = 1 And lngDigitVal > 7 Then
bMinusF = True
End If
lngCalcVal = lngCalcVal + (lngDigitVal * (16 ^ (Len(strHex) - i)))
Next i
If bMinusF = True Then
lngCalcVal = -(65536 - lngCalcVal)
End If
ParshHexToDec = CStr(lngCalcVal)
Exit Function
syserr:
Call MsgBox(Err.Description, vbExclamation)
End Function
Function CheckSum(i_pBuf() As Byte) As Byte
On Error GoTo syserr
Dim i As Integer
Dim lngCal_data As Long
Dim nHeaderByte As Integer
Dim nTerminateByte As Integer
nHeaderByte = 5 'Header(3Byte) + Command+Data Len(2Byte)
nTerminateByte = 2 'Terminate(1Byte) + Checksum(1Byte)
For i = nHeaderByte To (UBound(i_pBuf) - nTerminateByte)
lngCal_data = (lngCal_data) Xor (i_pBuf(i))
Next i
CheckSum = lngCal_data
Exit Function
syserr:
Call MsgBox(Err.Description, vbExclamation)
End Function
Function GetStartStatus(ByVal i_nChannel As eCHANNEL) As Boolean
On Error GoTo syserr
Call GetFPGAStatus
GetStartStatus = gFPGASts.bTRIG_START(i_nChannel)
Exit Function
syserr:
End Function
Function GetStopStatus(ByVal i_nChannel As eCHANNEL) As Boolean
On Error GoTo syserr
Call GetFPGAStatus
GetStopStatus = gFPGASts.bTRIG_STOP(i_nChannel)
Exit Function
syserr:
End Function
Function GetIntModeEN(ByVal i_nChannel As eCHANNEL) As Boolean
On Error GoTo syserr
Call GetFPGAStatus
GetIntModeEN = gFPGASts.bINT_MODE_EN(i_nChannel)
Exit Function
syserr:
End Function
Function GetFPGAStatus() As Boolean
On Error GoTo syserr
Dim nARMAddress As eARM_ADDRESS
nARMAddress = eARM_ADDRESS.FPGA_STATE
ReDim fbyteSendBuf(11)
'Header---------------------
fbyteSendBuf(0) = Val("&H" & "AA")
fbyteSendBuf(1) = Val("&H" & "AA")
fbyteSendBuf(2) = Val("&H" & "98")
'Header---------------------
'Command+Data Len-----------
fbyteSendBuf(3) = Val("&H" & "05")
fbyteSendBuf(4) = Val("&H" & "00")
'Command+Data Len-----------
'Command--------------------
fbyteSendBuf(5) = Val("&H" & "43")
'Command--------------------
'Data Address
fbyteSendBuf(6) = Val("&H" & Mid(fstrARM_Adr(nARMAddress), 7, 2))
fbyteSendBuf(7) = Val("&H" & Mid(fstrARM_Adr(nARMAddress), 5, 2))
fbyteSendBuf(8) = Val("&H" & Mid(fstrARM_Adr(nARMAddress), 3, 2))
fbyteSendBuf(9) = Val("&H" & Mid(fstrARM_Adr(nARMAddress), 1, 2))
'Checksum-------------------
fbyteSendBuf(10) = CheckSum(fbyteSendBuf)
'Checksum-------------------
'Terminate------------------
fbyteSendBuf(11) = Val("&H" & "0A")
'Terminate------------------
Call SendData(fbyteSendBuf)
fbDataArrival = False
Call CheckTimer(True)
Do
DoEvents
If CheckTimer(False) > OVERTIME_SEC Then
Call CheckTimer(True)
Exit Function
End If
Loop Until fbDataArrival = True
Call CheckTimer(True)
If fbyteReadBuf(6) = 255 Then '정상처리 Ack Return
gFPGASts = ParshFPGAStatus(fbyteReadBuf)
GetFPGAStatus = True
Else
Call Get_ErrNo(fbyteReadBuf)
GetFPGAStatus = False
End If
Exit Function
syserr:
Call MsgBox(Err.Description, vbExclamation)
GetFPGAStatus = False
End Function
'// 현재 엔코더 포지션을 설정 또는 획득 (D31 = 부호 셋팅은 '1' 만 가능)
Function GetEncPosition_Mm(ByVal i_nChannel As eCHANNEL) As Double
On Error GoTo syserr
Dim nARMAddress As eARM_ADDRESS
ReDim fbyteSendBuf(11)
If i_nChannel = eCHANNEL.ch1 Then
nARMAddress = eARM_ADDRESS.ENC_POS_REG1
ElseIf i_nChannel = eCHANNEL.CH2 Then
nARMAddress = eARM_ADDRESS.ENC_POS_REG2
ElseIf i_nChannel = eCHANNEL.CH3 Then
nARMAddress = eARM_ADDRESS.ENC_POS_REG3
Else
GetEncPosition_Mm = -1
Exit Function
End If
'Header---------------------
fbyteSendBuf(0) = Val("&H" & "AA")
fbyteSendBuf(1) = Val("&H" & "AA")
fbyteSendBuf(2) = Val("&H" & "98")
'Header---------------------
'Command+Data Len-----------
fbyteSendBuf(3) = Val("&H" & "05")
fbyteSendBuf(4) = Val("&H" & "00")
'Command+Data Len-----------
'Command--------------------
fbyteSendBuf(5) = Val("&H" & "43")
'Command--------------------
'Data Address
fbyteSendBuf(6) = Val("&H" & Mid(fstrARM_Adr(nARMAddress), 7, 2))
fbyteSendBuf(7) = Val("&H" & Mid(fstrARM_Adr(nARMAddress), 5, 2))
fbyteSendBuf(8) = Val("&H" & Mid(fstrARM_Adr(nARMAddress), 3, 2))
fbyteSendBuf(9) = Val("&H" & Mid(fstrARM_Adr(nARMAddress), 1, 2))
'Checksum-------------------
fbyteSendBuf(10) = CheckSum(fbyteSendBuf)
'Checksum-------------------
'Terminate------------------
fbyteSendBuf(11) = Val("&H" & "0A")
'Terminate------------------
Call SendData(fbyteSendBuf)
fbDataArrival = False
Call CheckTimer(True)
Do
DoEvents
If CheckTimer(False) > OVERTIME_SEC Then '
Call CheckTimer(True)
Exit Function
End If
Loop Until fbDataArrival = True
Call CheckTimer(True)
If fbyteReadBuf(6) = 255 Then '정상처리 Ack Return
gdEncPos_Mm(i_nChannel) = ParshTrigBdPos_Mm(i_nChannel, fbyteReadBuf)
GetEncPosition_Mm = gdEncPos_Mm(i_nChannel)
Else
GetEncPosition_Mm = -1
Call Get_ErrNo(fbyteReadBuf)
End If
Exit Function
syserr:
Call MsgBox(Err.Description, vbExclamation)
GetEncPosition_Mm = -1
End Function
Function GetRegistry(ByVal i_nARMAddress As eARM_ADDRESS) As Double
On Error GoTo syserr
If GetWinsockStatus() = False Then
GetRegistry = -1
Exit Function
End If
ReDim fbyteSendBuf(11)
'Header---------------------
fbyteSendBuf(0) = Val("&H" & "AA")
fbyteSendBuf(1) = Val("&H" & "AA")
fbyteSendBuf(2) = Val("&H" & "98")
'Header---------------------
'Command+Data Len-----------
fbyteSendBuf(3) = Val("&H" & "05")
fbyteSendBuf(4) = Val("&H" & "00")
'Command+Data Len-----------
'Command--------------------
fbyteSendBuf(5) = Val("&H" & "43")
'Command--------------------
'Data Address
fbyteSendBuf(6) = Val("&H" & Mid(fstrARM_Adr(i_nARMAddress), 7, 2))
fbyteSendBuf(7) = Val("&H" & Mid(fstrARM_Adr(i_nARMAddress), 5, 2))
fbyteSendBuf(8) = Val("&H" & Mid(fstrARM_Adr(i_nARMAddress), 3, 2))
fbyteSendBuf(9) = Val("&H" & Mid(fstrARM_Adr(i_nARMAddress), 1, 2))
'Checksum-------------------
fbyteSendBuf(10) = CheckSum(fbyteSendBuf)
'Checksum-------------------
'Terminate------------------
fbyteSendBuf(11) = Val("&H" & "0A")
'Terminate------------------
Call SendData(fbyteSendBuf)
fbDataArrival = False
Call CheckTimer(True)
Do
DoEvents
If CheckTimer(False) > OVERTIME_SEC Then
Call CheckTimer(True)
Exit Function
End If
Loop Until fbDataArrival = True
Call CheckTimer(True)
If fbyteReadBuf(6) = 255 Then '정상처리 Ack Return
If (i_nARMAddress = ENC_POS_REG1) Or (i_nARMAddress = ENC_START_POS_REG1) Or (i_nARMAddress = ENC_INC_END_POS_REG1) Or (i_nARMAddress = ENC_STOP_POS_REG1) Then
GetRegistry = ParshTrigBdPos_Mm(ch1, fbyteReadBuf)
ElseIf (i_nARMAddress = ENC_POS_REG2) Or (i_nARMAddress = ENC_START_POS_REG2) Or (i_nARMAddress = ENC_INC_END_POS_REG2) Or (i_nARMAddress = ENC_STOP_POS_REG2) Then
GetRegistry = ParshTrigBdPos_Mm(CH2, fbyteReadBuf)
ElseIf (i_nARMAddress = ENC_POS_REG3) Or (i_nARMAddress = ENC_START_POS_REG3) Or (i_nARMAddress = ENC_INC_END_POS_REG3) Or (i_nARMAddress = ENC_STOP_POS_REG3) Then
GetRegistry = ParshTrigBdPos_Mm(CH3, fbyteReadBuf)
Else
GetRegistry = ParshResponse(fbyteReadBuf)
End If
Else
Call Get_ErrNo(fbyteReadBuf)
End If
Exit Function
syserr:
Call MsgBox(Err.Description, vbExclamation)
GetRegistry = -1
End Function
Private Sub DelayWaitTrg(ipWaitTime As Double, ipFlagDoeventsEnabled As Boolean)
rtn_dbl# = DelayWait_TimeCheck(True)
Do
If ipFlagDoeventsEnabled = True Then
DoEvents
End If
Loop Until DelayWait_TimeCheck(False) >= ipWaitTime
rtn_dbl# = DelayWait_TimeCheck(True)
End Sub
Private Function DelayWaitTrg_TimeCheck(ClearAll As Boolean) As Double
Static fl_Start As Boolean
Static DuStart As Double
Static DurationTime As Double
If ClearAll = False Then
If fl_Start = False Then
DuStart = GetTickCount / (10 ^ 3)
fl_Start = True
Else
DurationTime = Abs((GetTickCount / (10 ^ 3)) - DuStart)
DelayWaitTrg_TimeCheck = DurationTime
End If
Else
fl_Start = False
DuStart = 0
DurationTime = 0
DelayWaitTrg_TimeCheck = 0
End If
End Function
Function SendRegistry(ByVal i_nARMAddress As eARM_ADDRESS, ByVal i_dValue As Double) As Boolean
On Error GoTo syserr
Dim strValue As String
If GetWinsockStatus() = False Then
SendRegistry = False
Exit Function
End If
ReDim fbyteSendBuf(15)
strValue = ParshHextoString(Hex(i_dValue), 8)
'Header---------------------
fbyteSendBuf(0) = Val("&H" & "AA")
fbyteSendBuf(1) = Val("&H" & "AA")
fbyteSendBuf(2) = Val("&H" & "98")
'Header---------------------
'Command+Data Len-----------
fbyteSendBuf(3) = Val("&H" & "09")
fbyteSendBuf(4) = Val("&H" & "00")
'Command+Data Len-----------
'Command--------------------
fbyteSendBuf(5) = Val("&H" & "42")
'Command--------------------
'Data Address
fbyteSendBuf(6) = Val("&H" & Mid(fstrARM_Adr(i_nARMAddress), 7, 2))
fbyteSendBuf(7) = Val("&H" & Mid(fstrARM_Adr(i_nARMAddress), 5, 2))
fbyteSendBuf(8) = Val("&H" & Mid(fstrARM_Adr(i_nARMAddress), 3, 2))
fbyteSendBuf(9) = Val("&H" & Mid(fstrARM_Adr(i_nARMAddress), 1, 2))
'Data Value
fbyteSendBuf(10) = Val("&H" & Mid(strValue, 7, 2))
fbyteSendBuf(11) = Val("&H" & Mid(strValue, 5, 2))
fbyteSendBuf(12) = Val("&H" & Mid(strValue, 3, 2))
fbyteSendBuf(13) = Val("&H" & Mid(strValue, 1, 2))
'Checksum-------------------
fbyteSendBuf(14) = CheckSum(fbyteSendBuf)
'Checksum-------------------
'Terminate------------------
fbyteSendBuf(15) = Val("&H" & "0A")
'Terminate------------------
Call SendData(fbyteSendBuf)
fbDataArrival = False
Call CheckTimer(True)
Do
DoEvents
If CheckTimer(False) > OVERTIME_SEC Then
Call CheckTimer(True)
Exit Function
End If
Loop Until fbDataArrival = True
Call CheckTimer(True)
If fbyteReadBuf(6) = 255 Then '정상처리 Ack Return
SendRegistry = True
Else
SendRegistry = False
Call Get_ErrNo(fbyteReadBuf)
End If
Exit Function
syserr:
Call MsgBox(Err.Description, vbExclamation)
SendRegistry = False
End Function
Public Function Get_ErrNo(i_pInBuf() As Byte) As Double
On Error GoTo syserr
Dim strErrHex As String
Dim dErrNo As Double
strErrHex = ParshHextoString(Hex(i_pInBuf(10)), 2)
strErrHex = strErrHex & ParshHextoString(Hex(i_pInBuf(9)), 2)
strErrHex = strErrHex & ParshHextoString(Hex(i_pInBuf(8)), 2)
strErrHex = strErrHex & ParshHextoString(Hex(i_pInBuf(7)), 2)
dErrNo = ParshHexToDec(strErrHex)
Call MsgBox(Format(dErrNo, "0"), vbExclamation)
Get_ErrNo = dErrNo
Exit Function
syserr:
Call MsgBox(Err.Description, vbExclamation)
Get_ErrNo = -1
End Function
Public Function ParshFPGAStatus(i_pInBuf() As Byte) As ST_FPGA
On Error GoTo syserr
Dim i As eFPGA_STATUS_BIT
Dim lngValue As Long
Dim bValue As Boolean
Dim strGetValue(7) As String
Dim strHex As String
Dim ST_FPGASts As ST_FPGA
Dim nBufSize As Integer
nBufSize = UBound(i_pInBuf)
If UBound(i_pInBuf) <> 16 Then
ParshFPGAStatus = gFPGASts
Exit Function
End If
strGetValue(0) = ParshHextoString(Hex(i_pInBuf(7)), 2)
strGetValue(1) = ParshHextoString(Hex(i_pInBuf(8)), 2)
strGetValue(2) = ParshHextoString(Hex(i_pInBuf(9)), 2)
strGetValue(3) = ParshHextoString(Hex(i_pInBuf(10)), 2)
strGetValue(4) = ParshHextoString(Hex(i_pInBuf(11)), 2)
strGetValue(5) = ParshHextoString(Hex(i_pInBuf(12)), 2)
strGetValue(6) = ParshHextoString(Hex(i_pInBuf(13)), 2)
strGetValue(7) = ParshHextoString(Hex(i_pInBuf(14)), 2)
strHex = (strGetValue(7) & strGetValue(6) & strGetValue(5) & strGetValue(4))
lngValue = Val("&H" & strHex)
For i = eFPGA_STATUS_BIT.TRIG_START1 To eFPGA_STATUS_BIT.INT_MODE_EN3 Step 1
If ((lngValue And (2 ^ i)) <> 0) Then
bValue = True
Else
bValue = False
End If
Select Case i
Case eFPGA_STATUS_BIT.TRIG_START1
ST_FPGASts.bTRIG_START(eCHANNEL.ch1) = bValue
Case eFPGA_STATUS_BIT.TRIG_STOP1
ST_FPGASts.bTRIG_STOP(eCHANNEL.ch1) = bValue
Case eFPGA_STATUS_BIT.INT_MODE_EN1
ST_FPGASts.bINT_MODE_EN(eCHANNEL.ch1) = bValue
Case eFPGA_STATUS_BIT.TRIG_START2
ST_FPGASts.bTRIG_START(eCHANNEL.CH2) = bValue
Case eFPGA_STATUS_BIT.TRIG_STOP2
ST_FPGASts.bTRIG_STOP(eCHANNEL.CH2) = bValue
Case eFPGA_STATUS_BIT.INT_MODE_EN2
ST_FPGASts.bINT_MODE_EN(eCHANNEL.CH2) = bValue
Case eFPGA_STATUS_BIT.TRIG_START3
ST_FPGASts.bTRIG_START(eCHANNEL.CH3) = bValue
Case eFPGA_STATUS_BIT.TRIG_STOP3
ST_FPGASts.bTRIG_STOP(eCHANNEL.CH3) = bValue
Case eFPGA_STATUS_BIT.INT_MODE_EN3
ST_FPGASts.bINT_MODE_EN(eCHANNEL.CH3) = bValue
End Select
Next i
ParshFPGAStatus = ST_FPGASts
Exit Function
syserr:
Call MsgBox(Err.Description, vbExclamation)
End Function
Public Function ParshResponse(i_pInBuf() As Byte) As Double
On Error GoTo syserr
Dim i As eFPGA_STATUS_BIT
Dim dValue As Double
Dim strGetValue(7) As String
Dim strHex As String
Dim nBufSize As Integer
nBufSize = UBound(i_pInBuf)
If UBound(i_pInBuf) <> 16 Then
ParshResponse = -1
Exit Function
End If
strGetValue(0) = ParshHextoString(Hex(i_pInBuf(7)), 2)
strGetValue(1) = ParshHextoString(Hex(i_pInBuf(8)), 2)
strGetValue(2) = ParshHextoString(Hex(i_pInBuf(9)), 2)
strGetValue(3) = ParshHextoString(Hex(i_pInBuf(10)), 2)
strGetValue(4) = ParshHextoString(Hex(i_pInBuf(11)), 2)
strGetValue(5) = ParshHextoString(Hex(i_pInBuf(12)), 2)
strGetValue(6) = ParshHextoString(Hex(i_pInBuf(13)), 2)
strGetValue(7) = ParshHextoString(Hex(i_pInBuf(14)), 2)
strHex = strGetValue(7) & strGetValue(6) & strGetValue(5) & strGetValue(4)
dValue = CDbl("&H" & strHex)
ParshResponse = dValue
Exit Function