Code: Select all
print('Connecting to scope')
# Create chandle and status ready for use
chandle = ctypes.c_int16()
status = {}
# Open PicoScope 5000 Series device
# Resolution set to 12 Bit
resolution =ps5.PS5000A_DEVICE_RESOLUTION["PS5000A_DR_16BIT"]
# Returns handle to chandle for use in future API functions
status["openunit"] = ps5.ps5000aOpenUnit(ctypes.byref(chandle), None, resolution)
try:
assert_pico_ok(status["openunit"])
except: # PicoNotOkError:
powerStatus = status["openunit"]
if powerStatus == 286:
status["changePowerSource"] = ps5.ps5000aChangePowerSource(chandle, powerStatus)
elif powerStatus == 282:
status["changePowerSource"] = ps5.ps5000aChangePowerSource(chandle, powerStatus)
else:
raise
assert_pico_ok(status["changePowerSource"])
enabled = 1
disabled = 0
analogue_offset = 0.0
# Set up channel A
# handle = chandle
# channel = PS5000_CHANNEL_A = 0
# enabled = 1
# coupling type = PS5000_DC = 1
# analogue offset = 0 V
print('Setting scope chan A range to +/-' + str(SCOPE_RANGE_VOLTS[scopeRange]) + 'V')
# there has got to be a better way
channel_range = ps5.PS5000A_RANGE[list(ps5.PS5000A_RANGE)[scopeRange]]
status["setChA"] = ps5.ps5000aSetChannel(chandle,
ps5.PS5000A_CHANNEL['PS5000A_CHANNEL_A'],
enabled,
coupling,
channel_range,
analogue_offset)
assert_pico_ok(status["setChA"])
print('Disabling scope chan B')
# Disable channel B
status["disableChB"] = ps5.ps5000aSetChannel(chandle,
ps5.PS5000A_CHANNEL['PS5000A_CHANNEL_B'],
0,
1,
channel_range,
analogue_offset)
assert_pico_ok(status["disableChB"])
''' Channels C and D are automatically disabled if on USB power
# Disable channel C
status["disableChC"] = ps5.ps5000aSetChannel(chandle,
ps5.PS5000A_CHANNEL['PS5000A_CHANNEL_C'],
0,
1,
channel_range,
analogue_offset)
assert_pico_ok(status["disableChC"])
# Disable channel D
status["disableChD"] = ps5.ps5000aSetChannel(chandle,
ps5.PS5000A_CHANNEL['PS5000A_CHANNEL_D'],
0,
1,
channel_range,
analogue_offset)
assert_pico_ok(status["disableChD"])
'''
print('Configuring data buffers')
# Size of capture
totalSamples = sizeOfOneBuffer * numBuffersToCapture
# Create buffers ready for assigning pointers for data collection
bufferAMax = np.zeros(shape=sizeOfOneBuffer, dtype=np.int16)
memory_segment = 0
# Set data buffer location for data collection from channel A
# handle = chandle
# source = PS5000A_CHANNEL_A = 0
# pointer to buffer max = ctypes.byref(bufferAMax)
# pointer to buffer min = ctypes.byref(bufferAMin)
# buffer length = maxSamples
# segment index = 0
# ratio mode = PS5000A_RATIO_MODE_NONE = 0
status["setDataBuffersA"] = ps5.ps5000aSetDataBuffers(chandle,
ps5.PS5000A_CHANNEL['PS5000A_CHANNEL_A'],
bufferAMax.ctypes.data_as(ctypes.POINTER(ctypes.c_int16)),
None,
sizeOfOneBuffer,
memory_segment,
ps5.PS5000A_RATIO_MODE['PS5000A_RATIO_MODE_NONE'])
assert_pico_ok(status["setDataBuffersA"])
# Begin streaming mode:
sampleInterval = ctypes.c_int32(SAMPLE_PER_NS)
#sampleInterval = ctypes.c_int32(100)
sampleUnits = ps5.PS5000A_TIME_UNITS['PS5000A_US']
#sampleUnits = ps.PS4000_TIME_UNITS['PS4000_NS']
# We are not triggering:
maxPreTriggerSamples = 0
autoStopOn = 0
# No downsampling:
downsampleRatio = 1
status["runStreaming"] = ps5.ps5000aRunStreaming(chandle,
ctypes.byref(sampleInterval),
sampleUnits,
maxPreTriggerSamples,
totalSamples,
autoStopOn,
downsampleRatio,
ps5.PS5000A_RATIO_MODE['PS5000A_RATIO_MODE_NONE'],
sizeOfOneBuffer)
assert_pico_ok(status["runStreaming"])
print('Capturing at sample interval '+ str(SAMPLE_PER_NS) + 'ns from preamp ' + preName)
# We need a big buffer, not registered with the driver, to keep our complete capture in.
bufferCompleteA = np.zeros(shape=totalSamples, dtype=np.int16)
# after putting everything in a function, the program would fail on the line "destEnd = nextSample + noOfSamples" saying nextSample was not defined
# I could tell nextSample was unbound, and the line below these comments fixed that
# TBH I don't really understand the problem and why this fixed it
global nextSample
nextSample = 0
autoStopOuter = False
wasCalledBack = False
def streaming_callback(handle, noOfSamples, startIndex, overflow, triggerAt, triggered, autoStop, param):
global nextSample, autoStopOuter, wasCalledBack
wasCalledBack = True
destEnd = nextSample + noOfSamples
sourceEnd = startIndex + noOfSamples
bufferCompleteA[nextSample:destEnd] = bufferAMax[startIndex:sourceEnd]
nextSample += noOfSamples
if autoStop:
autoStopOuter = True
# Convert the python function into a C function pointer.
cFuncPtr = ps.StreamingReadyType(streaming_callback)
# Fetch data from the driver in a loop, copying it out of the registered buffers and into our complete one.
while nextSample < totalSamples and not autoStopOuter:
wasCalledBack = False
status["getStreamingLastestValues"] = ps5.ps5000aGetStreamingLatestValues(chandle, cFuncPtr, None)
if not wasCalledBack:
# If we weren't called back by the driver, this means no data is ready. Sleep for a short while before trying
# again.
time.sleep(0.01)
print("Capture complete")