# -*- 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") 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 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","AIN1","AIN13"] #aScanListNames = ["AIN%i"%i for i in range(FIRST_AIN_CHANNEL, FIRST_AIN_CHANNEL+NUMBER_OF_AINS)] #Scan list names #aScanListNames.append("DIO0_EF_READ_A") aScanListNames.append("FIO_STATE") #print("\nScan List = " + " ".join(aScanListNames)) numAddresses = len(aScanListNames) aScanList = ljm.namesToAddresses(numAddresses, aScanListNames)[0] scanRate = 10000 scansPerRead = int(1.*scanRate) ain=np.zeros((numAddresses,scansPerRead)) try: # Configure the analog inputs' negative channel, range, settling time and # resolution. # Note when streaming, negative channels and ranges can be configured for # individual analog inputs, but the stream has only one settling time and # resolution. #aNames = ["AIN_ALL_NEGATIVE_CH", "AIN0_RANGE", "AIN1_RANGE", "AIN2_RANGE", "STREAM_SETTLING_US", # "STREAM_RESOLUTION_INDEX"] #AIN_ALL_RANGE=Valid values/ranges: 0.0=Default=> +/-10V, 10.0=> +/-10V, 1.0=> +/-1V, 0.1=> +/-0.1V, or 0.01=> +/-0.01V. #STREAM_RESOLUTION_INDEX= #aValues = [ljm.constants.GND, 10.0, 10.0, 1.0, 0, 0] #single-ended, +/-10V, 0 (default), #0 (default) aNames = ["AIN_ALL_NEGATIVE_CH", "AIN_ALL_RANGE", "STREAM_SETTLING_US", "STREAM_RESOLUTION_INDEX"] #AIN_ALL_RANGE=Valid values/ranges: 0.0=Default=> +/-10V, 10.0=> +/-10V, 1.0=> +/-1V, 0.1=> +/-0.1V, or 0.01=> +/-0.01V. #STREAM_RESOLUTION_INDEX= aValues = [ljm.constants.GND, 10.0, 0, 0] #single-ended, +/-10V, 0 (default), #0 (default) ljm.eWriteNames(handle, len(aNames), aNames, aValues) aNames = ["DIO0_EF_ENABLE"] #1 Disable DIO2_EF aValues = [0] ljm.eWriteNames(handle, len(aNames), aNames, aValues) aNames = ["STREAM_TRIGGER_INDEX", "DIO0_EF_INDEX"] #2002 sets DIO2 / FIO2 as the stream trigger #0 Clear any previous DIO0_EF settings #5 enables a rising or falling edge to trigger stream #1 Enable DIO0_EF aValues = [2000, 3] ljm.eWriteNames(handle, len(aNames), aNames, aValues) aNames = ["DIO0_EF_ENABLE"] #1 Enable DIO0_EF aValues = [1] ljm.eWriteNames(handle, len(aNames), aNames, aValues) # Configure and start stream scanRate = ljm.eStreamStart(handle, scansPerRead, numAddresses, aScanList, scanRate) #print("Stream started with a scan rate of %0.0f Hz." % scanRate) #print("\nPerforming %i stream reads." % MAX_REQUESTS) #start = datetime.datetime.now() totScans = 0 totSkip = 0 # Total skipped samples i = 1 while i <= MAX_REQUESTS: #start2 = datetime.datetime.now() try: ret = ljm.eStreamRead(handle) data = ret[0] # Count the skipped samples which are indicated by -9999 values. Missed # samples occur after a device's stream buffer overflows and are # reported after auto-recover mode ends. curSkip = data.count(-9999.0) totSkip += curSkip #deinterlace the data for j in range(numAddresses): ain[j,:]=data[j::numAddresses] scans = len(data)/numAddresses totScans += scans except Exception as e: print("oups... %s"%e) #end2 = datetime.datetime.now() #print(end2-start2) #print("\neStreamRead %i" % i) #ainStr = "" #for j in range(0, numAddresses): # ainStr += "%s = %0.5f " % (aScanListNames[j], data[j]) #print(" 1st scan out of %i: %s" % (scans, ainStr)) #print(" Scans Skipped = %0.0f, Scan Backlogs: Device = %i, LJM = " \ # "%i" % (curSkip/numAddresses, ret[1], ret[2])) i += 1 #end = datetime.datetime.now() #print("\nTotal scans = %i" % (totScans)) #tt = (end-start).seconds + float((end-start).microseconds)/1000000 #print("Time taken = %f seconds" % (tt)) #print("LJM Scan Rate = %f scans/second" % (scanRate)) #print("Timed Scan Rate = %f scans/second" % (totScans/tt)) #print("Timed Sample Rate = %f samples/second" % (totScans*numAddresses/tt)) #print("Skipped scans = %0.0f" % (totSkip/numAddresses)) #print("Stop Stream") ljm.eStreamStop(handle) except ljm.LJMError: ljme = sys.exc_info()[1] print(ljme) except Exception: e = sys.exc_info()[1] print(e) return ain