# -*- coding: utf-8 -*- """ Created on Mon Nov 20 11:19:20 2017 @author: speckle """ import numpy as np from labjack import ljm import time import datetime import sys def openLabJack(): # Open first found LabJack handle = ljm.open(ljm.constants.dtANY, ljm.constants.ctANY, "ANY") #handle = ljm.openS("ANY", "ANY", "ANY") try: # Stop stream just in case it is already running. Ignore any errors. ljm.eWriteNames(handle, STREAM_ENABLE, 0) except: pass info = ljm.getHandleInfo(handle) print("Opened a LabJack with Device type: %i, Connection type: %i,\n" \ "Serial number: %i, IP address: %s, Port: %i,\nMax bytes per MB: %i" % \ (info[0], info[1], info[2], ljm.numberToIP(info[3]), info[4], info[5])) err=ljm.eWriteName(handle, "IO_CONFIG_SET_CURRENT_TO_FACTORY", 1) return handle def closeLabJack(handle): # Close handle ljm.close(handle) print("LabJack closed...") def tempToVoltage(temp): return 2.5-0.1*temp def updateVoltage(handle,voltage,thepause): #0V -> 25°C #+1V -> -10°C #print("\n##########################################################") #print("########### Voltage: % 6.3f V, pause of %4.1f s ###########"%(voltage,thepause)) #print("##########################################################") #err=ljm.eWriteName(handle, "TDAC02", voltage) time.sleep(thepause) def config_DIO_EF(handle): print("Configuring DIO_EF") #Set up clock source for PWM output. PWM will be the stream trigger source. aNames = ["DIO_EF_CLOCK0_ENABLE", "DIO_EF_CLOCK0_DIVISOR", "DIO_EF_CLOCK0_OPTIONS", "DIO_EF_CLOCK0_ROLL_VALUE", "DIO_EF_CLOCK0_ENABLE"] aValues = [0, 8, 0, 2000000, 1] ljm.eWriteNames(handle, len(aNames), aNames, aValues) #Configure the trigger source. aNames = ["DIO0_EF_ENABLE", "DIO0_EF_INDEX", "DIO0_EF_OPTIONS", "DIO0_EF_VALUE_A", "DIO0_EF_ENABLE"] aValues = [0, 3, 0, 2, 1] ljm.eWriteNames(handle, len(aNames), aNames, aValues) #Configure the PWM. aNames = ["DIO2_EF_ENABLE", "DIO2_EF_INDEX", "DIO2_EF_OPTIONS", "DIO2_EF_VALUE_A", "DIO2_EF_ENABLE"] aValues = [0, 0, 0, 1000000, 1] ljm.eWriteNames(handle, len(aNames), aNames, aValues) def acquireSpectrum(handle): MAX_REQUESTS = 1 # The number of eStreamRead calls that will be performed. FIRST_AIN_CHANNEL = 0 #AIN0 NUMBER_OF_AINS = 3 # Stream Configuration aScanListNames = ["AIN0"] numAddresses = len(aScanListNames) aScanList = ljm.namesToAddresses(numAddresses, aScanListNames)[0] scanRate = 1000 scansPerRead = int(1.*scanRate) Trigger_Edge = -1 # 1 = Rising, 0 = Falling, -1 = Something Broke ain=np.zeros((numAddresses,scansPerRead)) try: aNames = ["AIN_ALL_NEGATIVE_CH", "AIN_ALL_RANGE", "STREAM_SETTLING_US", "STREAM_RESOLUTION_INDEX"] aValues = [ljm.constants.GND, 10.0, 0, 0] #single-ended, +/-10V, 0 (default), (default) ljm.eWriteNames(handle, len(aNames), aNames, aValues) #Do-it # Configure and start stream aNames = ["STREAM_TRIGGER_INDEX"]#, "STREAM_NUM_SCANS"] aValues = [2000, scansPerRead] ljm.eWriteNames(handle, len(aNames), aNames, aValues) scanRate = ljm.eStreamStart(handle, scansPerRead, numAddresses, aScanList, scanRate) totScans = 0 totSkip = 0 # Total skipped samples i = 1 while i <= MAX_REQUESTS: try: ret = ljm.eStreamRead(handle) data = ret[0] #The PWM is running at 5 Hz and 50% duty cycle. Stream is scanning at 1 kHz. #The first 100 values should be high: (3.3 V) #Check a couple of the values in the first 100 to determine the trigger edge. if data[25] > 3.0 and data[50] > 3.0 and data[75] > 3.0 : Trigger_Edge = 1 else : Trigger_Edge = 0 scans = len(data)/numAddresses totScans += scans except Exception as e: print("oups... %s"%e) i += 1 ljm.eStreamStop(handle) except ljm.LJMError: ljme = sys.exc_info()[1] print(ljme) except Exception: e = sys.exc_info()[1] print(e) #print("trigger edge: %s" % Trigger_Edge) return Trigger_Edge