-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModule1.vb
1778 lines (1492 loc) · 82.4 KB
/
Module1.vb
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
'/**************************************************************************
'*
'* Filename: PS3000ACSConsole.cs
'*
'* Copyright: Pico Technology Limited 2012
'*
'* Author: MJL
'*
'* Description:
'* This is a console-mode program that demonstrates how to use the
'* PS3000A driver using .NET
'*
'* Examples:
'* Collect a block of Analogue samples immediately
'* Collect a block of Analogue samples when a trigger event occurs
'* Collect a stream of Analogue data
'* Collect a stream of Analogue data and show when a trigger event occurs
'*
'* Collect a block of Digital Samples immediately
'* Collect a block of Digital Samples when a Digital trigger event occurs
'* Collect a block of Digital and Analogue Samples when an Analogue AND a Digital trigger event occurs
'* Collect a block of Digital and Analogue Samples when an Analogue OR a Digital trigger event occurs
'* Collect a stream of Digital Samples
'* Collect a stream of Digital Samples and show Aggregated results
'*
'*
'***************************************************************************/
Imports System
Imports System.IO
Imports System.Threading
Imports PS3000AVBConsole.picotech
Imports PS3000AVBConsole.picotech.PS3000AAPI
Imports Microsoft.VisualBasic
Namespace picotech.PS3000A
Structure ChannelSettings
Public DCcoupled As Boolean
Public range As PS3000AAPI.Range
Public enabled As Boolean
End Structure
Structure Pwq
Public conditions() As PS3000AAPI.PwqConditions
Public nConditions As Short
Public direction As PS3000AAPI.ThresholdDirection
Public lower As UInt32
Public upper As UInt32
Public type As PS3000AAPI.PulseWidthType
Public Sub New(conditions() As PS3000AAPI.PwqConditions, _
nConditions As Short, _
direction As PS3000AAPI.ThresholdDirection, _
lower As UInt32, upper As UInt32, _
type As PS3000AAPI.PulseWidthType)
Me.conditions = conditions
Me.nConditions = nConditions
Me.direction = direction
Me.lower = lower
Me.upper = upper
Me.type = type
End Sub
End Structure
Class PS3000AConsole
Private ReadOnly _handle As Short
Public Const BUFFER_SIZE As Int32 = 4096 '1024
Public Const MAX_CHANNELS As Int32 = 4
Public Const QUAD_SCOPE As Int32 = 4
Public Const DUAL_SCOPE As Int32 = 2
Private _timebase As UInt32 = 8
Private _oversample As Short = 1
Private _scaleVoltages As Boolean = True
Private inputRanges() As UInt32 = {10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000}
Private Shared _ready As Boolean = False
Private Shared _trig As Short = 0
Private Shared _trigAt As UInt32 = 0
Private Shared _sampleCount As Int32 = 0
Private Shared _startIndex As UInt32 = 0
Private Shared _autoStop As Boolean
Private _channelSettings() As ChannelSettings
Private _channelCount As Int32
Private _firstRange As PS3000AAPI.Range
Private _lastRange As PS3000AAPI.Range
Private _digitalPorts As Int32
Private _callbackDelegate As PS3000AAPI.ps3000aBlockReady
Private StreamFile As String = "stream.txt"
Private BlockFile As String = "block.txt"
Private _minPinned As PinnedArray(Of Short)
Private _digiPinned As PinnedArray(Of Short)
Private Property minPinned(i As Integer) As PinnedArray(Of Short)
Get
Return _minPinned
End Get
Set(value As PinnedArray(Of Short))
_minPinned = value
End Set
End Property
Private Property digiPinned(i As Integer) As PinnedArray(Of Short)
Get
Return _digiPinned
End Get
Set(value As PinnedArray(Of Short))
_digiPinned = value
End Set
End Property
'/****************************************************************************
' * Callback
' * used by PS4000 data streaimng collection calls, on receipt of data.
' * used to set global flags etc checked by user routines
' ****************************************************************************/
Private Shared Sub StreamingCallback(handle As Short, _
noOfSamples As Int32, _
startIndex As UInt32, _
ov As Short, _
triggerAt As UInt32, _
triggered As Short, _
autoStop As Short, _
pVoid As IntPtr)
'// used for streaming
_sampleCount = noOfSamples
_startIndex = startIndex
_autoStop = autoStop <> 0
'// flag to say done reading data
_ready = True
'// flags to show if & where a trigger has occurred
_trig = triggered
_trigAt = triggerAt
End Sub
'/****************************************************************************
' * Callback
' * used by PS4000 data block collection calls, on receipt of data.
' * used to set global flags etc checked by user routines
' ****************************************************************************/
Private Shared Sub BlockCallback(handle As Short, status As Short, pVoid As IntPtr)
'// flag to say done reading data
If status <> CShort(PS3000AAPI.PICO_CANCELLED) Then
_ready = True
End If
End Sub
'/****************************************************************************
'* SetDefaults - restore default settings
'****************************************************************************/
Private Sub SetDefaults()
Dim i As Integer
For i = 0 To _channelCount - 1 '// reset channels to most recent settings
PS3000AAPI.SetChannel(_handle, _
PS3000AAPI.Channel.ChannelA + i, _
IIf(_channelSettings(PS3000AAPI.Channel.ChannelA + i).enabled, 1, 0), _
IIf(_channelSettings(PS3000AAPI.Channel.ChannelA + i).DCcoupled, 1, 0), _
_channelSettings(PS3000AAPI.Channel.ChannelA + i).range, _
0)
Next
End Sub
'/****************************************************************************
'* SetDigitals - enable Digital Channels
'****************************************************************************/
Private Sub SetDigitals()
Dim port As PS3000AAPI.Channel
Dim status As Short
Dim logicLevel As Short
Dim logicVoltage As Single = 1.5
Dim maxLogicVoltage As Short = 5
Dim enabled As Short = 1
status = CShort(PS3000AAPI.PICO_OK)
'// Set logic threshold
logicLevel = CShort(((logicVoltage / maxLogicVoltage) * PS3000AAPI.MaxLogicLevel))
'// Enable Digital ports
For port = PS3000AAPI.Channel.PS3000A_DIGITAL_PORT0 To PS3000AAPI.Channel.PS3000A_DIGITAL_PORT2 - 1
status = PS3000AAPI.SetDigitalPort(_handle, port, enabled, logicLevel)
Next
Console.WriteLine(IIf(status <> CShort(PS3000AAPI.PICO_OK), "SetDigitals:PS3000AAPI.SetDigitalPort Status = 0x{0:X6}", ""), status)
End Sub
'/****************************************************************************
'* DisableDigital - disable Digital Channels
'****************************************************************************/
Private Sub DisableDigital()
Dim port As PS3000AAPI.Channel
Dim status As Short
status = CShort(PS3000AAPI.PICO_OK)
'// Disable Digital ports
For port = PS3000AAPI.Channel.PS3000A_DIGITAL_PORT0 To PS3000AAPI.Channel.PS3000A_DIGITAL_PORT1
status = PS3000AAPI.SetDigitalPort(_handle, port, 0, 0)
Next
Console.WriteLine(IIf(status <> CShort(PS3000AAPI.PICO_OK), "DisableDigital:PS3000AAPI.SetDigitalPort Status = 0x{0:X6}", ""), status)
End Sub
'/****************************************************************************
'* DisableAnalogue - disable analogue Channels
'****************************************************************************/
Private Sub DisableAnalogue()
Dim status As Short
Dim i As Integer
status = CShort(PS3000AAPI.PICO_OK)
'// Disable analogue ports
For i = 0 To _channelCount - 1
status = PS3000AAPI.SetChannel(_handle, PS3000AAPI.Channel.ChannelA + i, 0, 0, 0, 0)
Next
Console.WriteLine(IIf(status <> CShort(PS3000AAPI.PICO_OK), "DisableAnalogue:PS3000AAPI.SetChannel Status = 0x{0:X6}", ""), status)
End Sub
'/****************************************************************************
'* adc_to_mv
'*
'* Convert an 16-bit ADC count into millivolts
'****************************************************************************/
Private Function adc_to_mv(raw As Integer, range As PS3000AAPI.Range) As Integer
Return CInt((CInt(raw) * inputRanges(range)) / PS3000AAPI.MaxValue)
End Function
'/****************************************************************************
'* mv_to_adc
'*
'* Convert a millivolt value into a 16-bit ADC count
'*
'* (useful for setting trigger thresholds)
'****************************************************************************/
Private Function mv_to_adc(mv As Short, range As PS3000AAPI.Range) As Int16
Return CType((CInt(mv) * PS3000AAPI.MaxValue) / inputRanges(range), Int16)
End Function
'/****************************************************************************
'* BlockDataHandler
'* - Used by all block data routines
'* - acquires data (user sets trigger mode before calling), displays 10 items
'* and saves all to block.txt
'* Input :
'* - text : the text to display before the display of data slice
'* - offset : the offset into the data buffer to start the display's slice.
'****************************************************************************/
Private Sub BlockDataHandler(text As String, offset As Int32, mode As PS3000AAPI.Mode)
Dim status As Short
Dim retry As Boolean
Dim sampleCount As UInt32 = BUFFER_SIZE
Dim minPinned() As PinnedArray(Of Short) = New PinnedArray(Of Short)(_channelCount) {}
Dim maxPinned() As PinnedArray(Of Short) = New PinnedArray(Of Short)(_channelCount) {}
Dim digiPinned() As PinnedArray(Of Short) = New PinnedArray(Of Short)(_digitalPorts) {}
Dim timeIndisposed As Integer
If mode = PS3000AAPI.Mode.ANALOGUE Or mode = PS3000AAPI.Mode.MIXED Then
Dim i As Integer
For i = 0 To _channelCount - 1
Dim minBuffers(sampleCount) As Short
Dim maxBuffers(sampleCount) As Short
minPinned(i) = New PinnedArray(Of Short)(minBuffers)
maxPinned(i) = New PinnedArray(Of Short)(maxBuffers)
status = PS3000AAPI.SetDataBuffers(_handle, CType(i, PS3000AAPI.Channel), maxBuffers, minBuffers, CInt(sampleCount), 0, PS3000AAPI.RatioMode.None)
Console.WriteLine(IIf(status <> CShort(PS3000AAPI.PICO_OK), "BlockDataHandler:PS3000AAPI.SetDataBuffers Channel {0} Status = 0x{1:X6}", ""), Chr(Asc("A") + i), status)
Next
End If
If mode = PS3000AAPI.Mode.DIGITAL Or mode = PS3000AAPI.Mode.MIXED Then
Dim i As Integer
For i = 0 To _digitalPorts - 1
Dim digiBuffer(sampleCount) As Short
digiPinned(i) = New PinnedArray(Of Short)(digiBuffer)
status = PS3000AAPI.SetDataBuffer(_handle, i + PS3000AAPI.Channel.PS3000A_DIGITAL_PORT0, digiBuffer, CInt(sampleCount), 0, PS3000AAPI.RatioMode.None)
Console.WriteLine(IIf(status <> CShort(PS3000AAPI.PICO_OK), "BlockDataHandler:PS3000AAPI.SetDataBuffer {0} Status = 0x{1,0:X6}", ""), i + PS3000AAPI.Channel.PS3000A_DIGITAL_PORT0, status)
Next
End If
'/* find the maximum number of samples, the time interval (in timeUnits),
'* the most suitable time units, and the maximum _oversample at the current _timebase*/
Dim timeInterval As Integer
Dim maxSamples As Integer
While (PS3000AAPI.GetTimebase(_handle, _timebase, CInt(sampleCount), timeInterval, _oversample, maxSamples, 0) <> 0)
Console.WriteLine("Selected timebase {0} could not be used", _timebase)
_timebase += 1
End While
Console.Write("Timebase: {0}", _timebase)
Console.WriteLine(vbTab & "oversample:{0}", _oversample)
'/* Start it collecting, then wait for completion*/
_ready = False
_callbackDelegate = AddressOf BlockCallback
Do
retry = False
status = PS3000AAPI.RunBlock(_handle, 0, CInt(sampleCount), _timebase, _oversample, timeIndisposed, 0, _callbackDelegate, IntPtr.Zero)
If status = CShort(PS3000AAPI.PICO_POWER_SUPPLY_CONNECTED) Or status = CShort(PS3000AAPI.PICO_POWER_SUPPLY_NOT_CONNECTED) Or status = CShort(PS3000AAPI.PICO_POWER_SUPPLY_UNDERVOLTAGE) Then
status = PowerSourceSwitch(_handle, status)
retry = True
Else
Console.WriteLine(IIf(status <> CShort(PS3000AAPI.PICO_OK), "BlockDataHandler:PS3000AAPI.RunBlock Status = 0x{0:X6}", ""), status)
End If
Loop While retry
Console.WriteLine("Waiting for data...Press a key to abort")
While Not _ready And Not Console.KeyAvailable
Thread.Sleep(100)
End While
If Console.KeyAvailable Then
Console.ReadKey(True) ' // clear the key
End If
PS3000AAPI.StopUnit(_handle)
If _ready Then
Dim overflow As Short
status = PS3000AAPI.GetValues(_handle, 0, sampleCount, 1, PS3000AAPI.RatioMode.None, 0, overflow)
If status = CShort(PS3000AAPI.PICO_OK) Then
'/* Print out the first 10 readings, converting the readings to mV if required */
Console.WriteLine(text)
Dim ch As Integer
If mode = PS3000AAPI.Mode.ANALOGUE Or mode = PS3000AAPI.Mode.MIXED Then
Console.WriteLine("Value {0}", IIf(_scaleVoltages, "mV", "ADC Counts"))
For ch = 0 To _channelCount - 1
If _channelSettings(ch).enabled Then
Console.Write("Channel{0} ", Chr(Asc("A") + ch))
End If
Next
End If
If mode = PS3000AAPI.Mode.DIGITAL Or mode = PS3000AAPI.Mode.MIXED Then
Console.Write("DIGITAL VALUE")
End If
Console.WriteLine()
For i = offset To offset + 10 - 1
If mode = PS3000AAPI.Mode.ANALOGUE Or mode = PS3000AAPI.Mode.MIXED Then
For ch = 0 To _channelCount - 1
If _channelSettings(ch).enabled Then
'// If _scaleVoltages, show mV values
'// else show ADC counts
Debug.Print(maxPinned(ch).Target(i))
Debug.Print(CInt(_channelSettings(CInt(PS3000AAPI.Channel.ChannelA + ch)).range))
Debug.Print(adc_to_mv(maxPinned(ch).Target(i), _channelSettings(PS3000AAPI.Channel.ChannelA + ch).range))
Console.Write("{0,8} ", _
IIf(_scaleVoltages, adc_to_mv(maxPinned(ch).Target(i), _
_channelSettings(PS3000AAPI.Channel.ChannelA + ch).range), _
maxPinned(ch).Target(i) _
) _
)
End If
Next
End If
If mode = PS3000AAPI.Mode.DIGITAL Or mode = PS3000AAPI.Mode.MIXED Then
Dim digiValue As Short = digiPinned(1).Target(i)
digiValue <<= 8
digiValue = digiValue Or digiPinned(0).Target(i)
Console.Write("0x{0,4:X}", digiValue.ToString("X4"))
End If
Console.WriteLine()
Next
If mode = PS3000AAPI.Mode.ANALOGUE Or mode = PS3000AAPI.Mode.MIXED Then
sampleCount = Math.Min(sampleCount, BUFFER_SIZE)
Dim writer As TextWriter = New StreamWriter(BlockFile, False)
writer.Write("For each of the {0} Channels, results shown are....", _channelCount)
writer.WriteLine()
writer.WriteLine("Time interval Maximum Aggregated value ADC Count & mV, Minimum Aggregated value ADC Count & mV")
writer.WriteLine()
For i = 0 To _channelCount - 1
If _channelSettings(i).enabled Then
writer.Write("Time Ch Max ADC Max mV Min ADC Min mV ")
End If
Next
writer.WriteLine()
For i = 0 To sampleCount - 1
For ch = 0 To _channelCount - 1
If _channelSettings(ch).enabled Then
writer.Write("{0,5} ", (i * timeInterval))
writer.Write( _
"Ch{0} {1,7} {2,7} {3,7} {4,7} ", _
Chr(Asc("A") + ch), _
maxPinned(ch).Target(i), _
adc_to_mv(maxPinned(ch).Target(i), _channelSettings(PS3000AAPI.Channel.ChannelA + ch).range), _
minPinned(ch).Target(i), _
adc_to_mv(minPinned(ch).Target(i), _channelSettings(PS3000AAPI.Channel.ChannelA + ch).range) _
)
End If
Next
writer.WriteLine()
Next
writer.Close()
End If
Else
If status = CShort(PS3000AAPI.PICO_POWER_SUPPLY_CONNECTED) Or status = CShort(PS3000AAPI.PICO_POWER_SUPPLY_NOT_CONNECTED) Or status = CShort(PS3000AAPI.PICO_POWER_SUPPLY_UNDERVOLTAGE) Then
If status = CShort(PS3000AAPI.PICO_POWER_SUPPLY_UNDERVOLTAGE) Then
status = PowerSourceSwitch(_handle, status)
Else
Console.WriteLine("Power source changed. Data collection aborted")
End If
Else
Console.WriteLine("BlockDataHandler:PS3000AAPI.GetValues Status = 0x{0:X6}", status)
End If
End If
Else
Console.WriteLine("data collection aborted")
End If
PS3000AAPI.StopUnit(_handle)
If mode = PS3000AAPI.Mode.ANALOGUE Or mode = PS3000AAPI.Mode.MIXED Then
For Each p As PinnedArray(Of Short) In minPinned
If Not IsNothing(p) Then
p.Dispose()
End If
Next
For Each p As PinnedArray(Of Short) In maxPinned
If Not IsNothing(p) Then
p.Dispose()
End If
Next
End If
If mode = PS3000AAPI.Mode.DIGITAL Or mode = PS3000AAPI.Mode.MIXED Then
For Each p As PinnedArray(Of Short) In digiPinned
If Not IsNothing(p) Then
p.Dispose()
End If
Next
End If
End Sub
'/****************************************************************************
' * RapidBlockDataHandler
' * - Used by all the CollectBlockRapid routine
' * - acquires data (user sets trigger mode before calling), displays 10 items
' * and saves all to data.txt
' * Input :
' * - nRapidCaptures : the user specified number of blocks to capture
' ****************************************************************************/
Private Sub RapidBlockDataHandler(nRapidCaptures As UShort)
Dim status As Short
Dim numChannels As Integer = _channelCount
Dim numSamples As UInt32 = BUFFER_SIZE
'// Run the rapid block capture
Dim timeIndisposed As Int32
_ready = False
_callbackDelegate = AddressOf BlockCallback
PS3000AAPI.RunBlock(_handle,
0,
CInt(numSamples),
_timebase,
_oversample,
timeIndisposed,
0,
_callbackDelegate,
IntPtr.Zero)
Console.WriteLine("Waiting for data...Press a key to abort")
While Not _ready And Not Console.KeyAvailable
Thread.Sleep(100)
End While
If Console.KeyAvailable Then
Console.ReadKey(True) '// clear the key
End If
PS3000AAPI.StopUnit(_handle)
'// Set up the data arrays and pin them
Dim values As Short()()() = New Short(nRapidCaptures)()() {}
Dim pinned(nRapidCaptures, numChannels) As PinnedArray(Of Short)
For segment As UShort = 0 To nRapidCaptures - 1
values(segment) = New Short(numChannels)() {}
For channel As Short = 0 To numChannels - 1
If _channelSettings(channel).enabled Then
ReDim values(segment)(channel)(numSamples)
pinned(segment, channel) = New PinnedArray(Of Short)(values(segment)(channel))
status = PS3000AAPI.SetDataBuffer(_handle,
CType(channel, PS3000AAPI.Channel),
values(segment)(channel),
CInt(numSamples),
segment,
0)
Console.WriteLine(IIf(status <> CShort(PS3000AAPI.PICO_OK), "RapidBlockDataHandler:PS3000AAPI.SetDataBuffer Channel {0} Status = 0x{1:X6}", ""), Chr(Asc("A") + channel), status)
Else
status = PS3000AAPI.SetDataBuffer(_handle, _
CType(channel, PS3000AAPI.Channel), _
Nothing, _
0, _
segment, _
0)
Console.WriteLine(IIf(status <> CShort(PS3000AAPI.PICO_OK), "RapidBlockDataHandler:PS3000AAPI.SetDataBuffer Channel {0} Status = 0x{1:X6}", ""), Chr(Asc("A") + channel), status)
End If
Next
Next
'// Read the data
Dim overflows As Short() = New Short(nRapidCaptures) {}
status = PS3000AAPI.GetValuesRapid(_handle, numSamples, 0, CUShort(nRapidCaptures - 1), 1, PS3000AAPI.RatioMode.None, overflows)
'/* Print out the first 10 readings, converting the readings to mV if required */
Console.WriteLine("Values in {0}", IIf(_scaleVoltages, "mV", "ADC Counts"))
For seg As Integer = 0 To nRapidCaptures - 1
Console.WriteLine("Capture {0}", seg)
For i As Integer = 0 To 10 - 1
For chan As Integer = 0 To _channelCount - 1
If (_channelSettings(chan).enabled) Then
Console.Write( _
"{0}" & vbTab, _
IIf( _
_scaleVoltages, _
adc_to_mv( _
pinned(seg, chan).Target(i), _
_channelSettings(PS3000AAPI.Channel.ChannelA + chan).range _
), _
pinned(seg, chan).Target(i) _
) _
)
'// If _scaleVoltages, show mV values
'// else show ADC counts
End If
Next
Console.WriteLine()
Next
Console.WriteLine()
Next
' // Un-pin the arrays
For Each p As PinnedArray(Of Short) In pinned
If Not IsNothing(p) Then
p.Dispose()
End If
Next
' //TODO: Do what ever is required with the data here.
End Sub
'/****************************************************************************
'* SetTrigger (Non-Digital Version)
'* this function sets all the required trigger parameters, and calls the
'* triggering functions
'****************************************************************************/
Private Function SetTrigger(channelProperties() As PS3000AAPI.TriggerChannelProperties, _
nChannelProperties As Short, _
triggerConditions() As PS3000AAPI.TriggerConditions, _
nTriggerConditions As Short, _
directions() As PS3000AAPI.ThresholdDirection, _
pwq As Pwq, _
delay As UInt32, _
auxOutputEnabled As Short, _
autoTriggerMs As Int32) As Short
Dim status As Short
If (status = PS3000AAPI.SetTriggerChannelProperties(_handle, channelProperties, nChannelProperties, auxOutputEnabled, autoTriggerMs)) <> 0 Then
Return status
End If
If (status = PS3000AAPI.SetTriggerChannelConditions(_handle, triggerConditions, nTriggerConditions)) <> 0 Then
Return status
End If
If IsNothing(directions) Then
directions = New PS3000AAPI.ThresholdDirection() {
PS3000AAPI.ThresholdDirection.None,
PS3000AAPI.ThresholdDirection.None,
PS3000AAPI.ThresholdDirection.None,
PS3000AAPI.ThresholdDirection.None,
PS3000AAPI.ThresholdDirection.None,
PS3000AAPI.ThresholdDirection.None
}
End If
status = PS3000AAPI.SetTriggerChannelDirections(_handle, _
directions(CInt(PS3000AAPI.Channel.ChannelA)), _
directions(CInt(PS3000AAPI.Channel.ChannelB)), _
directions(CInt(PS3000AAPI.Channel.ChannelC)), _
directions(CInt(PS3000AAPI.Channel.ChannelD)), _
directions(CInt(PS3000AAPI.Channel.External)), _
directions(CInt(PS3000AAPI.Channel.Aux)))
If status <> 0 Then
Return status
End If
status = PS3000AAPI.SetTriggerDelay(_handle, delay)
If status <> 0 Then
Return status
End If
If IsNothing(pwq) Then
pwq = New Pwq(Nothing, 0, PS3000AAPI.ThresholdDirection.None, 0, 0, PS3000AAPI.PulseWidthType.None)
End If
status = PS3000AAPI.SetPulseWidthQualifier(_handle, pwq.conditions,
pwq.nConditions, pwq.direction,
pwq.lower, pwq.upper, pwq.type)
Return status
End Function
'/****************************************************************************
'* SetTrigger
'* this overloaded version of SetTrigger includes digital parameters
'****************************************************************************/
Private Function SetTrigger(channelProperties() As PS3000AAPI.TriggerChannelProperties, _
nChannelProperties As Short, _
triggerConditions() As PS3000AAPI.TriggerConditionsV2, _
nTriggerConditions As Short, _
directions() As PS3000AAPI.ThresholdDirection, _
pwq As Pwq, _
delay As UInt32, _
auxOutputEnabled As Short, _
autoTriggerMs As Int32, _
digitalDirections() As PS3000AAPI.DigitalChannelDirections, _
nDigitalDirections As Short) As Short
Dim status As Short
If (status = PS3000AAPI.SetTriggerChannelProperties(_handle, channelProperties, nChannelProperties, auxOutputEnabled, autoTriggerMs)) <> 0 Then
Return status
End If
If (status = PS3000AAPI.SetTriggerChannelConditionsV2(_handle, triggerConditions, nTriggerConditions)) <> 0 Then
Return status
End If
If IsNothing(directions) Then
directions = New PS3000AAPI.ThresholdDirection() {
PS3000AAPI.ThresholdDirection.None,
PS3000AAPI.ThresholdDirection.None,
PS3000AAPI.ThresholdDirection.None,
PS3000AAPI.ThresholdDirection.None,
PS3000AAPI.ThresholdDirection.None,
PS3000AAPI.ThresholdDirection.None
}
End If
If ((status = PS3000AAPI.SetTriggerChannelDirections(_handle,
directions(CInt(PS3000AAPI.Channel.ChannelA)),
directions(CInt(PS3000AAPI.Channel.ChannelB)),
directions(CInt(PS3000AAPI.Channel.ChannelC)),
directions(CInt(PS3000AAPI.Channel.ChannelD)),
directions(CInt(PS3000AAPI.Channel.External)),
directions(CInt(PS3000AAPI.Channel.Aux)))) <> 0) Then
Return status
End If
If (status = PS3000AAPI.SetTriggerDelay(_handle, delay)) <> 0 Then
Return status
End If
If IsNothing(pwq) Then
pwq = New Pwq(Nothing, 0, PS3000AAPI.ThresholdDirection.None, 0, 0, PS3000AAPI.PulseWidthType.None)
End If
status = PS3000AAPI.SetPulseWidthQualifier(_handle, _
pwq.conditions, _
pwq.nConditions, _
pwq.direction, _
pwq.lower, _
pwq.upper, _
pwq.type)
If (_digitalPorts > 0) Then
If ((status = PS3000AAPI.SetTriggerDigitalPort(_handle, digitalDirections, nDigitalDirections)) <> 0) Then
Return status
End If
End If
Return status
End Function
'/****************************************************************************
'* CollectBlockImmediate
'* this function demonstrates how to collect a single block of data
'* from the unit (start collecting immediately)
'****************************************************************************/
Private Sub CollectBlockImmediate()
Console.WriteLine("Collect block immediate...")
Console.WriteLine("Press a key to start")
WaitForKey()
SetDefaults()
'/* Trigger disabled */
SetTrigger({}, 0, {}, 0, {}, Nothing, 0, 0, 0)
BlockDataHandler("First 10 readings", 0, PS3000AAPI.Mode.ANALOGUE)
End Sub
'/****************************************************************************
'* CollectBlockRapid
'* this function demonstrates how to collect blocks of data
'* using the RapidCapture function
'****************************************************************************/
Private Sub CollectBlockRapid()
Dim numRapidCaptures As UShort = 1
Dim valid As Boolean = False
Console.WriteLine("Collect rapid block...")
Console.WriteLine("Specify number of captures:")
Do
Try
numRapidCaptures = UShort.Parse(Console.ReadLine())
valid = True
Catch ex As Exception
valid = False
Console.WriteLine("Enter numeric values only")
End Try
Loop While (PS3000AAPI.SetNoOfRapidCaptures(_handle, numRapidCaptures) > 0 Or Not valid)
Dim maxSamples As Integer
PS3000AAPI.MemorySegments(_handle, numRapidCaptures, maxSamples)
Console.WriteLine("Collecting {0} rapid blocks. Press a key to start", numRapidCaptures)
WaitForKey()
SetDefaults()
' /* Trigger is optional, disable it for now */
SetTrigger({}, 0, {}, 0, {}, Nothing, 0, 0, 0)
RapidBlockDataHandler(numRapidCaptures)
End Sub
' /****************************************************************************
'* CollectBlockTriggered
'* this function demonstrates how to collect a single block of data from the
'* unit, when a trigger event occurs.
'****************************************************************************/
Private Sub CollectBlockTriggered()
Dim triggerVoltage As Short = mv_to_adc(inputRanges(_channelSettings(PS3000AAPI.Channel.ChannelA).range) / 5, _channelSettings(PS3000AAPI.Channel.ChannelA).range) '// ChannelInfo stores ADC counts
Dim sourceDetails() As PS3000AAPI.TriggerChannelProperties = New PS3000AAPI.TriggerChannelProperties() {
New PS3000AAPI.TriggerChannelProperties(triggerVoltage, _
256 * 10, _
triggerVoltage, _
256 * 10, _
PS3000AAPI.Channel.ChannelA, _
PS3000AAPI.ThresholdMode.Level)
}
Dim conditions() As PS3000AAPI.TriggerConditions = New PS3000AAPI.TriggerConditions() {
New PS3000AAPI.TriggerConditions( _
PS3000AAPI.TriggerState.TrigTrue, _
PS3000AAPI.TriggerState.DontCare, _
PS3000AAPI.TriggerState.DontCare, _
PS3000AAPI.TriggerState.DontCare, _
PS3000AAPI.TriggerState.DontCare, _
PS3000AAPI.TriggerState.DontCare, _
PS3000AAPI.TriggerState.DontCare)
}
Dim directions() As PS3000AAPI.ThresholdDirection = New PS3000AAPI.ThresholdDirection() {
PS3000AAPI.ThresholdDirection.Rising,
PS3000AAPI.ThresholdDirection.None,
PS3000AAPI.ThresholdDirection.None,
PS3000AAPI.ThresholdDirection.None,
PS3000AAPI.ThresholdDirection.None,
PS3000AAPI.ThresholdDirection.None
}
Console.WriteLine("Collect block triggered...")
Console.Write("Collects when value rises past {0}", IIf(_scaleVoltages, _
adc_to_mv(sourceDetails(0).ThresholdMajor, _
CInt(_channelSettings(CInt(PS3000AAPI.Channel.ChannelA)).range)) _
, sourceDetails(0).ThresholdMajor) _
)
Console.WriteLine("{0}", IIf(_scaleVoltages, "mV", " ADC Counts"))
Console.WriteLine("Press a key to start...")
WaitForKey()
SetDefaults()
'/* Trigger enabled
' * Rising edge
' * Threshold = 1000mV */
SetTrigger(sourceDetails, 1, conditions, 1, directions, Nothing, 0, 0, 0)
BlockDataHandler("Ten readings after trigger", 0, PS3000AAPI.Mode.ANALOGUE)
End Sub
'/****************************************************************************
'* Initialise unit' structure with Variant specific defaults
'****************************************************************************/
Private Sub GetDeviceInfo()
Dim description() As String = {
"Driver Version ",
"USB Version ",
"Hardware Version ",
"Variant Info ",
"Serial ",
"Cal Date ",
"Kernel Ver ",
"Digital Hardware ",
"Analogue Hardware "
}
Dim line As System.Text.StringBuilder = New System.Text.StringBuilder(80)
If (_handle >= 0) Then
For i As Integer = 0 To 9 - 1
Dim requiredSize As Short
PS3000AAPI.GetUnitInfo(_handle, line, 80, requiredSize, i)
If i = 3 Then
If line.ToString().Contains("340") Then '// PS340XA/B device
_channelCount = QUAD_SCOPE
Else
_channelCount = DUAL_SCOPE
End If
End If
If i = 3 Then
If line.ToString().EndsWith("MSO") Then
_digitalPorts = 2
Else
_digitalPorts = 0
End If
End If
Console.WriteLine("{0}: {1}", description(i), line)
Next
End If
End Sub
'/****************************************************************************
' * Select input voltage ranges for channels A and B
' ****************************************************************************/
Private Sub SetVoltages()
Dim valid As Boolean = False
Dim count As Short = 0
Dim i As Integer
' /* See what ranges are available... */
For i = CInt(_firstRange) To CInt(_lastRange)
Console.WriteLine("{0} . {1} mV", i, inputRanges(i))
Next
Do
'/* Ask the user to select a range */
Console.WriteLine("Specify voltage range ({0}..{1})", _firstRange, _lastRange)
Console.WriteLine("99 - switches channel off")
Dim ch As Integer
For ch = 0 To _channelCount - 1
Console.WriteLine("")
Dim range As UInt32 = 8
Do
Try
Console.WriteLine("Channel: {0}", Chr(Asc("A") + ch))
range = UInteger.Parse(Console.ReadLine())
valid = True
Catch ex As Exception
valid = False
Console.WriteLine("Enter numeric values only")
End Try
Loop While ( _
(range <> 99 _
And (range < CType(_firstRange, UInt32) Or range > CType(_lastRange, UInt32)) _
Or Not valid) _
)
If range <> 99 Then
_channelSettings(ch).range = CType(range, PS3000AAPI.Range)
Console.WriteLine(" = {0} mV", inputRanges(range))
_channelSettings(ch).enabled = True
count += 1
Else
Console.WriteLine("Channel Switched off")
_channelSettings(ch).enabled = False
_channelSettings(ch).range = PS3000AAPI.Range.Range_MAX_RANGE - 1
End If
Next
Console.Write(IIf(count = 0, "*** At least 1 channel must be enabled *** ", ""))
Loop While (count = 0) '// must have at least one channel enabled
SetDefaults() '// Set defaults now, so that if all but 1 channels get switched off, timebase updates to timebase 0 will work
End Sub
'/****************************************************************************
' *
' * Select _timebase, set _oversample to on and time units as nano seconds
' *
' ****************************************************************************/
Private Sub SetTimebase()
Dim timeInterval As Integer
Dim maxSamples As Integer
Dim valid As Boolean = False
Console.WriteLine("Specify timebase")
Do
Try
_timebase = UInteger.Parse(Console.ReadLine())
valid = True
Catch ex As Exception
valid = False
Console.WriteLine("Enter numeric values only")
End Try
Loop While (Not valid)
While (PS3000AAPI.GetTimebase(_handle, _timebase, BUFFER_SIZE, timeInterval, 1, maxSamples, 0) <> 0)
Console.WriteLine("Selected timebase {0} could not be used", _timebase)
_timebase += 1
End While
Console.WriteLine("Timebase {0} - {1} ns", _timebase, timeInterval)
_oversample = 1
End Sub
'/****************************************************************************
'* Stream Data Handler
'* - Used by the two stream data examples - untriggered and triggered
'* Inputs:
'* - preTrigger - the number of samples in the pre-trigger phase
'* (0 if no trigger has been set)
'* mode - ANALOGUE,
'***************************************************************************/
Private Sub StreamDataHandler(preTrigger As UInt32, mode As PS3000AAPI.Mode)
Dim sampleCount As UInteger = BUFFER_SIZE * 10 '/* *10 is to make sure buffer large enough */
Dim minBuffers()() As Short = New Short(_channelCount)() {}
Dim maxBuffers()() As Short = New Short(_channelCount)() {}
Dim minPinned() As PinnedArray(Of Short) = New PinnedArray(Of Short)(_channelCount) {}
Dim maxPinned() As PinnedArray(Of Short) = New PinnedArray(Of Short)(_channelCount) {}
Dim digiBuffersA()() As Short = New Short(_digitalPorts)() {}
Dim digiBuffersB()() As Short = New Short(_digitalPorts)() {}
Dim digiPinnedA() As PinnedArray(Of Short) = New PinnedArray(Of Short)(_digitalPorts) {}
Dim digiPinnedB() As PinnedArray(Of Short) = New PinnedArray(Of Short)(_digitalPorts) {}
Dim totalsamples As UInt32 = 0
Dim triggeredAt As UInt32 = 0
Dim status As Short
Dim downsampleRatio As UInt32
Dim timeUnits As PS3000AAPI.ReportedTimeUnits
Dim sampleInterval As UInt32
Dim ratioMode As PS3000AAPI.RatioMode
Dim postTrigger As UInt32
Dim autoStop As Boolean
Dim retry As Boolean
Dim powerChange As Boolean = False
sampleInterval = 1
status = CShort(PS3000AAPI.PICO_OK)
Select Case mode
Case PS3000AAPI.Mode.ANALOGUE
For channel As Integer = 0 To _channelCount - 1 '// create data buffers
minBuffers(channel) = New Short(sampleCount) {}
maxBuffers(channel) = New Short(sampleCount) {}
minPinned(channel) = New PinnedArray(Of Short)(minBuffers(channel))
maxPinned(channel) = New PinnedArray(Of Short)(maxBuffers(channel))
status = PS3000AAPI.SetDataBuffers(_handle, CType(channel, PS3000AAPI.Channel), minBuffers(channel), maxBuffers(channel), CInt(sampleCount), 0, PS3000AAPI.RatioMode.Aggregate)
Console.Write(IIf(status <> CShort(PS3000AAPI.PICO_OK), "StreamDataHandler:PS3000AAPI.SetDataBuffers Channel {0} Status = 0x{1:X6}", ""), Chr(Asc("A") + channel), status)
Next
downsampleRatio = 1000
timeUnits = PS3000AAPI.ReportedTimeUnits.MicroSeconds
sampleInterval = 1
ratioMode = PS3000AAPI.RatioMode.Aggregate
postTrigger = 1000000
autoStop = True
sampleInterval = 1