Skip to content

Commit 0de4fac

Browse files
author
neil.hamilton
committed
Create variant of Block example that uses the callback method
1 parent 24c8097 commit 0de4fac

File tree

1 file changed

+197
-0
lines changed

1 file changed

+197
-0
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
#
2+
# Copyright (C) 2018 Pico Technology Ltd. See LICENSE file for terms.
3+
#
4+
# PS5000A BLOCK MODE EXAMPLE
5+
# This example opens a 5000a driver device, sets up two channels and a trigger then collects a block of data.
6+
# This data is then plotted as mV against time in ns.
7+
8+
import ctypes
9+
import numpy as np
10+
from picosdk.ps5000a import ps5000a as ps
11+
import matplotlib.pyplot as plt
12+
from picosdk.functions import adc2mV, assert_pico_ok, mV2adc
13+
from picosdk.constants import PICO_STATUS
14+
import time
15+
16+
# Create chandle and status ready for use
17+
chandle = ctypes.c_int16()
18+
status = {}
19+
20+
# Open 5000 series PicoScope
21+
# Resolution set to 12 Bit
22+
resolution =ps.PS5000A_DEVICE_RESOLUTION["PS5000A_DR_12BIT"]
23+
# Returns handle to chandle for use in future API functions
24+
status["openunit"] = ps.ps5000aOpenUnit(ctypes.byref(chandle), None, resolution)
25+
26+
try:
27+
assert_pico_ok(status["openunit"])
28+
except: # PicoNotOkError:
29+
30+
powerStatus = status["openunit"]
31+
32+
if powerStatus == 286:
33+
status["changePowerSource"] = ps.ps5000aChangePowerSource(chandle, powerStatus)
34+
elif powerStatus == 282:
35+
status["changePowerSource"] = ps.ps5000aChangePowerSource(chandle, powerStatus)
36+
else:
37+
raise
38+
39+
assert_pico_ok(status["changePowerSource"])
40+
41+
# Set up channel A
42+
# handle = chandle
43+
channel = ps.PS5000A_CHANNEL["PS5000A_CHANNEL_A"]
44+
# enabled = 1
45+
coupling_type = ps.PS5000A_COUPLING["PS5000A_DC"]
46+
chARange = ps.PS5000A_RANGE["PS5000A_20V"]
47+
# analogue offset = 0 V
48+
status["setChA"] = ps.ps5000aSetChannel(chandle, channel, 1, coupling_type, chARange, 0)
49+
assert_pico_ok(status["setChA"])
50+
51+
# Set up channel B
52+
# handle = chandle
53+
channel = ps.PS5000A_CHANNEL["PS5000A_CHANNEL_B"]
54+
# enabled = 1
55+
# coupling_type = ps.PS5000A_COUPLING["PS5000A_DC"]
56+
chBRange = ps.PS5000A_RANGE["PS5000A_2V"]
57+
# analogue offset = 0 V
58+
status["setChB"] = ps.ps5000aSetChannel(chandle, channel, 1, coupling_type, chBRange, 0)
59+
assert_pico_ok(status["setChB"])
60+
61+
# find maximum ADC count value
62+
# handle = chandle
63+
# pointer to value = ctypes.byref(maxADC)
64+
maxADC = ctypes.c_int16()
65+
status["maximumValue"] = ps.ps5000aMaximumValue(chandle, ctypes.byref(maxADC))
66+
assert_pico_ok(status["maximumValue"])
67+
68+
# Set up single trigger
69+
# handle = chandle
70+
# enabled = 1
71+
source = ps.PS5000A_CHANNEL["PS5000A_CHANNEL_A"]
72+
threshold = int(mV2adc(500,chARange, maxADC))
73+
# direction = PS5000A_RISING = 2
74+
# delay = 0 s
75+
# auto Trigger = 1000 ms
76+
status["trigger"] = ps.ps5000aSetSimpleTrigger(chandle, 1, source, threshold, 2, 0, 1000)
77+
assert_pico_ok(status["trigger"])
78+
79+
# Set number of pre and post trigger samples to be collected
80+
preTriggerSamples = 2500
81+
postTriggerSamples = 2500
82+
maxSamples = preTriggerSamples + postTriggerSamples
83+
84+
# Get timebase information
85+
# handle = chandle
86+
timebase = 8
87+
# noSamples = maxSamples
88+
# pointer to timeIntervalNanoseconds = ctypes.byref(timeIntervalns)
89+
# pointer to maxSamples = ctypes.byref(returnedMaxSamples)
90+
# segment index = 0
91+
timeIntervalns = ctypes.c_float()
92+
returnedMaxSamples = ctypes.c_int32()
93+
status["getTimebase2"] = ps.ps5000aGetTimebase2(chandle, timebase, maxSamples, ctypes.byref(timeIntervalns), ctypes.byref(returnedMaxSamples), 0)
94+
assert_pico_ok(status["getTimebase2"])
95+
96+
# setup callback function
97+
ready = False
98+
99+
def block_callback(handle, statusCallback, param):
100+
global wasCalledBack, ready
101+
wasCalledBack = True
102+
if statusCallback != PICO_STATUS['PICO_CANCELLED']:
103+
ready = True
104+
105+
# Convert the python function into a C function pointer.
106+
cFuncPtr = ps.BlockReadyType(block_callback)
107+
108+
# Run block capture
109+
# handle = chandle
110+
# number of pre-trigger samples = preTriggerSamples
111+
# number of post-trigger samples = PostTriggerSamples
112+
# timebase = 8 = 80 ns (see Programmer's guide for mre information on timebases)
113+
# time indisposed ms = None (not needed in the example)
114+
# segment index = 0
115+
# lpReady = cFuncPtr
116+
# pParameter = None
117+
status["runBlock"] = ps.ps5000aRunBlock(chandle, preTriggerSamples, postTriggerSamples, timebase, None, 0, cFuncPtr, None)
118+
assert_pico_ok(status["runBlock"])
119+
120+
# Check for data collection to finish using ps5000aIsReady
121+
check = ctypes.c_int16(0)
122+
while ready == False:
123+
time.sleep(0.01)
124+
125+
print("Capture finished")
126+
127+
128+
# Create buffers ready for assigning pointers for data collection
129+
bufferAMax = (ctypes.c_int16 * maxSamples)()
130+
bufferAMin = (ctypes.c_int16 * maxSamples)() # used for downsampling which isn't in the scope of this example
131+
bufferBMax = (ctypes.c_int16 * maxSamples)()
132+
bufferBMin = (ctypes.c_int16 * maxSamples)() # used for downsampling which isn't in the scope of this example
133+
134+
# Set data buffer location for data collection from channel A
135+
# handle = chandle
136+
source = ps.PS5000A_CHANNEL["PS5000A_CHANNEL_A"]
137+
# pointer to buffer max = ctypes.byref(bufferAMax)
138+
# pointer to buffer min = ctypes.byref(bufferAMin)
139+
# buffer length = maxSamples
140+
# segment index = 0
141+
# ratio mode = PS5000A_RATIO_MODE_NONE = 0
142+
status["setDataBuffersA"] = ps.ps5000aSetDataBuffers(chandle, source, ctypes.byref(bufferAMax), ctypes.byref(bufferAMin), maxSamples, 0, 0)
143+
assert_pico_ok(status["setDataBuffersA"])
144+
145+
# Set data buffer location for data collection from channel B
146+
# handle = chandle
147+
source = ps.PS5000A_CHANNEL["PS5000A_CHANNEL_B"]
148+
# pointer to buffer max = ctypes.byref(bufferBMax)
149+
# pointer to buffer min = ctypes.byref(bufferBMin)
150+
# buffer length = maxSamples
151+
# segment index = 0
152+
# ratio mode = PS5000A_RATIO_MODE_NONE = 0
153+
status["setDataBuffersB"] = ps.ps5000aSetDataBuffers(chandle, source, ctypes.byref(bufferBMax), ctypes.byref(bufferBMin), maxSamples, 0, 0)
154+
assert_pico_ok(status["setDataBuffersB"])
155+
156+
# create overflow loaction
157+
overflow = ctypes.c_int16()
158+
# create converted type maxSamples
159+
cmaxSamples = ctypes.c_int32(maxSamples)
160+
161+
# Retried data from scope to buffers assigned above
162+
# handle = chandle
163+
# start index = 0
164+
# pointer to number of samples = ctypes.byref(cmaxSamples)
165+
# downsample ratio = 0
166+
# downsample ratio mode = PS5000A_RATIO_MODE_NONE
167+
# pointer to overflow = ctypes.byref(overflow))
168+
status["getValues"] = ps.ps5000aGetValues(chandle, 0, ctypes.byref(cmaxSamples), 0, 0, 0, ctypes.byref(overflow))
169+
assert_pico_ok(status["getValues"])
170+
171+
172+
# convert ADC counts data to mV
173+
adc2mVChAMax = adc2mV(bufferAMax, chARange, maxADC)
174+
adc2mVChBMax = adc2mV(bufferBMax, chBRange, maxADC)
175+
176+
# Create time data
177+
time = np.linspace(0, (cmaxSamples.value) * timeIntervalns.value, cmaxSamples.value)
178+
179+
# plot data from channel A and B
180+
plt.plot(time, adc2mVChAMax[:])
181+
plt.plot(time, adc2mVChBMax[:])
182+
plt.xlabel('Time (ns)')
183+
plt.ylabel('Voltage (mV)')
184+
plt.show()
185+
186+
# Stop the scope
187+
# handle = chandle
188+
status["stop"] = ps.ps5000aStop(chandle)
189+
assert_pico_ok(status["stop"])
190+
191+
# Close unit Disconnect the scope
192+
# handle = chandle
193+
status["close"]=ps.ps5000aCloseUnit(chandle)
194+
assert_pico_ok(status["close"])
195+
196+
# display status returns
197+
print(status)

0 commit comments

Comments
 (0)