1+ %% PicoScope 6000 Series (A API) Instrument Driver Oscilloscope Streaming Data Capture Example
2+ % This is an example of an instrument control session using a device
3+ % object. The instrument control session comprises all the steps you
4+ % are likely to take when communicating with your instrument.
5+ %
6+ % These steps are:
7+ %
8+ % # Create a device object
9+ % # Connect to the instrument
10+ % # Configure properties
11+ % # Invoke functions
12+ % # Disconnect from the instrument
13+ %
14+ % To run the instrument control session, type the name of the file,
15+ % PS6000A_ID_Streaming_Example, at the MATLAB command prompt.
16+ %
17+ % The file, PS6000A_ID_STREAMING_EXAMPLE.M must be on your MATLAB PATH. For
18+ % additional information on setting your MATLAB PATH, type 'help addpath'
19+ % at the MATLAB command prompt.
20+ %
21+ % *Example:*
22+ % PS6000A_ID_Streaming_Example;
23+ %
24+ % *Description:*
25+ % Demonstrates how to set properties and call functions in order to
26+ % stream data from a PicoScope 6000 (A API) Series Oscilloscope.
27+ %
28+ % *See also:* <matlab:doc('icdevice') |icdevice|> | <matlab:doc('instrument/invoke') |invoke|>
29+ %
30+ % *Copyright:* © 2021 Pico Technology Ltd. See LICENSE file for terms.
31+
32+ %% Suggested Input Settings
33+
34+ %% Clear Command Window and Close Figures
35+
36+ clc ;
37+ close all ;
38+
39+ %% Load Configuration Information
40+
41+ [ps6000aStructs , ps6000aEnumInfo ]=PS6000aSetConfig();
42+
43+ %% Device Connection
44+
45+ % Check if an Instrument session using the device object 'ps6000DeviceObj'
46+ % is still open, and if so, disconnect if the User chooses 'Yes' when prompted.
47+ if (exist(' ps6000aDeviceObj' , ' var' ) && ps6000aDeviceObj .isvalid && strcmp(ps6000aDeviceObj .status , ' open' ))
48+
49+ openDevice = questionDialog([' Device object ps6000aDeviceObj has an open connection. ' ...
50+ ' Do you wish to close the connection and continue?' ], ...
51+ ' Device Object Connection Open' );
52+
53+ if (openDevice == PicoConstants .TRUE )
54+
55+ % Close connection to device
56+ disconnect(ps6000aDeviceObj );
57+ delete(ps6000aDeviceObj );
58+
59+ else
60+
61+ % Exit script if User selects 'No'
62+ return ;
63+
64+ end
65+
66+ end
67+
68+ %% Create a device object.
69+ % The serial number can be specified as a second input parameter.
70+
71+ ps6000aDeviceObj = icdevice(' picotech_ps6000a_generic.mdd' ,' ' );
72+
73+ %% Connect scope
74+
75+ connect(ps6000aDeviceObj )
76+
77+ %% Set Device Resolution
78+
79+ resolution = ps6000aEnumInfo .enPicoDeviceResolution .PICO_DR_10BIT ;
80+
81+ [status .setResolution ] = invoke(ps6000aDeviceObj , ' ps6000aSetDeviceResolution' , resolution );
82+
83+ disp(' Device Resolution set to 10 bits' )
84+
85+ %% Enable Channel A + B
86+ % Disable other channels
87+ for i = (0 : 7 )
88+ try
89+ [status .setChannelOff ] = invoke(ps6000aDeviceObj , ' ps6000aSetChannelOff' , i );
90+ catch
91+
92+ end
93+ end
94+
95+ for j = (128 : 1 : 131 )
96+ try
97+ [status .turnDigitalPortOff ] = invoke(ps6000aDeviceObj , ' ps6000aDigitalPortOff' ,j );
98+ catch
99+ end
100+ end
101+
102+ % Enable channels A + B with +-5 V range with DC coupling and full bandwidth
103+
104+ channelA = ps6000aEnumInfo .enPicoChannel .PICO_CHANNEL_A ;
105+ couplingDC = ps6000aEnumInfo .enPicoCoupling .PICO_DC ;
106+ range = ps6000aEnumInfo .enPicoConnectProbeRange .PICO_X1_PROBE_5V ;
107+ bandwidth = ps6000aEnumInfo .enPicoBandwidthLimiter .PICO_BW_FULL ;
108+
109+
110+ [status .setChannelOn .A ] = invoke(ps6000aDeviceObj , ' ps6000aSetChannelOn' , channelA , couplingDC , range , 0 , bandwidth );
111+
112+ disp(' Channels A set' )
113+
114+ %% Set Simple Trigger
115+
116+ enable = 1 ;
117+ source = channelA ;
118+ threshold = 1000 ; % mV
119+ direction = ps6000aEnumInfo .enPicoThresholdDirection .PICO_RISING ;
120+ delay = 0 ;
121+ autoTriggerMicroSeconds = 1000000 ; % us
122+
123+ [status .setSimpleTrigger ] = invoke(ps6000aDeviceObj , ' ps6000aSetSimpleTrigger' , enable , source , threshold , direction ,...
124+ delay , autoTriggerMicroSeconds );
125+
126+ disp(' Simple Trigger set' )
127+
128+ %% Set number of samples to be collected
129+
130+ numPreTriggerSamples = 100000 ;
131+ numPostTriggerSamples = 900000 ;
132+ totalSamples = numPreTriggerSamples + numPostTriggerSamples ;
133+
134+ %% Create Buffers
135+
136+ bufferA = zeros(totalSamples , 1 , ' int16' );
137+
138+ maxBuffers = 10 ;
139+
140+ for i = (1 : maxBuffers )
141+ pBufferA(i ,1 ) = libpointer(' int16Ptr' , bufferA );
142+ end
143+
144+
145+ dataType = ps6000aEnumInfo .enPicoDataType .PICO_INT16_T ;
146+ waveform = 0 ;
147+ downSampleRatioMode = ps6000aEnumInfo .enPicoRatioMode .PICO_RATIO_MODE_AVERAGE ;
148+ actionA = bitor(ps6000aEnumInfo .enPicoAction .PICO_CLEAR_ALL , ps6000aEnumInfo .enPicoAction .PICO_ADD );
149+ actionB = ps6000aEnumInfo .enPicoAction .PICO_ADD ;
150+
151+
152+ [status .setBufferA ] = invoke(ps6000aDeviceObj , ' ps6000aSetDataBuffer' , channelA , pBufferA(1 ,1 ), ...
153+ totalSamples , dataType , waveform , downSampleRatioMode , actionA );
154+
155+
156+ %% Run Block Capture
157+
158+ sampleInterval = 1 ;
159+ sampleIntervalTimeUnits = ps6000aEnumInfo .enPicoTimeUnits .PICO_US ;
160+ autoStop = 1 ;
161+ downSampleRatio = 1 ;
162+
163+ disp(' Streaming starting...' )
164+
165+ [status .runStreaming ] = invoke(ps6000aDeviceObj , ' ps6000aRunStreaming' , sampleInterval , sampleIntervalTimeUnits ,...
166+ numPreTriggerSamples , numPostTriggerSamples , autoStop , downSampleRatio , downSampleRatioMode );
167+ %%
168+
169+ streamData = ps6000aStructs .tPicoStreamingDataInfo .members ;
170+
171+ streamData.bufferIndex_ = 0 ;
172+ streamData.channel_ = ps6000aEnumInfo .enPicoChannel .PICO_CHANNEL_A ;
173+ streamData.mode_ = ps6000aEnumInfo .enPicoRatioMode .PICO_RATIO_MODE_AVERAGE ;
174+ streamData.noOfSamples_ = 0 ;
175+ streamData.overflow_ = 0 ;
176+ streamData.startIndex_ = 0 ;
177+ streamData.type_ = ps6000aEnumInfo .enPicoDataType .PICO_INT16_T ;
178+
179+ pStreamData = libpointer(' tPicoStreamingDataInfoPtr' ,streamData );
180+
181+ streamTrigger = ps6000aStructs .tPicoStreamingDataTriggerInfo .members ;
182+ streamTrigger.triggerAt_=0 ;
183+ streamTrigger.triggered_=0 ;
184+ streamTrigger.autoStop_=0 ;
185+
186+ pStreamTrigger = libpointer(' tPicoStreamingDataTriggerInfoPtr' ,streamTrigger );
187+ %%
188+ i= 1
189+ while i < maxBuffers
190+
191+ pause(0.25 )
192+
193+ [status .getStreamingLatestValues ] = invoke(ps6000aDeviceObj , ' ps6000aGetStreamingLatestValues' , pStreamData , 1 , pStreamTrigger );
194+
195+ if status .getStreamingLatestValues ~= 0
196+ bufferA(: ,i ) = pBufferA(i ,1 ).Value;
197+ i= i + 1
198+ [status .setBufferA ] = invoke(ps6000aDeviceObj , ' ps6000aSetDataBuffer' , channelA , pBufferA(i ,1 ), ...
199+ totalSamples , dataType , waveform , downSampleRatioMode , actionB );
200+ end
201+
202+ end
203+
204+ disp(' Streaming finished' )
205+
206+ %% Convert Data from ADC counts to mV
207+
208+ pMinValue = libpointer(' int16Ptr' ,0 );
209+ pMaxValue = libpointer(' int16Ptr' ,0 );
210+
211+ [status .getAdcLimits ] = invoke(ps6000aDeviceObj , ' ps6000aGetAdcLimits' , resolution , pMinValue , pMaxValue );
212+
213+ minValue = pMinValue .Value ;
214+ maxValue = pMaxValue .Value ;
215+
216+ voltageRange = 5000 ; % mV
217+
218+ bufferA = pBufferA .Value ;
219+
220+ bufferA = adc2mv(bufferA ,voltageRange ,double(maxValue ));
221+
222+ disp(' Data converted to mV' )
223+
224+ %% Plot Collected Data
225+
226+ samplesCollected= length(bufferA(: ,1 ));
227+ maxTime = (double(samplesCollected ) * sampleInterval );
228+ timeUS = linspace(0 ,maxTime ,samplesCollected );
229+
230+ figure(1 )
231+ plot(timeUS ,bufferA(: ,1 ));
232+ hold on
233+ ylabel(' Voltage (mV)' );
234+ xlabel(' Time (us)' );
235+ hold off
236+
237+ %% Disconnect scope
238+
239+ disconnect(ps6000aDeviceObj );
240+
241+ %%
242+
243+ delete(ps6000aDeviceObj );
0 commit comments