PicoScope 7 Software
Available on Windows, Mac and Linux
Code: Select all
from openpyxl import load_workbook
wb = load_workbook('D:\\PhD_Work\\PicoScope\\DAta_trials_streamingA.xlsx')
work_sheet = wb.active # Get active sheet
import ctypes
import numpy as np
from picosdk.ps5000a import ps5000a as ps
import matplotlib.pyplot as plt
from picosdk.functions import adc2mV, assert_pico_ok
import time
# Create chandle and status ready for use
chandle = ctypes.c_int16()
status = {} #status is the dictionary we are opening. This dictionary has various
#components and these are updated as we move down the code.
# Open PicoScope 5000 Series device
# Resolution set to 12 Bit
resolution =ps.PS5000A_DEVICE_RESOLUTION["PS5000A_DR_12BIT"]
# Returns handle to chandle for use in future API functions
status["openunit"] = ps.ps5000aOpenUnit(ctypes.byref(chandle), None, resolution)
#ps5000aOpenunit(handle, serial: if it is none then 1st scope found is opened, resolution)
try:
assert_pico_ok(status["openunit"])
except: # PicoNotOkError:
powerStatus = status["openunit"]
if powerStatus == 286:
status["changePowerSource"] = ps.ps5000aChangePowerSource(chandle, powerStatus)
elif powerStatus == 282:
status["changePowerSource"] = ps.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 = PS5000A_CHANNEL_A = 0
# enabled = 1
# coupling type = PS5000A_DC = 1
# range = PS5000A_2V = 7
# analogue offset = 0 V
channel_range = ps.PS5000A_RANGE['PS5000A_10MV']
status["setChA"] = ps.ps5000aSetChannel(chandle,
ps.PS5000A_CHANNEL['PS5000A_CHANNEL_A'],
enabled,
ps.PS5000A_COUPLING['PS5000A_DC'],
channel_range,
analogue_offset)
assert_pico_ok(status["setChA"])
# Size of capture
sizeOfOneBuffer = 500 #original value = 500
numBuffersToCapture = 10 #original value = 10
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"] = ps.ps5000aSetDataBuffers(chandle,
ps.PS5000A_CHANNEL['PS5000A_CHANNEL_A'],
bufferAMax.ctypes.data_as(ctypes.POINTER(ctypes.c_int16)),
None,
sizeOfOneBuffer,
memory_segment,
ps.PS5000A_RATIO_MODE['PS5000A_RATIO_MODE_NONE'])
assert_pico_ok(status["setDataBuffersA"])
# Begin streaming mode:
sampleInterval = ctypes.c_int32(250)#ORIGINALLY 250
sampleUnits = ps.PS5000A_TIME_UNITS['PS5000A_US'] #originally US changed it to MS
# We are not triggering:
maxPreTriggerSamples = 0
autoStopOn = 1
# No downsampling:
downsampleRatio = 1
status["runStreaming"] = ps.ps5000aRunStreaming(chandle,
ctypes.byref(sampleInterval),
sampleUnits,
maxPreTriggerSamples,
totalSamples,
autoStopOn,
downsampleRatio,
ps.PS5000A_RATIO_MODE['PS5000A_RATIO_MODE_NONE'],
sizeOfOneBuffer)
assert_pico_ok(status["runStreaming"])
actualSampleInterval = sampleInterval.value
actualSampleIntervalNs = actualSampleInterval
print("Capturing at sample interval %s ns" % actualSampleIntervalNs)
# We need a big buffer, not registered with the driver, to keep our complete capture in.
bufferCompleteA = np.zeros(shape=totalSamples, dtype=np.int16)
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"] = ps.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.1)
print("Done grabbing values.")
# Find maximum ADC count value
# handle = chandle
# pointer to value = ctypes.byref(maxADC)
maxADC = ctypes.c_int16()
status["maximumValue"] = ps.ps5000aMaximumValue(chandle, ctypes.byref(maxADC))
assert_pico_ok(status["maximumValue"])
# Convert ADC counts data to mV
adc2mVChAMax = adc2mV(bufferCompleteA, channel_range, maxADC)
# Create time data
time = np.linspace(0, (totalSamples) * actualSampleIntervalNs, totalSamples)
#find the minimum and maximum intensity values
MaxIA = max(adc2mVChAMax[:]);
MinIA = min(adc2mVChAMax[:]);
SizeA = len(adc2mVChAMax[:]);
SizeT = len(time);
work_sheet.append([MaxIA, MinIA, SizeA, SizeT])
wb.save('D:\\PhD_Work\\PicoScope\\DAta_trials_streamingA.xlsx')
# Plot data from channel A and B
plt.figure(2)
plt.plot(time, adc2mVChAMax[:], 'r')
plt.xlabel('Time (s)')
plt.ylabel('Voltage (mV)')
plt.show()
# Stop the scope
# handle = chandle
status["stop"] = ps.ps5000aStop(chandle)
assert_pico_ok(status["stop"])
# Disconnect the scope
# handle = chandle
status["close"] = ps.ps5000aCloseUnit(chandle)
assert_pico_ok(status["close"])
# Display status returns
print(status)