Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PICO__STATUS = {
- 0 : "PICO_OK", \
- 1 : "PICO_MAX_UNITS_OPENED", \
- 2 : "PICO_MEMORY_FAIL", \
- 3 : "PICO_NOT_FOUND", \
- 4 : "PICO_FW_FAIL", \
- 5 : "PICO_OPEN_OPERATION_IN_PROGRESS", \
- 6 : "PICO_OPERATION_FAILED", \
- 7 : "PICO_NOT_RESPONDING", \
- 8 : "PICO_CONFIG_FAIL", \
- 9 : "PICO_KERNEL_DRIVER_TOO_OLD", \
- 10 : "PICO_EEPROM_CORRUPT", \
- 11 : "PICO_OS_NOT_SUPPORTED", \
- 12 : "PICO_INVALID_HANDLE", \
- 13 : "PICO_INVALID_PARAMETER", \
- 14 : "PICO_INVALID_TIMEBASE", \
- 15 : "PICO_INVALID_VOLTAGE_RANGE", \
- 16 : "PICO_INVALID_CHANNEL", \
- 17 : "PICO_INVALID_TRIGGER_CHANNEL", \
- 18 : "PICO_INVALID_CONDITION_CHANNEL", \
- 19 : "PICO_NO_SIGNAL_GENERATOR", \
- 20 : "PICO_STREAMING_FAILED", \
- 21 : "PICO_BLOCK_MODE_FAILED", \
- 22 : "PICO_NULL_PARAMETER", \
- 23 : "PICO_ETS_MODE_SET", \
- 24 : "PICO_DATA_NOT_AVAILABLE", \
- 25 : "PICO_STRING_BUFFER_TO_SMALL", \
- 26 : "PICO_ETS_NOT_SUPPORTED", \
- 27 : "PICO_AUTO_TRIGGER_TIME_TO_SHORT", \
- 28 : "PICO_BUFFER_STALL", \
- 29 : "PICO_TOO_MANY_SAMPLES", \
- 30 : "PICO_TOO_MANY_SEGMENTS", \
- 31 : "PICO_PULSE_WIDTH_QUALIFIER", \
- 32 : "PICO_DELAY", \
- 33 : "PICO_SOURCE_DETAILS", \
- 34 : "PICO_CONDITIONS", \
- 35 : "PICO_USER_CALLBACK", \
- 36 : "PICO_DEVICE_SAMPLING", \
- 37 : "PICO_NO_SAMPLES_AVAILABLE", \
- 38 : "PICO_SEGMENT_OUT_OF_RANGE", \
- 39 : "PICO_BUSY", \
- 40 : "PICO_STARTINDEX_INVALID", \
- 41 : "PICO_INVALID_INFO", \
- 42 : "PICO_INFO_UNAVAILABLE", \
- 43 : "PICO_INVALID_SAMPLE_INTERVAL", \
- 44 : "PICO_TRIGGER_ERROR", \
- 45 : "PICO_MEMORY", \
- 46 : "PICO_SIG_GEN_PARAM", \
- 47 : "PICO_SHOTS_SWEEPS_WARNING", \
- 48 : "PICO_SIGGEN_TRIGGER_SOURCE", \
- 49 : "PICO_AUX_OUTPUT_CONFLICT", \
- 50 : "PICO_AUX_OUTPUT_ETS_CONFLICT", \
- 51 : "PICO_WARNING_EXT_THRESHOLD_CONFLICT", \
- 52 : "PICO_WARNING_AUX_OUTPUT_CONFLICT", \
- 53 : "PICO_SIGGEN_OUTPUT_OVER_VOLTAGE", \
- 54 : "PICO_DELAY_NULL", \
- 55 : "PICO_INVALID_BUFFER", \
- 56 : "PICO_SIGGEN_OFFSET_VOLTAGE", \
- 57 : "PICO_SIGGEN_PK_TO_PK", \
- 58 : "PICO_CANCELLED", \
- 59 : "PICO_SEGMENT_NOT_USED", \
- 60 : "PICO_INVALID_CALL", \
- 61 : "PICO_GET_VALUES_INTERRUPTED", \
- 63 : "PICO_NOT_USED", \
- 64 : "PICO_INVALID_SAMPLERATIO", \
- 65 : "PICO_INVALID_STATE", \
- 66 : "PICO_NOT_ENOUGH_SEGMENTS", \
- 67 : "PICO_DRIVER_FUNCTION", \
- 68 : "PICO_RESERVED", \
- 69 : "PICO_INVALID_COUPLING", \
- 70 : "PICO_BUFFERS_NOT_SET", \
- 71 : "PICO_RATIO_MODE_NOT_SUPPORTED", \
- 72 : "PICO_RAPID_NOT_SUPPORT_AGGREGATION", \
- 73 : "PICO_INVALID_TRIGGER_PROPERTY", \
- 74 : "PICO_INTERFACE_NOT_CONNECTED", \
- 75 : "PICO_RESISTANCE_AND_PROBE_NOT_ALLOWED", \
- 76 : "PICO_POWER_FAILED", \
- 77 : "PICO_SIGGEN_WAVEFORM_SETUP_FAILED", \
- 78 : "PICO_FPGA_FAIL", \
- 79 : "PICO_POWER_MANAGER", \
- 80 : "PICO_INVALID_ANALOGUE_OFFSET", \
- 81 : "PICO_PLL_LOCK_FAILED", \
- 82 : "PICO_ANALOG_BOARD", \
- 83 : "PICO_CONFIG_FAIL_AWG", \
- 84 : "PICO_INITIALISE_FPGA", \
- 86 : "PICO_EXTERNAL_FREQUENCY_INVALID", \
- 87 : "PICO_CLOCK_CHANGE_ERROR", \
- 88 : "PICO_TRIGGER_AND_EXTERNAL_CLOCK_CLASH", \
- 89 : "PICO_PWQ_AND_EXTERNAL_CLOCK_CLASH", \
- 90 : "PICO_UNABLE_TO_OPEN_SCALING_FILE", \
- 91 : "PICO_MEMORY_CLOCK_FREQUENCY", \
- 92 : "PICO_I2C_NOT_RESPONDING", \
- 93 : "PICO_NO_CAPTURES_AVAILABLE", \
- 94 : "PICO_NOT_USED_IN_THIS_CAPTURE_MODE", \
- 259 : "PICO_GET_DATA_ACTIVE", \
- 260 : "PICO_IP_NETWORKED", \
- 261 : "PICO_INVALID_IP_ADDRESS", \
- 262 : "PICO_IPSOCKET_FAILED", \
- 263 : "PICO_IPSOCKET_TIMEDOUT", \
- 264 : "PICO_SETTINGS_FAILED", \
- 265 : "PICO_NETWORK_FAILED", \
- 266 : "PICO_WS2_32_DLL_NOT_LOADED", \
- 267 : "PICO_INVALID_IP_PORT", \
- 268 : "PICO_COUPLING_NOT_SUPPORTED", \
- 269 : "PICO_BANDWIDTH_NOT_SUPPORTED", \
- 270 : "PICO_INVALID_BANDWIDTH", \
- 271 : "PICO_AWG_NOT_SUPPORTED", \
- 272 : "PICO_ETS_NOT_RUNNING", \
- 273 : "PICO_SIG_GEN_WHITENOISE_NOT_SUPPORTED", \
- 274 : "PICO_SIG_GEN_WAVETYPE_NOT_SUPPORTED", \
- 275 : "PICO_INVALID_DIGITAL_PORT", \
- 276 : "PICO_INVALID_DIGITAL_CHANNEL", \
- 277 : "PICO_INVALID_DIGITAL_TRIGGER_DIRECTION", \
- 278 : "PICO_SIG_GEN_PRBS_NOT_SUPPORTED", \
- 279 : "PICO_ETS_NOT_AVAILABLE_WITH_LOGIC_CHANNELS", \
- 280 : "PICO_WARNING_REPEAT_VALUE", \
- 281 : "PICO_POWER_SUPPLY_CONNECTED", \
- 282 : "PICO_POWER_SUPPLY_NOT_CONNECTED", \
- 283 : "PICO_POWER_SUPPLY_REQUEST_INVALID", \
- 284 : "PICO_POWER_SUPPLY_UNDERVOLTAGE", \
- 285 : "PICO_CAPTURING_DATA", \
- 286 : "PICO_USB3_0_DEVICE_NON_USB3_0_PORT", \
- 287 : "PICO_NOT_SUPPORTED_BY_THIS_DEVICE", \
- 288 : "PICO_INVALID_DEVICE_RESOLUTION", \
- 289 : "PICO_INVALID_NUMBER_CHANNELS_FOR_RESOLUTION", \
- 290 : "PICO_CHANNEL_DISABLED_DUE_TO_USB_POWERED", \
- 291 : "PICO_SIGGEN_DC_VOLTAGE_NOT_CONFIGURABLE", \
- 292 : "PICO_NO_TRIGGER_ENABLED_FOR_TRIGGER_IN_PRE_TRIG", \
- 293 : "PICO_TRIGGER_WITHIN_PRE_TRIG_NOT_ARMED", \
- 294 : "PICO_TRIGGER_WITHIN_PRE_NOT_ALLOWED_WITH_DELAY", \
- 295 : "PICO_TRIGGER_INDEX_UNAVAILABLE", \
- 296 : "PICO_AWG_CLOCK_FREQUENCY", \
- 297 : "PICO_TOO_MANY_CHANNELS_IN_USE", \
- 298 : "PICO_NULL_CONDITIONS", \
- 299 : "PICO_DUPLICATE_CONDITION_SOURCE", \
- 300 : "PICO_INVALID_CONDITION_INFO", \
- 301 : "PICO_SETTINGS_READ_FAILED", \
- 302 : "PICO_SETTINGS_WRITE_FAILED", \
- 303 : "PICO_ARGUMENT_OUT_OF_RANGE", \
- 304 : "PICO_HARDWARE_VERSION_NOT_SUPPORTED", \
- 305 : "PICO_DIGITAL_HARDWARE_VERSION_NOT_SUPPORTED", \
- 306 : "PICO_ANALOGUE_HARDWARE_VERSION_NOT_SUPPORTED", \
- 307 : "PICO_UNABLE_TO_CONVERT_TO_RESISTANCE", \
- 308 : "PICO_DUPLICATED_CHANNEL", \
- 309 : "PICO_INVALID_RESISTANCE_CONVERSION", \
- 310 : "PICO_INVALID_VALUE_IN_MAX_BUFFER", \
- 311 : "PICO_INVALID_VALUE_IN_MIN_BUFFER", \
- 312 : "PICO_SIGGEN_FREQUENCY_OUT_OF_RANGE", \
- 313 : "PICO_EEPROM2_CORRUPT", \
- 314 : "PICO_EEPROM2_FAIL",\
- }
- PicoInfo = {\
- 0 : " PICO_DRIVER_VER ",\
- 1 : " PICO_USB_VER ",\
- 2 : " PICO_HW_VER ",\
- 3 : " PICO_VARIANT_INFO ",\
- 4 : " PICO_BATCH_AND_SERIAL",\
- 5 : " PICO_CAL_DATE ",\
- 6 : " PICO_KERNEL_VERSION ",\
- 7 : " DIGITAL_HW_VER, N/A ",\
- 8 : " DIGITAL_SW_VER, N/A ",\
- 9 : " PICO_FW_VER_1 ",\
- 10: "PICO_FW_VER_2 "\
- }
- ENABLED = True
- DISABLED = False
- DC = True
- AC = False
- PS4000_CHANNEL_A = 0
- PS4000_CHANNEL_B = 1
- PS4000_CHANNEL_C = 2
- PS4000_CHANNEL_D = 3
- PS4000_10MV = 0
- PS4000_20MV = 1
- PS4000_50MV = 2
- PS4000_100MV = 3
- PS4000_200MV = 4
- PS4000_500MV = 5
- PS4000_1V = 6
- PS4000_2V = 7
- PS4000_5V = 8
- PS4000_10V = 9
- PS4000_20V = 10
- PS4000_50V = 11
- PS4000_100V = 12
- from ctypes import *
- PS4000_FS = c_uint32(0)
- PS4000_PS = c_uint32(1)
- PS4000_NS = c_uint32(2)
- PS4000_US = c_uint32(3)
- PS4000_MS = c_uint32(4)
- PS4000_S = c_uint32(5)
- ABOVE = 0
- BELOW = 1
- RISING = 2
- FALLING = 3
- RISING_OR_FALLING = 4
- ABOVE_LOWER = 5
- BELOW_LOWER = 6
- RISING_LOWER = 7
- FALLING_LOWER = 8
- TRIG_NOT_ENABLED = c_int16(0)
- TRIG_ENABLED = c_int16(1)
- from time import sleep
- import ctypes
- from ctypes import c_int32 as c_enum
- import os
- import time
- import sys
- # pico dll
- os.chdir("F:\\Picoscope\\ctrlpascal\\")
- DLL = ctypes.WinDLL("ps4000.dll")
- # global variables
- # system setup
- handle = c_short(0)
- pSTAT = c_long()
- # flags
- gf_ready = bool(False)
- gf_streaming = bool(False)
- g_index = 0
- # sampling/streaming
- g_sampleInterval = c_ulong(500)
- g_sampleCount = c_int32(10000)
- #g_ReadySampleCount = c_short(0)
- g_ReadySampleCount = 0
- g_callbackhandle = c_short(0)
- g_startIndex = c_uint(0)
- g_overflow = c_short(0)
- g_triggerAt = c_uint(0)
- g_triggered = c_short(0)
- g_autoStop = c_short(0)
- g_parameter = c_uint(0)
- # buffers
- MAX_BUF_SIZE = 1000
- tmp_buf_A = (c_short * MAX_BUF_SIZE)(0)
- buf_A = (c_short * MAX_BUF_SIZE)(0)
- # for debugging
- irq_counter = 0
- # Output filename
- f_path = "F:\\apo\\Picoscope\\ctrlpascal\\"
- f_name = "picodat1.txt"
- FILENAME = f_path + f_name
- ###############################################################################################
- def picostatus(st):
- for i in PICO__STATUS:
- if st == i:
- return PICO__STATUS[st]
- return "***Error: Device returned UNKNOWN status ***"
- ###############################################################################################
- def GetDeviceInfo():
- print('\ndoing TryGettingInfo')
- requiredSize = c_int16(0) #: SmallInt
- RInfo = c_int() #: PICO_INFO
- HadFailure = bool(False)
- MyReturnStr = create_string_buffer(15)
- print('pico Handle: ', handle)
- for RInfo in range(0,10+1):
- if RInfo == 7 or RInfo == 8: continue
- print('doing GetUnitInfo: ', end='')
- ST = DLL.ps4000GetUnitInfo(handle, byref(MyReturnStr), \
- c_int16(len(MyReturnStr)), byref(requiredSize), RInfo)
- print(RInfo, PicoInfo[RInfo], ': ', end='')
- if ST == 0:
- print(MyReturnStr.value, (13-len(MyReturnStr.value))*' ', picostatus(ST))
- else:
- HadFailure = True
- print(hex(ST), 8*' ', picostatus(ST))
- return not HadFailure
- ###############################################################################################
- # result type as first argument:
- callback = CFUNCTYPE(None, c_short, c_int, c_uint, c_short, c_uint, c_short, c_short, c_void_p)
- ###############################################################################################
- def callback_py(callbackhandle, noOfSamples, startIndex, overflow, triggerAt, triggered, autoStop, parameter):
- global gf_ready
- global gf_streaming
- global g_ReadySampleCount
- global g_callbackhandle
- global g_startIndex
- global g_overflow
- global g_triggerAt
- global g_triggered
- global g_autoStop
- global g_parameter
- # indicate that data packet is ready
- gf_ready = True
- g_ReadySampleCount = noOfSamples
- # identifies the scope device returning the samples
- g_callbackhandle = callbackhandle
- # indicate if and where a trigger has occured
- g_triggerAt = triggerAt
- g_triggered = triggered
- # set of flags that indicate whether an overvoltage has occurred on any of the channels
- g_overflow = overflow
- # index to the first valid sample in the buffer.
- g_startIndex = startIndex
- # as set in ps4000_RunStreaming()
- g_autoStop = autoStop
- print("\n======> autoStop", g_autoStop, autoStop)
- # the pointer passed from ps4000_GetStreamingLatestValues()
- g_parameter = parameter
- #print(hex(g_parameter), g_parameter)
- ### for debugging
- print("callbackhandle =", g_callbackhandle, " g_ReadySampleCount =", g_ReadySampleCount, \
- " startIndex =", g_startIndex, " overflow =", g_overflow, " triggerAt =", g_triggerAt, \
- " triggered =", g_triggered, " autoStop =", g_autoStop, " parameter =", g_parameter)
- # copy tmp_buf to application buf
- if g_ReadySampleCount:
- print("--->", g_ReadySampleCount)
- for i in range(0,g_ReadySampleCount):
- buf_A[i] = tmp_buf_A[i]
- for i in range(0,10):
- print (i, buf_A[i], tmp_buf_A[i], sep = '__' )
- print('\nEntered callback_py, got {} samples: \n'.format(g_ReadySampleCount))
- #return
- ###############################################################################################
- call_back = callback(callback_py)
- ###############################################################################################
- def StreamingChannelsSetup(active_channels):
- chan_A = bool(False)
- chan_B = bool(False)
- chan_C = bool(False)
- chan_D = bool(False)
- for chan in active_channels:
- if chan == 'A':
- ST = DLL.ps4000SetChannel(handle, PS4000_CHANNEL_A, ENABLED, AC, PS4000_10V)
- if ST: print("Error Returned: ", picostatus(ST), "while setting channel A -> ON")
- chan_A = True
- if chan == 'B':
- ST = DLL.ps4000SetChannel(handle, PS4000_CHANNEL_B, ENABLED, AC, PS4000_10V)
- if ST: print("Error Returned: ", picostatus(ST), "while setting channel B -> ON")
- chan_B = True
- if chan == 'C':
- ST = DLL.ps4000SetChannel(handle, PS4000_CHANNEL_C, ENABLED, AC, PS4000_10V)
- if ST: print("Error Returned: ", picostatus(ST), "while setting channel C -> ON")
- chan_C = True
- if chan == 'D':
- ST = DLL.ps4000SetChannel(handle, PS4000_CHANNEL_D, ENABLED, AC, PS4000_10V)
- if ST: print("Error Returned: ", picostatus(ST), "while setting channel D -> ON")
- chan_D = True
- ###############################################################################################
- def DataStreamingSetup():
- global g_sampleCount
- global handle
- global buf_A
- print('\nInto DataStreamingSetup')
- # sampleTime = c_int() # *** check
- # setup Channels: A->ON, BCD->OFF
- ST = DLL.ps4000SetChannel(handle, PS4000_CHANNEL_A, ENABLED, AC, PS4000_10V)
- if ST: print("Error Returned: ", picostatus(ST), "while setting channel A -> ON")
- ST = DLL.ps4000SetChannel(handle, PS4000_CHANNEL_B, DISABLED, DC, PS4000_10V)
- if ST: print("Error Returned: ", picostatus(ST), "while setting channel B -> OFF")
- ST = DLL.ps4000SetChannel(handle, PS4000_CHANNEL_C, DISABLED, DC, PS4000_10V)
- if ST: print("Error Returned: ", picostatus(ST), "while setting channel C -> OFF")
- ST = DLL.ps4000SetChannel(handle, PS4000_CHANNEL_D, DISABLED, DC, PS4000_10V)
- if ST: print("Error Returned: ", picostatus(ST), "while setting channel D -> OFF")
- # clear trigger mode
- trig_threshold = c_int16(5)
- trig_delay = c_uint32(0)
- autoTrigger_ms = c_int16(1)
- # Simple Trigger
- ST = DLL.ps4000SetSimpleTrigger(handle, TRIG_NOT_ENABLED, PS4000_CHANNEL_A, \
- trig_threshold, ABOVE, trig_delay, autoTrigger_ms)
- if ST: print ("Error: ", picostatus(ST), "while doing SetSimpleTrigger(Channel_A)")
- # set up buffer for Channel A
- g_sampleCount = 120
- #ST = DLL.ps4000SetDataBuffer(handle, PS4000_CHANNEL_A, byref(tmp_buf_A), g_sampleCount)
- ST = DLL.ps4000SetDataBuffer(handle, PS4000_CHANNEL_A, tmp_buf_A, g_sampleCount)
- if ST: print("Error: ", picostatus(ST), "while doing SetDataBuffers(Channel_A)")
- print('Setup is done, now will exec RunStreaming')
- ###############################################################################################
- def StartDataStreaming():
- global handle
- global g_sampleInterval
- # start streaming
- ST = DLL.ps4000RunStreaming(handle, byref(g_sampleInterval), PS4000_US, \
- c_uint32(0), c_uint32(120) , c_int16(1), c_uint32(1), g_sampleCount)
- print("RunStreaming() ->", picostatus(ST), "handle: ", handle)
- ###############################################################################################
- def do_while_streaming():
- '''
- Runs continuously after having started the streamning procedure until autostop is triggered or '!' is pressed
- When new data are available through the callback function, it will append them to the output file
- '''
- global handle
- global F
- global gf_ready
- global g_index
- global g_control
- g_index = 0
- total_samples = 0 # holds total no of samples acquired
- while not g_autoStop: # g_autostop <= 0
- gf_ready = False
- print('+',sep='',end='')
- ST = DLL.ps4000GetStreamingLatestValues(handle, call_back, None)
- # sleep(1)
- print(ST,end='')
- g_index += 1
- print('_',g_index, sep='',end='')
- if g_ReadySampleCount > 0:
- # received data from callback
- print ("=> g_ReadySampleCount is :", g_ReadySampleCount)
- if gf_ready: # ---> to merge in one if statement
- total_samples += g_ReadySampleCount
- print("collected {} samples, index = {}, Total: {} samples".format(g_sampleCount, g_startIndex, total_samples))
- if F.closed:
- print("File is not open for writing.-")
- else:
- print ("******---------*********")
- for i in range(total_samples-g_ReadySampleCount, total_samples):
- print (i)
- print(buf_A[i])
- F.write(i + '\t' + str(buf_A[i]) + '\n')
- if i in range (total_samples-g_ReadySampleCount,total_samples-g_ReadySampleCount+10):
- print(tmp_buf_A[i], buf_A[i])
- gf_ready = False
- if not F.closed:
- F.close()
- print(">>>> [3] >>> end of do_while_Streaming()")
- ###############################################################################################
- #
- # Main function
- #
- ###############################################################################################
- if __name__ == '__main__':
- print(DLL)
- # open file to write into
- F = open(FILENAME, 'w')
- current_time = time.asctime(time.localtime(time.time()))
- F.write("Captured values, time is : " + current_time + "\n")
- print("========================== Captured values, time is : ", current_time, "========================== ")
- print()
- print('doing OpenUnit')
- pSTAT = DLL.ps4000OpenUnit(byref(handle))
- if pSTAT:
- print('OpenUnit failed, status=', picostatus(pSTAT), ' handle=', handle)
- else:
- print('OpenUnit ok, handle =', handle)
- if GetDeviceInfo():
- DataStreamingSetup()
- StartDataStreaming()
- do_while_streaming()
- F.close()
- if F.closed: print("-->>> File", FILENAME ," was closed!!! ")
- else: print("-->>> Could not close FILE")
- print('>>> doing ps4000Stop')
- pSTAT = DLL.ps4000Stop(handle)
- print(">>> ps4000Stop() returned: ", picostatus(pSTAT))
- print('>> doing CloseUnit')
- pSTAT = DLL.ps4000CloseUnit(handle)
- if pSTAT == 0: print('>> CloseUnit ok')
- else: print('>> CloseUnit failed, status=', picostatus(pSTAT), ' handle=', handle)
- print("-------- END OF APPLICATION --------")
- else:
- print("********** Could not get device info - Streaming will not start **********")
- print("********** Application will now exit with error-code 1 **********")
- sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement