def readLuaInfo(handle): """Function that selects the current lua execution block and prints out associated info from lua """ try: for i in range(20): # The script sets the interval length with LJ.IntervalConfig. # Note that LJ.IntervalConfig has some jitter and that this program's # interval (set by sleep) will have some minor drift from # LJ.IntervalConfig. sleep(1) print("LUA_RUN: %d" % ljm.eReadName(handle, "LUA_RUN")) # Add custom logic to control the Lua execution block executionLoopNum = i % 3 # Write which lua control block to run using the user ram register ljm.eWriteName(handle, "USER_RAM0_U16", executionLoopNum) numBytes = ljm.eReadName(handle, "LUA_DEBUG_NUM_BYTES") if (int(numBytes) == 0): continue print("LUA_DEBUG_NUM_BYTES: %d\n" % numBytes) aBytes = ljm.eReadNameByteArray(handle, "LUA_DEBUG_DATA", int(numBytes)) luaMessage = "".join([("%c" % val) for val in aBytes]) print("LUA_DEBUG_DATA: %s" % luaMessage) except ljm.LJMError: print("Error while running the main loop") raise
def readValueI2C(self): numBytes = 6 ljm.eWriteName(self.handle, "I2C_NUM_BYTES_TX", 0) ljm.eWriteName(self.handle, "I2C_NUM_BYTES_RX", numBytes) aBytes = ljm.eReadNameByteArray(self.handle, "I2C_DATA_RX", numBytes) ljm.eWriteName(self.handle, "I2C_GO", 1) resp = '' for i in aBytes: if (i > 1 and i < 255): resp = resp + ''.join(chr(i)) if (resp == ''): return 0 return float(resp)
def readFile(handle, sdPath): """Return the file contents of the specified path as a string """ sdPath = sanitizePath(sdPath) path, filename = os.path.split(sdPath) if path: raise ValueError('cannot accept a file path that is not in the cwd') # path = sanitizePath(path) # goToPath(handle, path) # This would need to be improved by adding a try/finally so that # readFile returns to the cwd before readFile was called dir_contents = getCurDirContents(handle) # Get file size from the directory contents try: fileSize = dir_contents[filename][0] except KeyError as excep: raise ValueError('File not found: %s' % (sdPath)) # 1) Write the length of the file name to FILE_IO_PATH_WRITE_LEN_BYTES (add # 1 for the null terminator); filename_len = len(filename) ljm.eWriteName(handle, "FILE_IO_PATH_WRITE_LEN_BYTES", filename_len) # 2) Write the name to FILE_IO_NAME_WRITE (with null terminator) filenameBytes = sdPath.encode() ljm.eWriteNameByteArray(handle, "FILE_IO_PATH_WRITE", filename_len, filenameBytes) # 3) Write any value to FILE_IO_OPEN ljm.eWriteName(handle, "FILE_IO_OPEN", 1) # 4) Read file data from FILE_IO_READ (using the size from FILE_IO_SIZE) fileDataBytes = ljm.eReadNameByteArray(handle, "FILE_IO_READ", fileSize) # 5) Write a value of 1 to FILE_IO_CLOSE ljm.eWriteName(handle, "FILE_IO_CLOSE", 1) # Convert data bytes to string fileData = "".join(chr(x) for x in fileDataBytes) return fileData
def getCWD(handle): """Returns the SD card system's current working directory as a string. """ # 1) Write a value of 1 to FILE_IO_DIR_CURRENT. The error returned # indicates whether there is a directory loaded as current. No error (0) # indicates a valid directory. ljm.eWriteName(handle, "FILE_IO_DIR_CURRENT", 1) # 2) Read FILE_IO_PATH_READ_LEN_BYTES. len = int(ljm.eReadName(handle, "FILE_IO_PATH_READ_LEN_BYTES")) # 3) Read an array of size FILE_IO_PATH_READ_LEN_BYTES from # FILE_IO_PATH_READ nameInBytes = ljm.eReadNameByteArray(handle, "FILE_IO_PATH_READ", len) # convert to string nameStr = "".join(chr(x) for x in nameInBytes) return nameStr
def getCurDirContents(handle): """Return the current working directory's contents as an iterable. """ # 1) Write a value of 1 to FILE_IO_DIR_FIRST. The error returned indicates # whether anything was found. No error (0) indicates that something was # found. FILE_IO_NOT_FOUND (2960) indicates that nothing was found. ljm.eWriteName(handle, "FILE_IO_DIR_FIRST", 1) # Loop reading name and properties of one file per iteration more_files = True dirContents = {} while more_files: # 2) Read FILE_IO_PATH_READ_LEN_BYTES, FILE_IO_ATTRIBUTES, and # FILE_IO_SIZE_BYTES len_file_name_as_bytes = int( ljm.eReadName(handle, "FILE_IO_PATH_READ_LEN_BYTES")) size = (int(ljm.eReadName(handle, "FILE_IO_SIZE_BYTES"))) attr = (int(ljm.eReadName(handle, "FILE_IO_ATTRIBUTES"))) # 3) Read an array of size FILE_IO_PATH_READ_LEN_BYTES from # FILE_IO_PATH_READ. file_name_as_bytes = ljm.eReadNameByteArray(handle, "FILE_IO_PATH_READ", len_file_name_as_bytes) # convert to string file_name_as_strings = "".join(chr(x) for x in file_name_as_bytes) dirContents[file_name_as_strings] = (size, attr) # 4) Write a value of 1 to FILE_IO_DIR_NEXT. The error returned # indicates whether anything was found. No error (0) indicates that # there are more items->go back to step 2. FILE_IO_INVALID_OBJECT # (2809) and potentially error code FILE_IO_NOT_FOUND (2960) # indicates that there are no more items->Done. try: ljm.eWriteName(handle, "FILE_IO_DIR_NEXT", 1) except: more_files = False return dirContents
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 # Display the bytes written print("") for i in range(numBytes): print("dataWrite[%i] = %0.0f" % (i, dataWrite[i])) # Read the bytes dataRead = ljm.eReadNameByteArray(handle, "SPI_DATA_RX", numBytes) # Display the bytes read print("") for i in range(numBytes): print("dataRead[%i] = %0.0f" % (i, dataRead[i])) # Close handle ljm.close(handle)
1) # Set the number of bytes to transmit ljm.eWriteName(handle, "I2C_NUM_BYTES_RX", 4) # Set the number of bytes to receive # Set the TX bytes. We are sending 1 byte for the address. numBytes = 1 aBytes = [0] # Byte 0: Memory pointer = 0 ljm.eWriteNameByteArray(handle, "I2C_DATA_TX", numBytes, aBytes) ljm.eWriteName(handle, "I2C_GO", 1) # Do the I2C communications. # Read the RX bytes. numBytes = 4 # aBytes[0] to aBytes[3] will contain the data aBytes = [0] * 4 aBytes = ljm.eReadNameByteArray(handle, "I2C_DATA_RX", numBytes) print("\nRead User Memory [0-3] = %s" % " ".join([("%.0f" % val) for val in aBytes])) # Write EEPROM bytes 0-3 in the user memory area, using the page write # technique. Note that page writes are limited to 16 bytes max, and must be # aligned with the 16-byte page intervals. For instance, if you start writing # at address 14, you can only write two bytes because byte 16 is the start of a # new page. ljm.eWriteName(handle, "I2C_NUM_BYTES_TX", 5) # Set the number of bytes to transmit ljm.eWriteName(handle, "I2C_NUM_BYTES_RX", 0) # Set the number of bytes to receive # Set the TX bytes.
def _read_array(self, register, num_bytes): return ljm.eReadNameByteArray(self.handle, "I2C_DATA_RX", read_bytes)
def t7_startup_check(lj_handle): """Read various parameters from the LabJack T7 device. Parameters that are read from the LabJack T7 include code versions, hardware serial number, etc. The information is put into a monitor point dictionary, and the values are compared with the requirements. Args: lj_handle (int): Unique handle for the LabJack driver for this antenna. Returns: start_up_state (dict): Dictionary of monitor points with the startup information acquired. """ start_up_state = dict(factory=False, prod_id=-1, hw_ver=0.0, fw_ver=0.0, boot_ver=0.0, ser_no=0, dev_name='', lua_running=False, lua_code_ver=-1, config_valid=True) # Read relevant device information and configuration registers. start_up_state['factory'] = bool( ljm.eReadName(lj_handle, 'IO_CONFIG_CHECK_FOR_FACTORY')) start_up_state['prod_id'] = int(ljm.eReadName(lj_handle, 'PRODUCT_ID')) start_up_state['hw_ver'] = format( float(ljm.eReadName(lj_handle, 'HARDWARE_VERSION')), '.3f') start_up_state['fw_ver'] = format( float(ljm.eReadName(lj_handle, 'FIRMWARE_VERSION')), '.3f') start_up_state['boot_ver'] = format( float(ljm.eReadName(lj_handle, 'BOOTLOADER_VERSION')), '.3f') start_up_state['ser_no'] = int(ljm.eReadName(lj_handle, 'SERIAL_NUMBER')) dev_name = bytes( ljm.eReadNameByteArray(lj_handle, 'DEVICE_NAME_DEFAULT', 49)) d_name = '' for device in dev_name: if device == 0: break d_name += chr(device) start_up_state['dev_name'] = d_name start_up_state['lua_running'] = bool(ljm.eReadName(lj_handle, 'LUA_RUN')) if start_up_state['lua_running'] is False: print('Lua script not running. Attempting to load and start script') ljm.eWriteName(lj_handle, 'LUA_LOAD_SAVED', 1) time.sleep(2.0) ljm.eWriteName(lj_handle, 'LUA_RUN', 1) time.sleep(2.0) start_up_state['lua_running'] = bool( ljm.eReadName(lj_handle, 'LUA_RUN')) if start_up_state['lua_running'] is False: print('Failed to start script') start_up_state['config_valid'] = False start_up_state['lua_code_ver'] = format( float(ljm.eReadAddress(lj_handle, 46000, 3)), '.3f') for k, val in start_up_state.items(): print(" --{}: {}".format(k, val)) if start_up_state['factory'] is False or start_up_state['prod_id'] != 7: start_up_state['config_valid'] = False return start_up_state
aValues = [function, numTX, numRX, romH, romL, pathH, pathL] ljm.eWriteNames(handle, len(aNames), aNames, aValues) ljm.eWriteNameByteArray(handle, "ONEWIRE_DATA_TX", numTX, dataTX) ljm.eWriteName(handle, "ONEWIRE_GO", 1) # Read the binary temperature. print("Read the binary temperature.") function = 0x55 # Match numTX = 1 dataTX = [0xBE] # 0xBE = DS1822 Read scratchpad command numRX = 2 aNames = [ "ONEWIRE_FUNCTION", "ONEWIRE_NUM_BYTES_TX", "ONEWIRE_NUM_BYTES_RX", "ONEWIRE_ROM_MATCH_H", "ONEWIRE_ROM_MATCH_L", "ONEWIRE_PATH_H", "ONEWIRE_PATH_L" ] aValues = [function, numTX, numRX, romH, romL, pathH, pathL] ljm.eWriteNames(handle, len(aNames), aNames, aValues) ljm.eWriteNameByteArray(handle, "ONEWIRE_DATA_TX", numTX, dataTX) ljm.eWriteName(handle, "ONEWIRE_GO", 1) dataRX = ljm.eReadNameByteArray(handle, "ONEWIRE_DATA_RX", numRX) temperature = (int(dataRX[0]) + (int(dataRX[1]) << 8)) if temperature == 0x0550: print("The DS1822 power on reset value is 85 C.") print("Read again get the real temperature.") else: temperature = temperature * 0.0625 print("Temperature = %f C" % temperature) ljm.close(handle)