I am trying to write a streaming example for Picoscope 5000 series into a object oriented script. The example is available on their repo found here: PicoScope Streaming example. Here is an extract of the related code that I adapted from the example
class Readpicochannel():
"""Class for setting up and reading picoscope streams"""
def streaming_init(self):
self.status["runStreaming"] = ps.ps5000aRunStreaming(self.chandle, ctypes.byref(sampleInterval), sampleUnits, self.streaming_vars["maxPreTriggerSamples"], totalSamples, self.streaming_vars["autoStopOn"], self.streaming_vars["downsampleRatio"], ps.PS5000A_RATIO_MODE['PS5000A_RATIO_MODE_NONE'], sizeOfOneBuffer)
assert_pico_ok(self.status["runStreaming"])
print(f"Capturing at sample interval {actualSampleIntervalNs}ns")
def streaming_callback(self, handle, noOfSamples, startIndex, overflow, triggerAt, triggered, autoStop, param):
self.buffer_var["wasCalledBack"] = True
destEnd = self.buffer_var["nextSample"] + noOfSamples
sourceEnd = startIndex + noOfSamples
complete_buffer[self.buffer_var["nextSample"]:destEnd] = self.max_capture_buffer[startIndex:sourceEnd]
self.buffer_var["nextSample"] += self.noOfSamples
if autoStop:
self.buffer_var["autoStopOuter"] = True
def cont_streaming(self, cFuncPtr, tester):
if tester:
print("Entered testing loop")
while self.buffer_var["nextSample"] < totalSamples and not self.buffer_var["autoStopOuter"]:
self.buffer_var["wasCalledBack"] = False
self.status["getStreamingLastestValues"] = ps.ps5000aGetStreamingLatestValues(self.chandle, cFuncPtr, None)
if not self.buffer_var["wasCalledBack"]:
time.sleep(0.01)
print("Done capturing values")
return self.convert_d2a()
else:
while True:
wasCalledBack = False
self.status["getStreamingLastestValues"] = ps.ps5000aGetStreamingLatestValues(self.chandle, cFuncPtr, None)
if not wasCalledBack:
time.sleep(0.01)
return self.convert_d2a()
Edit: after reading the comments and adding the rest of the args, I get this error now:
AttributeError: 'Readpicochannel' object has no attribute 'noOfSamples'
Not sure what is wrong with my approach or if I didnt configure the C pointer to the function properly. Any help is appreciated. Thanks
It seems that there are some issues with how you’re using the noOfSamples
variable in your code. Specifically, it appears that noOfSamples
is not defined within the scope of the cont_streaming
method.
You should make sure that noOfSamples
is defined as an attribute of your class, or it should be passed as an argument to the cont_streaming
method. Additionally, you need to replace instances of self.noOfSamples
with the correct variable name.
Here’s a modified version of your code that defines noOfSamples
as an attribute of the class and passes it as an argument to the cont_streaming
method:
class Readpicochannel():
"""Class for setting up and reading picoscope streams"""
def __init__(self):
self.noOfSamples = 0 # Initialize noOfSamples as an attribute
def streaming_init(self):
# ... (your existing code)
def streaming_callback(self, handle, noOfSamples, startIndex, overflow, triggerAt, triggered, autoStop, param):
self.buffer_var["wasCalledBack"] = True
destEnd = self.buffer_var["nextSample"] + noOfSamples
sourceEnd = startIndex + noOfSamples
complete_buffer[self.buffer_var["nextSample"]:destEnd] = self.max_capture_buffer[startIndex:sourceEnd]
self.buffer_var["nextSample"] += self.noOfSamples # Use self.noOfSamples here
if autoStop:
self.buffer_var["autoStopOuter"] = True
def cont_streaming(self, cFuncPtr, tester):
if tester:
print("Entered testing loop")
while self.buffer_var["nextSample"] < totalSamples and not self.buffer_var["autoStopOuter"]:
self.buffer_var["wasCalledBack"] = False
self.status["getStreamingLastestValues"] = ps.ps5000aGetStreamingLatestValues(self.chandle, cFuncPtr, None)
if not self.buffer_var["wasCalledBack"]:
time.sleep(0.01)
print("Done capturing values")
return self.convert_d2a()
else:
while True:
wasCalledBack = False
self.status["getStreamingLastestValues"] = ps.ps5000aGetStreamingLatestValues(self.chandle, cFuncPtr, None)
if not wasCalledBack:
time.sleep(0.01)
return self.convert_d2a()
Make sure to adjust the rest of your code accordingly. If noOfSamples
needs to be initialized or modified somewhere else, you should do that in your code.