Skip to content

Commit 135d869

Browse files
author
neil.hamilton
committed
Create ps5000BlockExample.py
1 parent 8771fe1 commit 135d869

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#
2+
# Copyright (C) 2018 Pico Technology Ltd. See LICENSE file for terms.
3+
#
4+
# PS5000 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.ps5000 import ps5000 as ps
11+
import matplotlib.pyplot as plt
12+
from picosdk.functions import adc2mV, assert_pico_ok, mV2adc
13+
14+
# Create chandle and status ready for use
15+
chandle = ctypes.c_int16()
16+
status = {}
17+
18+
# Open 5000 series PicoScope
19+
# Returns handle to chandle for use in future API functions
20+
status["openunit"] = ps.ps5000OpenUnit(ctypes.byref(chandle))
21+
assert_pico_ok(status["openunit"])
22+
23+
24+
# Set up channel A
25+
# handle = chandle
26+
channel = ps.PS5000_CHANNEL["PS5000_CHANNEL_A"]
27+
# enabled = 1
28+
coupling_type = 1 # DC
29+
chARange = ps.PS5000_RANGE["PS5000_20V"]
30+
# analogue offset = 0 V
31+
status["setChA"] = ps.ps5000SetChannel(chandle, channel, 1, coupling_type, chARange)
32+
assert_pico_ok(status["setChA"])
33+
34+
# Set up channel B
35+
# handle = chandle
36+
channel = ps.PS5000_CHANNEL["PS5000_CHANNEL_B"]
37+
# enabled = 1
38+
# coupling_type = ps.PS5000_COUPLING["PS5000_DC"]
39+
chBRange = ps.PS5000_RANGE["PS5000_2V"]
40+
# analogue offset = 0 V
41+
status["setChB"] = ps.ps5000SetChannel(chandle, channel, 1, coupling_type, chBRange)
42+
assert_pico_ok(status["setChB"])
43+
44+
# find maximum ADC count value
45+
# handle = chandle
46+
# pointer to value = ctypes.byref(maxADC)
47+
maxADC = ctypes.c_int16(32512)
48+
49+
# Set up single trigger
50+
# handle = chandle
51+
# enabled = 1
52+
source = ps.PS5000_CHANNEL["PS5000_CHANNEL_A"]
53+
threshold = int(mV2adc(500,chARange, maxADC))
54+
# direction = PS5000_RISING = 2
55+
# delay = 0 s
56+
# auto Trigger = 1000 ms
57+
status["trigger"] = ps.ps5000SetSimpleTrigger(chandle, 1, source, threshold, 2, 0, 1000)
58+
assert_pico_ok(status["trigger"])
59+
60+
# Set number of pre and post trigger samples to be collected
61+
preTriggerSamples = 2500
62+
postTriggerSamples = 2500
63+
maxSamples = preTriggerSamples + postTriggerSamples
64+
65+
# Get timebase information
66+
# handle = chandle
67+
timebase = 8
68+
# noSamples = maxSamples
69+
# pointer to timeIntervalNanoseconds = ctypes.byref(timeIntervalns)
70+
oversample = 1
71+
# pointer to maxSamples = ctypes.byref(returnedMaxSamples)
72+
# segment index = 0
73+
timeIntervalns = ctypes.c_float()
74+
returnedMaxSamples = ctypes.c_int32()
75+
status["getTimebase"] = ps.ps5000GetTimebase(chandle, timebase, maxSamples, ctypes.byref(timeIntervalns), oversample, ctypes.byref(returnedMaxSamples), 0)
76+
assert_pico_ok(status["getTimebase2"])
77+
78+
# Run block capture
79+
# handle = chandle
80+
# number of pre-trigger samples = preTriggerSamples
81+
# number of post-trigger samples = PostTriggerSamples
82+
# timebase = 8 = 80 ns (see Programmer's guide for mre information on timebases)
83+
# oversample = 1
84+
# time indisposed ms = None (not needed in the example)
85+
# segment index = 0
86+
# lpReady = None (using ps5000IsReady rather than ps5000BlockReady)
87+
# pParameter = None
88+
status["runBlock"] = ps.ps5000RunBlock(chandle, preTriggerSamples, postTriggerSamples, timebase, oversample, None, 0, None, None)
89+
assert_pico_ok(status["runBlock"])
90+
91+
# Check for data collection to finish using ps5000IsReady
92+
ready = ctypes.c_int16(0)
93+
check = ctypes.c_int16(0)
94+
while ready.value == check.value:
95+
status["isReady"] = ps.ps5000IsReady(chandle, ctypes.byref(ready))
96+
97+
98+
# Create buffers ready for assigning pointers for data collection
99+
bufferAMax = (ctypes.c_int16 * maxSamples)()
100+
bufferAMin = (ctypes.c_int16 * maxSamples)() # used for downsampling which isn't in the scope of this example
101+
bufferBMax = (ctypes.c_int16 * maxSamples)()
102+
bufferBMin = (ctypes.c_int16 * maxSamples)() # used for downsampling which isn't in the scope of this example
103+
104+
# Set data buffer location for data collection from channel A
105+
# handle = chandle
106+
source = ps.PS5000_CHANNEL["PS5000_CHANNEL_A"]
107+
# pointer to buffer max = ctypes.byref(bufferAMax)
108+
# pointer to buffer min = ctypes.byref(bufferAMin)
109+
# buffer length = maxSamples
110+
status["setDataBuffersA"] = ps.ps5000SetDataBuffers(chandle, source, ctypes.byref(bufferAMax), ctypes.byref(bufferAMin), maxSamples)
111+
assert_pico_ok(status["setDataBuffersA"])
112+
113+
# Set data buffer location for data collection from channel B
114+
# handle = chandle
115+
source = ps.PS5000_CHANNEL["PS5000_CHANNEL_B"]
116+
# pointer to buffer max = ctypes.byref(bufferBMax)
117+
# pointer to buffer min = ctypes.byref(bufferBMin)
118+
# buffer length = maxSamples
119+
# segment index = 0
120+
# ratio mode = PS5000_RATIO_MODE_NONE = 0
121+
status["setDataBuffersB"] = ps.ps5000SetDataBuffers(chandle, source, ctypes.byref(bufferBMax), ctypes.byref(bufferBMin), maxSamples)
122+
assert_pico_ok(status["setDataBuffersB"])
123+
124+
# create overflow loaction
125+
overflow = ctypes.c_int16()
126+
# create converted type maxSamples
127+
cmaxSamples = ctypes.c_int32(maxSamples)
128+
129+
# Retried data from scope to buffers assigned above
130+
# handle = chandle
131+
# start index = 0
132+
# pointer to number of samples = ctypes.byref(cmaxSamples)
133+
# downsample ratio = 0
134+
# downsample ratio mode = PS5000_RATIO_MODE_NONE
135+
# pointer to overflow = ctypes.byref(overflow))
136+
status["getValues"] = ps.ps5000GetValues(chandle, 0, ctypes.byref(cmaxSamples), 0, 0, 0, ctypes.byref(overflow))
137+
assert_pico_ok(status["getValues"])
138+
139+
140+
# convert ADC counts data to mV
141+
adc2mVChAMax = adc2mV(bufferAMax, chARange, maxADC)
142+
adc2mVChBMax = adc2mV(bufferBMax, chBRange, maxADC)
143+
144+
# Create time data
145+
time = np.linspace(0, (cmaxSamples.value) * timeIntervalns.value, cmaxSamples.value)
146+
147+
# plot data from channel A and B
148+
plt.plot(time, adc2mVChAMax[:])
149+
plt.plot(time, adc2mVChBMax[:])
150+
plt.xlabel('Time (ns)')
151+
plt.ylabel('Voltage (mV)')
152+
plt.show()
153+
154+
# Stop the scope
155+
# handle = chandle
156+
status["stop"] = ps.ps5000Stop(chandle)
157+
assert_pico_ok(status["stop"])
158+
159+
# Close unit Disconnect the scope
160+
# handle = chandle
161+
status["close"]=ps.ps5000CloseUnit(chandle)
162+
assert_pico_ok(status["close"])
163+
164+
# display status returns
165+
print(status)

0 commit comments

Comments
 (0)