|
| 1 | +# |
| 2 | +# Copyright (C) 2018 Pico Technology Ltd. See LICENSE file for terms. |
| 3 | +# |
| 4 | +# PS4000A RAPID BLOCK MODE EXAMPLE |
| 5 | +# This example opens a 4000a driver device, sets up two channels and a trigger then collects 10 blocks of data with additional driver calls. |
| 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 | + |
| 14 | +# Create chandle and status ready for use |
| 15 | +chandle = ctypes.c_int16() |
| 16 | +status = {} |
| 17 | + |
| 18 | +# Open 4000 series PicoScope |
| 19 | +# Returns handle to chandle for use in future API functions |
| 20 | +status["openunit"] = ps.ps4000aOpenUnit(ctypes.byref(chandle), None) |
| 21 | + |
| 22 | +try: |
| 23 | + assert_pico_ok(status["openunit"]) |
| 24 | +except: # PicoNotOkError: |
| 25 | + powerStatus = status["openunit"] |
| 26 | + |
| 27 | + if powerStatus == 286: |
| 28 | + status["changePowerSource"] = ps.ps4000aChangePowerSource(chandle, powerStatus) |
| 29 | + elif powerStatus == 282: |
| 30 | + status["changePowerSource"] = ps.ps4000aChangePowerSource(chandle, powerStatus) |
| 31 | + else: |
| 32 | + raise |
| 33 | + |
| 34 | + assert_pico_ok(status["changePowerSource"]) |
| 35 | + |
| 36 | +# Set up channel A |
| 37 | +# handle = chandle |
| 38 | +# channel = PS4000a_CHANNEL_A = 0 |
| 39 | +# enabled = 1 |
| 40 | +# coupling type = PS4000a_DC = 1 |
| 41 | +# range = PS4000a_2V = 7 |
| 42 | +# analogOffset = 0 V |
| 43 | +chARange = 7 |
| 44 | +status["setChA"] = ps.ps4000aSetChannel(chandle, 0, 1, 1, chARange, 0) |
| 45 | +assert_pico_ok(status["setChA"]) |
| 46 | + |
| 47 | +# Set up channel B |
| 48 | +# handle = chandle |
| 49 | +# channel = PS4000a_CHANNEL_B = 1 |
| 50 | +# enabled = 1 |
| 51 | +# coupling type = PS4000a_DC = 1 |
| 52 | +# range = PS4000a_2V = 7 |
| 53 | +# analogOffset = 0 V |
| 54 | +chBRange = 7 |
| 55 | +status["setChB"] = ps.ps4000aSetChannel(chandle, 1, 1, 1, chBRange, 0) |
| 56 | +assert_pico_ok(status["setChB"]) |
| 57 | + |
| 58 | +# Set up single trigger |
| 59 | +# handle = chandle |
| 60 | +# enabled = 1 |
| 61 | +# source = PS4000a_CHANNEL_A = 0 |
| 62 | +# threshold = 1024 ADC counts |
| 63 | +# direction = PS4000a_RISING = 2 |
| 64 | +# delay = 0 s |
| 65 | +# auto Trigger = 1000 ms |
| 66 | +status["trigger"] = ps.ps4000aSetSimpleTrigger(chandle, 1, 0, 1024, 2, 0, 1000) |
| 67 | +assert_pico_ok(status["trigger"]) |
| 68 | + |
| 69 | +# Set number of pre and post trigger samples to be collected |
| 70 | +preTriggerSamples = 2500 |
| 71 | +postTriggerSamples = 2500 |
| 72 | +maxSamples = preTriggerSamples + postTriggerSamples |
| 73 | + |
| 74 | +# Get timebase information |
| 75 | +# handle = chandle |
| 76 | +# timebase = 8 = timebase |
| 77 | +# noSamples = maxSamples |
| 78 | +# pointer to timeIntervalNanoseconds = ctypes.byref(timeIntervalns) |
| 79 | +# pointer to maxSamples = ctypes.byref(returnedMaxSamples) |
| 80 | +# segment index = 0 |
| 81 | +timebase = 8 |
| 82 | +timeIntervalns = ctypes.c_float() |
| 83 | +returnedMaxSamples = ctypes.c_int32() |
| 84 | +oversample = ctypes.c_int16(1) |
| 85 | +status["getTimebase2"] = ps.ps4000aGetTimebase2(chandle, timebase, maxSamples, ctypes.byref(timeIntervalns), ctypes.byref(returnedMaxSamples), 0) |
| 86 | +assert_pico_ok(status["getTimebase2"]) |
| 87 | + |
| 88 | +# Set memory segments |
| 89 | +# handle = chandle |
| 90 | +# nSegments = 10 |
| 91 | +nMaxSamples = ctypes.c_int32(0) |
| 92 | +status["setMemorySegments"] = ps.ps4000aMemorySegments(chandle, 10, ctypes.byref(nMaxSamples)) |
| 93 | +assert_pico_ok(status["setMemorySegments"]) |
| 94 | + |
| 95 | +# Set number of captures |
| 96 | +# handle = chandle |
| 97 | +# nCaptures = 10 |
| 98 | +status["SetNoOfCaptures"] = ps.ps4000aSetNoOfCaptures(chandle, 10) |
| 99 | +assert_pico_ok(status["SetNoOfCaptures"]) |
| 100 | + |
| 101 | +# set up buffers |
| 102 | +bufferA0 = (ctypes.c_int16 * maxSamples)() |
| 103 | +bufferA1 = (ctypes.c_int16 * maxSamples)() |
| 104 | +bufferA2 = (ctypes.c_int16 * maxSamples)() |
| 105 | +bufferA3 = (ctypes.c_int16 * maxSamples)() |
| 106 | +bufferA4 = (ctypes.c_int16 * maxSamples)() |
| 107 | +bufferA5 = (ctypes.c_int16 * maxSamples)() |
| 108 | +bufferA6 = (ctypes.c_int16 * maxSamples)() |
| 109 | +bufferA7 = (ctypes.c_int16 * maxSamples)() |
| 110 | +bufferA8 = (ctypes.c_int16 * maxSamples)() |
| 111 | +bufferA9 = (ctypes.c_int16 * maxSamples)() |
| 112 | + |
| 113 | +bufferB0 = (ctypes.c_int16 * maxSamples)() |
| 114 | +bufferB1 = (ctypes.c_int16 * maxSamples)() |
| 115 | +bufferB2 = (ctypes.c_int16 * maxSamples)() |
| 116 | +bufferB3 = (ctypes.c_int16 * maxSamples)() |
| 117 | +bufferB4 = (ctypes.c_int16 * maxSamples)() |
| 118 | +bufferB5 = (ctypes.c_int16 * maxSamples)() |
| 119 | +bufferB6 = (ctypes.c_int16 * maxSamples)() |
| 120 | +bufferB7 = (ctypes.c_int16 * maxSamples)() |
| 121 | +bufferB8 = (ctypes.c_int16 * maxSamples)() |
| 122 | +bufferB9 = (ctypes.c_int16 * maxSamples)() |
| 123 | + |
| 124 | +# set data buffers |
| 125 | +# handle = chandle |
| 126 | +# channel = PS4000A_CHANNEL_A = 0 |
| 127 | +# buffer = bufferAX (where X = segmentIndex) |
| 128 | +# bufferLength = maxSamples |
| 129 | +# segmentIndex = X |
| 130 | +# mode = PS4000A_RATIO_MODE_NONE = 0 |
| 131 | + |
| 132 | +status["setDataBufferA0"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferA0), maxSamples, 0, 0) |
| 133 | +status["setDataBufferA1"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferA1), maxSamples, 1, 0) |
| 134 | +status["setDataBufferA2"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferA2), maxSamples, 2, 0) |
| 135 | +status["setDataBufferA3"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferA3), maxSamples, 3, 0) |
| 136 | +status["setDataBufferA4"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferA4), maxSamples, 4, 0) |
| 137 | +status["setDataBufferA5"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferA5), maxSamples, 5, 0) |
| 138 | +status["setDataBufferA6"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferA6), maxSamples, 6, 0) |
| 139 | +status["setDataBufferA7"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferA7), maxSamples, 7, 0) |
| 140 | +status["setDataBufferA8"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferA8), maxSamples, 8, 0) |
| 141 | +status["setDataBufferA9"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferA9), maxSamples, 9, 0) |
| 142 | + |
| 143 | +status["setDataBufferB0"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferB0), maxSamples, 0, 0) |
| 144 | +status["setDataBufferB1"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferB1), maxSamples, 1, 0) |
| 145 | +status["setDataBufferB2"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferB2), maxSamples, 2, 0) |
| 146 | +status["setDataBufferB3"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferB3), maxSamples, 3, 0) |
| 147 | +status["setDataBufferB4"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferB4), maxSamples, 4, 0) |
| 148 | +status["setDataBufferB5"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferB5), maxSamples, 5, 0) |
| 149 | +status["setDataBufferB6"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferB6), maxSamples, 6, 0) |
| 150 | +status["setDataBufferB7"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferB7), maxSamples, 7, 0) |
| 151 | +status["setDataBufferB8"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferB8), maxSamples, 8, 0) |
| 152 | +status["setDataBufferB9"] = ps.ps4000aSetDataBuffer(chandle, 0, ctypes.byref(bufferB9), maxSamples, 9, 0) |
| 153 | + |
| 154 | +# run block capture |
| 155 | +# handle = chandle |
| 156 | +# number of pre-trigger samples = preTriggerSamples |
| 157 | +# number of post-trigger samples = PostTriggerSamples |
| 158 | +# timebase = 3 = 80 ns = timebase (see Programmer's guide for mre information on timebases) |
| 159 | +# time indisposed ms = None (not needed in the example) |
| 160 | +# segment index = 0 |
| 161 | +# lpReady = None (using ps4000aIsReady rather than ps4000aBlockReady) |
| 162 | +# pParameter = None |
| 163 | +status["runBlock"] = ps.ps4000aRunBlock(chandle, preTriggerSamples, postTriggerSamples, timebase, None, 0, None, None) |
| 164 | +assert_pico_ok(status["runBlock"]) |
| 165 | + |
| 166 | +# check for end of capture |
| 167 | +ready = ctypes.c_int16(0) |
| 168 | +check = ctypes.c_int16(0) |
| 169 | +while ready.value == check.value: |
| 170 | + status["isReady"] = ps.ps4000aIsReady(chandle, ctypes.byref(ready)) |
| 171 | + |
| 172 | +# Creates a overlow location for data |
| 173 | +overflow = (ctypes.c_int16 * 10)() |
| 174 | +# Creates converted types maxsamples |
| 175 | +cmaxSamples = ctypes.c_int32(maxSamples) |
| 176 | + |
| 177 | +# collect data |
| 178 | +# handle = chandle |
| 179 | +# noOfSamples = cmaxSamples |
| 180 | +# fromSegmentIndex = 0 |
| 181 | +# toSegmentIndex = 9 |
| 182 | +# downSampleRatio = 1 |
| 183 | +# downSampleRatioMode = PS4000A_RATIO_MODE_NONE |
| 184 | +status["getValuesBulk"] = ps.ps4000aGetValuesBulk(chandle, ctypes.byref(cmaxSamples), 0, 9, 1, 0, ctypes.byref(overflow)) |
| 185 | +assert_pico_ok(status["getValuesBulk"]) |
| 186 | + |
| 187 | +# convert from adc to mV |
| 188 | + |
| 189 | +# plot data |
| 190 | + |
| 191 | +# Stop the scope |
| 192 | +# handle = chandle |
| 193 | +status["stop"] = ps.ps4000aStop(chandle) |
| 194 | +assert_pico_ok(status["stop"]) |
| 195 | + |
| 196 | +# Close unitDisconnect the scope |
| 197 | +# handle = chandle |
| 198 | +status["close"] = ps.ps4000aCloseUnit(chandle) |
| 199 | +assert_pico_ok(status["close"]) |
| 200 | + |
| 201 | +# display status returns |
| 202 | +print(status) |
| 203 | + |
0 commit comments