|
| 1 | +# |
| 2 | +# Copyright (C) 2018 Pico Technology Ltd. See LICENSE file for terms. |
| 3 | +# |
| 4 | +# PS2000A BLOCK MODE EXAMPLE |
| 5 | +# This example opens a 2000a 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.ps2000a import ps2000a 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 2000 series PicoScope |
| 20 | +# Returns handle to chandle for use in future API functions |
| 21 | +status["openunit"] = ps.ps2000aOpenUnit(ctypes.byref(chandle), None) |
| 22 | +assert_pico_ok(status["openunit"]) |
| 23 | + |
| 24 | +# Set up channel A |
| 25 | +# handle = chandle |
| 26 | +# channel = PS2000A_CHANNEL_A = 0 |
| 27 | +# enabled = 1 |
| 28 | +# coupling type = PS2000A_DC = 1 |
| 29 | +# range = PS2000A_2V = 7 |
| 30 | +# analogue offset = 0 V |
| 31 | +chARange = 7 |
| 32 | +status["setChA"] = ps.ps2000aSetChannel(chandle, 0, 1, 1, chARange, 0) |
| 33 | +assert_pico_ok(status["setChA"]) |
| 34 | + |
| 35 | +# Set up channel B |
| 36 | +# handle = chandle |
| 37 | +# channel = PS2000A_CHANNEL_B = 1 |
| 38 | +# enabled = 1 |
| 39 | +# coupling type = PS2000A_DC = 1 |
| 40 | +# range = PS2000A_2V = 7 |
| 41 | +# analogue offset = 0 V |
| 42 | +chBRange = 7 |
| 43 | +status["setChB"] = ps.ps2000aSetChannel(chandle, 1, 1, 1, chBRange, 0) |
| 44 | +assert_pico_ok(status["setChB"]) |
| 45 | + |
| 46 | +# Set up single trigger |
| 47 | +# handle = chandle |
| 48 | +# enabled = 1 |
| 49 | +# source = PS2000A_CHANNEL_A = 0 |
| 50 | +# threshold = 1024 ADC counts |
| 51 | +# direction = PS2000A_RISING = 2 |
| 52 | +# delay = 0 s |
| 53 | +# auto Trigger = 1000 ms |
| 54 | +status["trigger"] = ps.ps2000aSetSimpleTrigger(chandle, 1, 0, 1024, 2, 0, 1000) |
| 55 | +assert_pico_ok(status["trigger"]) |
| 56 | + |
| 57 | +# Set number of pre and post trigger samples to be collected |
| 58 | +preTriggerSamples = 2500 |
| 59 | +postTriggerSamples = 2500 |
| 60 | +totalSamples = preTriggerSamples + postTriggerSamples |
| 61 | + |
| 62 | +# Get timebase information |
| 63 | +# handle = chandle |
| 64 | +# timebase = 8 = timebase |
| 65 | +# noSamples = totalSamples |
| 66 | +# pointer to timeIntervalNanoseconds = ctypes.byref(timeIntervalNs) |
| 67 | +# pointer to totalSamples = ctypes.byref(returnedMaxSamples) |
| 68 | +# segment index = 0 |
| 69 | +timebase = 8 |
| 70 | +timeIntervalns = ctypes.c_float() |
| 71 | +returnedMaxSamples = ctypes.c_int32() |
| 72 | +oversample = ctypes.c_int16(0) |
| 73 | +status["getTimebase2"] = ps.ps2000aGetTimebase2(chandle, |
| 74 | + timebase, |
| 75 | + totalSamples, |
| 76 | + ctypes.byref(timeIntervalns), |
| 77 | + oversample, |
| 78 | + ctypes.byref(returnedMaxSamples), |
| 79 | + 0) |
| 80 | +assert_pico_ok(status["getTimebase2"]) |
| 81 | + |
| 82 | +wasCalledBack = False |
| 83 | + |
| 84 | +def blockready_callback(handle, statusCode, param): |
| 85 | + global wasCalledBack |
| 86 | + wasCalledBack = True |
| 87 | + |
| 88 | +cFuncPtr = ps.BlockReadyType(blockready_callback) |
| 89 | + |
| 90 | +# Run block capture |
| 91 | +# handle = chandle |
| 92 | +# number of pre-trigger samples = preTriggerSamples |
| 93 | +# number of post-trigger samples = PostTriggerSamples |
| 94 | +# timebase = 8 = 80 ns = timebase (see Programmer's guide for mre information on timebases) |
| 95 | +# oversample = 0 = oversample |
| 96 | +# time indisposed ms = None (not needed in the example) |
| 97 | +# segment index = 0 |
| 98 | +# lpReady = None (using ps2000aIsReady rather than ps2000aBlockReady) |
| 99 | +# pParameter = None |
| 100 | +status["runBlock"] = ps.ps2000aRunBlock(chandle, |
| 101 | + preTriggerSamples, |
| 102 | + postTriggerSamples, |
| 103 | + timebase, |
| 104 | + oversample, |
| 105 | + None, |
| 106 | + 0, |
| 107 | + cFuncPtr, |
| 108 | + None) |
| 109 | +assert_pico_ok(status["runBlock"]) |
| 110 | + |
| 111 | +# Check for data collection to finish and callback |
| 112 | +while wasCalledBack == False: |
| 113 | + time.sleep(0.01) |
| 114 | + |
| 115 | +# Create buffers ready for assigning pointers for data collection |
| 116 | +bufferAMax = (ctypes.c_int16 * totalSamples)() |
| 117 | +bufferAMin = (ctypes.c_int16 * totalSamples)() # used for downsampling which isn't in the scope of this example |
| 118 | +bufferBMax = (ctypes.c_int16 * totalSamples)() |
| 119 | +bufferBMin = (ctypes.c_int16 * totalSamples)() # used for downsampling which isn't in the scope of this example |
| 120 | + |
| 121 | +# Set data buffer location for data collection from channel A |
| 122 | +# handle = chandle |
| 123 | +# source = PS2000A_CHANNEL_A = 0 |
| 124 | +# pointer to buffer max = ctypes.byref(bufferDPort0Max) |
| 125 | +# pointer to buffer min = ctypes.byref(bufferDPort0Min) |
| 126 | +# buffer length = totalSamples |
| 127 | +# segment index = 0 |
| 128 | +# ratio mode = PS2000A_RATIO_MODE_NONE = 0 |
| 129 | +status["setDataBuffersA"] = ps.ps2000aSetDataBuffers(chandle, |
| 130 | + 0, |
| 131 | + ctypes.byref(bufferAMax), |
| 132 | + ctypes.byref(bufferAMin), |
| 133 | + totalSamples, |
| 134 | + 0, |
| 135 | + 0) |
| 136 | +assert_pico_ok(status["setDataBuffersA"]) |
| 137 | + |
| 138 | +# Set data buffer location for data collection from channel B |
| 139 | +# handle = chandle |
| 140 | +# source = PS2000A_CHANNEL_B = 1 |
| 141 | +# pointer to buffer max = ctypes.byref(bufferBMax) |
| 142 | +# pointer to buffer min = ctypes.byref(bufferBMin) |
| 143 | +# buffer length = totalSamples |
| 144 | +# segment index = 0 |
| 145 | +# ratio mode = PS2000A_RATIO_MODE_NONE = 0 |
| 146 | +status["setDataBuffersB"] = ps.ps2000aSetDataBuffers(chandle, |
| 147 | + 1, |
| 148 | + ctypes.byref(bufferBMax), |
| 149 | + ctypes.byref(bufferBMin), |
| 150 | + totalSamples, |
| 151 | + 0, |
| 152 | + 0) |
| 153 | +assert_pico_ok(status["setDataBuffersB"]) |
| 154 | + |
| 155 | +# Create overflow location |
| 156 | +overflow = ctypes.c_int16() |
| 157 | +# create converted type totalSamples |
| 158 | +cTotalSamples = ctypes.c_int32(totalSamples) |
| 159 | + |
| 160 | +# Retried data from scope to buffers assigned above |
| 161 | +# handle = chandle |
| 162 | +# start index = 0 |
| 163 | +# pointer to number of samples = ctypes.byref(cTotalSamples) |
| 164 | +# downsample ratio = 0 |
| 165 | +# downsample ratio mode = PS2000A_RATIO_MODE_NONE |
| 166 | +# pointer to overflow = ctypes.byref(overflow)) |
| 167 | +status["getValues"] = ps.ps2000aGetValues(chandle, 0, ctypes.byref(cTotalSamples), 0, 0, 0, ctypes.byref(overflow)) |
| 168 | +assert_pico_ok(status["getValues"]) |
| 169 | + |
| 170 | + |
| 171 | +# find maximum ADC count value |
| 172 | +# handle = chandle |
| 173 | +# pointer to value = ctypes.byref(maxADC) |
| 174 | +maxADC = ctypes.c_int16() |
| 175 | +status["maximumValue"] = ps.ps2000aMaximumValue(chandle, ctypes.byref(maxADC)) |
| 176 | +assert_pico_ok(status["maximumValue"]) |
| 177 | + |
| 178 | +# convert ADC counts data to mV |
| 179 | +adc2mVChAMax = adc2mV(bufferAMax, chARange, maxADC) |
| 180 | +adc2mVChBMax = adc2mV(bufferBMax, chBRange, maxADC) |
| 181 | + |
| 182 | +# Create time data |
| 183 | +time = np.linspace(0, ((cTotalSamples.value)-1) * timeIntervalns.value, cTotalSamples.value) |
| 184 | + |
| 185 | +# plot data from channel A and B |
| 186 | +plt.plot(time, adc2mVChAMax[:]) |
| 187 | +plt.plot(time, adc2mVChBMax[:]) |
| 188 | +plt.xlabel('Time (ns)') |
| 189 | +plt.ylabel('Voltage (mV)') |
| 190 | +plt.show() |
| 191 | + |
| 192 | +# Stop the scope |
| 193 | +# handle = chandle |
| 194 | +status["stop"] = ps.ps2000aStop(chandle) |
| 195 | +assert_pico_ok(status["stop"]) |
| 196 | + |
| 197 | +# Close unitDisconnect the scope |
| 198 | +# handle = chandle |
| 199 | +status["close"] = ps.ps2000aCloseUnit(chandle) |
| 200 | +assert_pico_ok(status["close"]) |
| 201 | + |
| 202 | +# display status returns |
| 203 | +print(status) |
0 commit comments