def read_device_values(device, registers): """Read a set of values from the device. @param device: The device to read the values from. @type device: LJM handle @param registers: List of register names to read. @type registers: list of str @return: Mapping from register name to that register's value as read from the device. @rtype: dict (str to float) """ ret_dict = {} values = ljm.eReadNames(device, len(registers), registers) return dict(zip(registers, values))
def getData(self,mock=None): """Read the signal on all pre-definid channels, one by one.""" try: #numFrames = len(channels) #names = ["AIN"+str(chan) if type(chan)!=str else chan for chan in channels] #results = ljm.eReadNames(self.handle, numFrames, names) results = ljm.eReadNames(self.handle, self.nchans, self.channels) results=[a*b+c for a,b,c in zip(results,self.gain,self.offset)] #ret=OrderedDict(zip(['t(s)'],[time.time()])) #ret.update(OrderedDict(zip(channels,results))) #results.insert(0,) return time.time(),results except KeyboardInterrupt: self.close() except Exception: print(sys.exc_info()[1]) self.close() raise
def AIN_Read(handle,AIN): return ljm.eReadNames(handle,1 , [AIN])
from labjack import ljm # 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])) # Setup and call eReadNames to read the Watchdog config. from the LabJack. aNames = ["WATCHDOG_ENABLE_DEFAULT", "WATCHDOG_ADVANCED_DEFAULT", "WATCHDOG_TIMEOUT_S_DEFAULT", "WATCHDOG_STARTUP_DELAY_S_DEFAULT", "WATCHDOG_STRICT_ENABLE_DEFAULT", "WATCHDOG_STRICT_KEY_DEFAULT", "WATCHDOG_RESET_ENABLE_DEFAULT", "WATCHDOG_DIO_ENABLE_DEFAULT", "WATCHDOG_DIO_STATE_DEFAULT", "WATCHDOG_DIO_DIRECTION_DEFAULT", "WATCHDOG_DIO_INHIBIT_DEFAULT", "WATCHDOG_DAC0_ENABLE_DEFAULT", "WATCHDOG_DAC0_DEFAULT", "WATCHDOG_DAC1_ENABLE_DEFAULT", "WATCHDOG_DAC1_DEFAULT"] numFrames = len(aNames) results = ljm.eReadNames(handle, numFrames, aNames) print("\nWatchdog configuration:") for i in range(numFrames): print(" %s : %f" % (aNames[i], results[i])) # Close handle ljm.close(handle)
# 0 = DIO directions are automatically changed # 1 = DIO directions are not automatically changed. # bits 2-3: Reserved # bits 4-7: Number of bits in the last byte. 0 = 8. # bits 8-15: Reserved # Enabling active low clock select pin ljm.eWriteName(handle, "SPI_OPTIONS", 0) # Read back and display the SPI settings aNames = ["SPI_CS_DIONUM", "SPI_CLK_DIONUM", "SPI_MISO_DIONUM", "SPI_MOSI_DIONUM", "SPI_MODE", "SPI_SPEED_THROTTLE", "SPI_OPTIONS"] aValues = [0]*len(aNames) numFrames = len(aNames) aValues = ljm.eReadNames(handle, numFrames, aNames) print("SPI Configuration:") for i in range(numFrames): print(" %s = %0.0f" % (aNames[i], aValues[i])) # Write(TX)/Read(RX) 4 bytes numBytes = 4; ljm.eWriteName(handle, "SPI_NUM_BYTES", numBytes) # Write the bytes dataWrite = [] dataWrite.extend([randrange(0, 256) for _ in range(numBytes)]) aNames = ["SPI_DATA_TX"]
SB_Is_Done.value = 1 # print 'spetp 2' #print Intensities I += 1 SB_Current_Record[:] = Intensities # print "Is Done" return I = 0 II = 0 numFrames = 1 names = ["AIN0"] results = ljm.eReadNames(DAQ_handle, numFrames, names) read_signal[I] = results[0] read_time[I] = time.time() I += 1 for Integration_index in Integration_list: #SB.Init(Spec_handle,Integration_index,3) Process(target=SB_Init, args=(Spec_handle,Integration_index,3)).start() Process(target=SB_Main, args=(Spec_handle,1)).start() Half_Cycle = No_Sample*(II) + No_Sample/2 Full_Cycle = No_Sample*(II) + No_Sample ljm.eWriteName(DAQ_handle, "DAC1", 5) #Laser is on
def main( initial_scan_rate_hz=INITIAL_SCAN_RATE_HZ, in_names=IN_NAMES, stream_outs=STREAM_OUTS, num_cycles=NUM_CYCLES ): print("Beginning...") handle = open_ljm_device(ljm.constants.dtANY, ljm.constants.ctANY, "ANY") print_device_info(handle) print("Initializing stream out buffers...") out_contexts = [] for stream_out in stream_outs: out_context = ljm_stream_util.create_out_context(stream_out) ljm_stream_util.initialize_stream_out(handle, out_context) out_contexts.append(out_context) print("") for out_context in out_contexts: print_register_value(handle, out_context["names"]["buffer_status"]) for out_context in out_contexts: update_str = "Updating %(stream_out)s buffer whenever " \ "%(buffer_status)s is greater or equal to " % out_context["names"] print(update_str + str(out_context["state_size"])) scans_per_read = int(min([context["state_size"] for context in out_contexts])) buffer_status_names = [out["names"]["buffer_status"] for out in out_contexts] try: scan_list = ljm_stream_util.create_scan_list( in_names=in_names, out_contexts=out_contexts ) print("scan_list: " + str(scan_list)) print("scans_per_read: " + str(scans_per_read)) scan_rate = ljm.eStreamStart(handle, scans_per_read, len(scan_list), scan_list, initial_scan_rate_hz) print("\nStream started with a scan rate of %0.0f Hz." % scan_rate) print("\nPerforming %i buffer updates." % num_cycles) iteration = 0 total_num_skipped_scans = 0 while iteration < num_cycles: buffer_statuses = [0] infinity_preventer = 0 while max(buffer_statuses) < out_context["state_size"]: buffer_statuses = ljm.eReadNames( handle, len(buffer_status_names), buffer_status_names ) infinity_preventer = infinity_preventer + 1 if infinity_preventer > scan_rate: raise ValueError( "Buffer statuses don't appear to be updating:" + \ str(buffer_status_names) + str(buffer_statuses) ) for out_context in out_contexts: ljm_stream_util.update_stream_out_buffer(handle, out_context) # ljm.eStreamRead will sleep until data has arrived stream_read = ljm.eStreamRead(handle) num_skipped_scans = ljm_stream_util.process_stream_results( iteration, stream_read, in_names, device_threshold=out_context["state_size"], ljm_threshold=out_context["state_size"] ) total_num_skipped_scans += num_skipped_scans iteration = iteration + 1 except ljm.LJMError: ljm_stream_util.prepare_for_exit(handle) raise except Exception: ljm_stream_util.prepare_for_exit(handle) raise ljm_stream_util.prepare_for_exit(handle) print("Total number of skipped scans: %d" % total_num_skipped_scans)
if len(sys.argv) > 1: #An argument was passed. The first argument specfies how many times to loop. try: loopAmount = int(sys.argv[1]) except: raise Exception("Invalid first argument \"%s\". This specifies how many " \ "times to loop and needs to be a number." % str(sys.argv[1])) else: #An argument was not passed. Loop an infinite amount of times. loopAmount = float("inf") s = " Press Ctrl+C to stop." print("\nStarting %s read loops.%s" % (str(loopAmount), s)) delay = 1 #delay between readings (in sec) i = 0 while i < loopAmount: try: results = ljm.eReadNames(handle, numFrames, names) print("\nAIN0 : %f V, AIN1 : %f V" % (results[0], results[1])) time.sleep(delay) i = i + 1 except KeyboardInterrupt: break except Exception: import sys print(sys.exc_info()[1]) break # Close handle ljm.close(handle)
def read(self, names): return ljm.eReadNames(self.handle, len(names), names)
def get_data(self): """ Short version, only used for eval_offset """ return [time()]+ljm.eReadNames(self.handle, len(self.chan_list), [c['name'] for c in self.chan_list])
for i in range(numFrames): print(" %s : %f" % (names[i], aValues[i])) # Read AIN0 and AIN1 from the LabJack with eReadNames in a loop. read_names = ["AIN1"] num_read_frames = len(read_names) print("\nStarting %s read loops.%s\n" % (str(loopAmount), loopMessage)) interval_handle = 1 ljm.startInterval(interval_handle, 100000) # Delay between readings (in microseconds) dac1_out = 10 ljm.eWriteNames(handle, 1, ["DAC1"], [dac1_out]) while True: try: results = ljm.eReadNames(handle, num_read_frames, read_names) time = datetime.now() labjack_value_0 = results[0] print("Time: {} Value: {:.3f} V".format(time, labjack_value_0)) ljm.waitForNextInterval(interval_handle) except Exception: print(sys.exc_info()[1]) break # Close handles ljm.cleanInterval(interval_handle) ljm.close(handle)
print(" DPU pin = %d" % dpuPin) print(" Options = %d" % options) # Search for the 1-Wire device and get its ROM ID and path. function = 0xF0 # Search numTX = 0 numRX = 0 aNames = ["ONEWIRE_FUNCTION", "ONEWIRE_NUM_BYTES_TX", "ONEWIRE_NUM_BYTES_RX"] aValues = [function, numTX, numRX] ljm.eWriteNames(handle, len(aNames), aNames, aValues) ljm.eWriteName(handle, "ONEWIRE_GO", 1) aNames = [ "ONEWIRE_SEARCH_RESULT_H", "ONEWIRE_SEARCH_RESULT_L", "ONEWIRE_ROM_BRANCHS_FOUND_H", "ONEWIRE_ROM_BRANCHS_FOUND_L" ] aValues = ljm.eReadNames(handle, len(aNames), aNames) romH = aValues[0] romL = aValues[1] rom = (int(romH) << 8) + int(romL) pathH = aValues[2] pathL = aValues[3] path = (int(pathH) << 8) + int(pathL) print(" ROM ID = %d" % rom) print(" Path = %d" % path) # Setup the binary temperature read. print("Setup the binary temperature read.") function = 0x55 # Match numTX = 1 dataTX = [0x44] # 0x44 = DS1822 Convert T command numRX = 0
(info[0], info[1], info[2], ljm.numberToIP(info[3]), info[4], info[5])) # Ensure that a lua script is running on the T7. If not, end the python script. if (ljm.eReadName(handle, "LUA_RUN") != 1): print("There is no Lua script running on the device.") print("Please use Kipling to begin a Lua script on the device.") sys.exit() # Load the calibration consants for the BMP180 from USER_RAM Registers. calAddrNames = [ "USER_RAM11_F32", "USER_RAM12_F32", "USER_RAM13_F32", "USER_RAM14_F32", "USER_RAM15_F32", "USER_RAM16_F32", "USER_RAM17_F32", "USER_RAM18_F32", "USER_RAM19_F32", "USER_RAM20_F32", "USER_RAM21_F32" ] calConst = ljm.eReadNames(handle, 11, calAddrNames) ac1 = calConst[0] ac2 = calConst[1] ac3 = calConst[2] ac4 = calConst[3] ac5 = calConst[4] ac6 = calConst[5] b1 = calConst[6] b2 = calConst[7] mb = calConst[8] mc = calConst[9] md = calConst[10] while True: try: ut = ljm.eReadName(handle, "USER_RAM9_F32")
# 1 = DIO directions are not automatically changed. # bits 2-3: Reserved # bits 4-7: Number of bits in the last byte. 0 = 8. # bits 8-15: Reserved # Enabling active low clock select pin ljm.eWriteName(handle, "SPI_OPTIONS", 0) # Read back and display the SPI settings aNames = [ "SPI_CS_DIONUM", "SPI_CLK_DIONUM", "SPI_MISO_DIONUM", "SPI_MOSI_DIONUM", "SPI_MODE", "SPI_SPEED_THROTTLE", "SPI_OPTIONS" ] aValues = [0] * len(aNames) numFrames = len(aNames) aValues = ljm.eReadNames(handle, numFrames, aNames) print("\nSPI Configuration:") for i in range(numFrames): print(" %s = %0.0f" % (aNames[i], aValues[i])) # Write(TX)/Read(RX) 4 bytes numBytes = 4 ljm.eWriteName(handle, "SPI_NUM_BYTES", numBytes) # Write the bytes dataWrite = [] dataWrite.extend([randrange(0, 256) for _ in range(numBytes)]) ljm.eWriteNameByteArray(handle, "SPI_DATA_TX", len(dataWrite), dataWrite) ljm.eWriteName(handle, "SPI_GO", 1) # Do the SPI communications