#Labjack streaming by Rohan Lele ######################################################################## global STREAM_OUT0_BUFFER_SIZE_BYTES STREAM_OUT0_BUFFER_SIZE_BYTES = 512 global STREAM_OUT0_LOOP_SIZE STREAM_OUT0_LOOP_SIZE = 128 global SLEEP_TIME SLEEP_TIME = 1/(STREAM_OUT0_LOOP_SIZE-1) #This is the address of stream out0 buffer(4800) global STREAM_OUT0 STREAM_OUT0 = 4800 def setupStream(LJHandle): #Get the address of DAC0 channel #nameToAddress is a function which takes string input of device name #and outputs a list with address and type of the device. #Here address is first element of list and type is second. target = ljm.nameToAddress("DAC0")[0] # Allocate memory for the stream-out buffer #eWriteName - This function will write a value to the device register try: #Disable stream_out0_enable by writing 0 to it, in case it was already enabled ljm.eWriteName(LJHandle, "STREAM_OUT0_ENABLE", 0) #Write the address of DAC0 to stream_out0_target ljm.eWriteName(LJHandle,"STREAM_OUT0_TARGET",target) #Write the value of stream_out0_buffer_size (512 in this case) ljm.eWriteName(LJHandle, "STREAM_OUT0_BUFFER_SIZE", STREAM_OUT0_BUFFER_SIZE_BYTES) #Enable stream_out0_enable by writing 1 to it ljm.eWriteName(LJHandle, "STREAM_OUT0_ENABLE", 1) except ljm.LJMError: print "\nFunction setupStream\n",sys.exc_info()[1] return ################################################################### #Arguments: #LJHandle - Handle of opened labjack device #vdc_scan_rate - Rate at which samples are needed to be streamed #streamOutList - list of samples to be streamed def startStream(LJHandle,vdc_scan_rate,streamOutList): SCAN_LIST = [STREAM_OUT0] #Write the loop size 128 to the stream out loop size. ljm.eWriteName(LJHandle, "STREAM_OUT0_LOOP_SIZE", STREAM_OUT0_LOOP_SIZE) # Configure and start stream #1 - number of addresses in scanList(here only one as DAC0) #SCAN_LIST - list of all the addresses to where data is to be streamed. Here only one DAC0 scan_rate = ljm.eStreamStart(LJHandle, vdc_scan_rate, 1, SCAN_LIST, vdc_scan_rate) #Write the first set of 128 values to the out buffer since i=0 i=0 try: while 1: #Get the stream out0 buffer status bs = ljm.eReadName(LJHandle, "STREAM_OUT0_BUFFER_STATUS") #If the buffer status is greater than half the buffer size if bs >= STREAM_OUT0_LOOP_SIZE: #Write the next 128 values into the buffer #------------------------------------------------------------- #Write the values in the buffer - This is NOT working. Please explain. #ljm.eWriteNameArray(LJHandle, "STREAM_OUT0_BUFFER_F32", STREAM_OUT0_LOOP_SIZE, streamOutList[i:i+STREAM_OUT0_LOOP_SIZE]) #Write the values in the buffer and for sampleValue in streamOutList[i:i+STREAM_OUT0_LOOP_SIZE]: ljm.eWriteName(LJHandle, "STREAM_OUT0_BUFFER_F32", sampleValue) #Set the stream out loop to 1 ljm.eWriteName(LJHandle, "STREAM_OUT0_SET_LOOP", 1) #------------------------------------------------------------- i=i+STREAM_OUT0_LOOP_SIZE #If sample values are over, break and exit if len(streamOutList[i:i+STREAM_OUT0_LOOP_SIZE])== 0: break #Wait for buffer to empty before checking again time.sleep(SLEEP_TIME) except ljm.LJMError: print "\tFunction startStream\n",sys.exc_info()[1] exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print(exc_type, fname, exc_tb.tb_lineno) return ###################################################################