Skip to content

Commit ddfb417

Browse files
author
neil.hamilton
committed
Create ps4000aExamples/ps4000aStreamingExample.py
1 parent e018b5a commit ddfb417

File tree

1 file changed

+208
-0
lines changed

1 file changed

+208
-0
lines changed
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
#
2+
# Copyright (C) 2018-2019 Pico Technology Ltd. See LICENSE file for terms.
3+
#
4+
# PS2000 Series (A API) STREAMING MODE EXAMPLE
5+
# This example demonstrates how to call the ps4000A driver API functions in order to open a device, setup 2 channels and collects streamed data (1 buffer).
6+
# This data is then plotted as mV against time in ns.
7+
8+
import ctypes
9+
import numpy as np
10+
from picosdk.ps4000a import ps4000a as ps
11+
import matplotlib.pyplot as plt
12+
from picosdk.functions import adc2mV, assert_pico_ok
13+
import time
14+
15+
# Create chandle and status ready for use
16+
chandle = ctypes.c_int16()
17+
status = {}
18+
19+
# Open PicoScope 2000 Series device
20+
# Returns handle to chandle for use in future API functions
21+
status["openunit"] = ps.ps4000aOpenUnit(ctypes.byref(chandle), None)
22+
23+
try:
24+
assert_pico_ok(status["openunit"])
25+
except:
26+
27+
powerStatus = status["openunit"]
28+
29+
if powerStatus == 286:
30+
status["changePowerSource"] = ps.ps4000aChangePowerSource(chandle, powerStatus)
31+
else:
32+
raise
33+
34+
assert_pico_ok(status["changePowerSource"])
35+
36+
37+
enabled = 1
38+
disabled = 0
39+
analogue_offset = 0.0
40+
41+
# Set up channel A
42+
# handle = chandle
43+
# channel = PS4000A_CHANNEL_A = 0
44+
# enabled = 1
45+
# coupling type = PS4000A_DC = 1
46+
# range = PS4000A_2V = 7
47+
# analogue offset = 0 V
48+
channel_range = 7
49+
status["setChA"] = ps.ps4000aSetChannel(chandle,
50+
ps.PS4000A_CHANNEL['PS4000A_CHANNEL_A'],
51+
enabled,
52+
ps.PS4000A_COUPLING['PS4000A_DC'],
53+
channel_range,
54+
analogue_offset)
55+
assert_pico_ok(status["setChA"])
56+
57+
# Set up channel B
58+
# handle = chandle
59+
# channel = PS4000A_CHANNEL_B = 1
60+
# enabled = 1
61+
# coupling type = PS4000A_DC = 1
62+
# range = PS4000A_2V = 7
63+
# analogue offset = 0 V
64+
status["setChB"] = ps.ps4000aSetChannel(chandle,
65+
ps.PS4000A_CHANNEL['PS4000A_CHANNEL_B'],
66+
enabled,
67+
ps.PS4000A_COUPLING['PS4000A_DC'],
68+
channel_range,
69+
analogue_offset)
70+
assert_pico_ok(status["setChB"])
71+
72+
# Size of capture
73+
sizeOfOneBuffer = 500
74+
numBuffersToCapture = 10
75+
76+
totalSamples = sizeOfOneBuffer * numBuffersToCapture
77+
78+
# Create buffers ready for assigning pointers for data collection
79+
bufferAMax = np.zeros(shape=sizeOfOneBuffer, dtype=np.int16)
80+
bufferBMax = np.zeros(shape=sizeOfOneBuffer, dtype=np.int16)
81+
82+
memory_segment = 0
83+
84+
# Set data buffer location for data collection from channel A
85+
# handle = chandle
86+
# source = PS4000A_CHANNEL_A = 0
87+
# pointer to buffer max = ctypes.byref(bufferAMax)
88+
# pointer to buffer min = ctypes.byref(bufferAMin)
89+
# buffer length = maxSamples
90+
# segment index = 0
91+
# ratio mode = PS4000A_RATIO_MODE_NONE = 0
92+
status["setDataBuffersA"] = ps.ps4000aSetDataBuffers(chandle,
93+
ps.PS4000A_CHANNEL['PS4000A_CHANNEL_A'],
94+
bufferAMax.ctypes.data_as(ctypes.POINTER(ctypes.c_int16)),
95+
None,
96+
sizeOfOneBuffer,
97+
memory_segment,
98+
ps.PS4000A_RATIO_MODE['PS4000A_RATIO_MODE_NONE'])
99+
assert_pico_ok(status["setDataBuffersA"])
100+
101+
# Set data buffer location for data collection from channel B
102+
# handle = chandle
103+
# source = PS4000A_CHANNEL_B = 1
104+
# pointer to buffer max = ctypes.byref(bufferBMax)
105+
# pointer to buffer min = ctypes.byref(bufferBMin)
106+
# buffer length = maxSamples
107+
# segment index = 0
108+
# ratio mode = PS4000A_RATIO_MODE_NONE = 0
109+
status["setDataBuffersB"] = ps.ps4000aSetDataBuffers(chandle,
110+
ps.PS4000A_CHANNEL['PS4000A_CHANNEL_B'],
111+
bufferBMax.ctypes.data_as(ctypes.POINTER(ctypes.c_int16)),
112+
None,
113+
sizeOfOneBuffer,
114+
memory_segment,
115+
ps.PS4000A_RATIO_MODE['PS4000A_RATIO_MODE_NONE'])
116+
assert_pico_ok(status["setDataBuffersB"])
117+
118+
# Begin streaming mode:
119+
sampleInterval = ctypes.c_int32(250)
120+
sampleUnits = ps.PS4000A_TIME_UNITS['PS4000A_US']
121+
# We are not triggering:
122+
maxPreTriggerSamples = 0
123+
autoStopOn = 1
124+
# No downsampling:
125+
downsampleRatio = 1
126+
status["runStreaming"] = ps.ps4000aRunStreaming(chandle,
127+
ctypes.byref(sampleInterval),
128+
sampleUnits,
129+
maxPreTriggerSamples,
130+
totalSamples,
131+
autoStopOn,
132+
downsampleRatio,
133+
ps.PS4000A_RATIO_MODE['PS4000A_RATIO_MODE_NONE'],
134+
sizeOfOneBuffer)
135+
assert_pico_ok(status["runStreaming"])
136+
137+
actualSampleInterval = sampleInterval.value
138+
actualSampleIntervalNs = actualSampleInterval * 1000
139+
140+
print("Capturing at sample interval %s ns" % actualSampleIntervalNs)
141+
142+
# We need a big buffer, not registered with the driver, to keep our complete capture in.
143+
bufferCompleteA = np.zeros(shape=totalSamples, dtype=np.int16)
144+
bufferCompleteB = np.zeros(shape=totalSamples, dtype=np.int16)
145+
nextSample = 0
146+
autoStopOuter = False
147+
wasCalledBack = False
148+
149+
150+
def streaming_callback(handle, noOfSamples, startIndex, overflow, triggerAt, triggered, autoStop, param):
151+
global nextSample, autoStopOuter, wasCalledBack
152+
wasCalledBack = True
153+
destEnd = nextSample + noOfSamples
154+
sourceEnd = startIndex + noOfSamples
155+
bufferCompleteA[nextSample:destEnd] = bufferAMax[startIndex:sourceEnd]
156+
bufferCompleteB[nextSample:destEnd] = bufferBMax[startIndex:sourceEnd]
157+
nextSample += noOfSamples
158+
if autoStop:
159+
autoStopOuter = True
160+
161+
162+
# Convert the python function into a C function pointer.
163+
cFuncPtr = ps.StreamingReadyType(streaming_callback)
164+
165+
# Fetch data from the driver in a loop, copying it out of the registered buffers and into our complete one.
166+
while nextSample < totalSamples and not autoStopOuter:
167+
wasCalledBack = False
168+
status["getStreamingLastestValues"] = ps.ps4000aGetStreamingLatestValues(chandle, cFuncPtr, None)
169+
if not wasCalledBack:
170+
# If we weren't called back by the driver, this means no data is ready. Sleep for a short while before trying
171+
# again.
172+
time.sleep(0.01)
173+
174+
print("Done grabbing values.")
175+
176+
# Find maximum ADC count value
177+
# handle = chandle
178+
# pointer to value = ctypes.byref(maxADC)
179+
maxADC = ctypes.c_int16()
180+
status["maximumValue"] = ps.ps4000aMaximumValue(chandle, ctypes.byref(maxADC))
181+
assert_pico_ok(status["maximumValue"])
182+
183+
# Convert ADC counts data to mV
184+
adc2mVChAMax = adc2mV(bufferCompleteA, channel_range, maxADC)
185+
adc2mVChBMax = adc2mV(bufferCompleteB, channel_range, maxADC)
186+
187+
# Create time data
188+
time = np.linspace(0, (totalSamples) * actualSampleIntervalNs, totalSamples)
189+
190+
# Plot data from channel A and B
191+
plt.plot(time, adc2mVChAMax[:])
192+
plt.plot(time, adc2mVChBMax[:])
193+
plt.xlabel('Time (ns)')
194+
plt.ylabel('Voltage (mV)')
195+
plt.show()
196+
197+
# Stop the scope
198+
# handle = chandle
199+
status["stop"] = ps.ps4000aStop(chandle)
200+
assert_pico_ok(status["stop"])
201+
202+
# Disconnect the scope
203+
# handle = chandle
204+
status["close"] = ps.ps4000aCloseUnit(chandle)
205+
assert_pico_ok(status["close"])
206+
207+
# Display status returns
208+
print(status)

0 commit comments

Comments
 (0)