def waitForMove(): waiting = True while waiting: # print("Current Position", getPosition()) # Wait for "enable" flag to go to 0 indicating that movement has finished. waiting = ljm.eReadAddress(handle, ENABLE_REG, 0) == 1
def monitor_dio_ain(handles, information, display_sec=1, conn_attempt=5, conn_try_sec=3): """ function to display values of some register :param handles: list of connections :param information: information of connections :param display_sec: time between each display :param conn_attempt: number of reconnection try :param conn_try_sec: time between each reconnection try :return: """ threads = [] while not Globals.exiting: time.sleep(display_sec) for handle in Globals.handles: try: print("Device " + str(ljm.getHandleInfo(handle))) print("AIN0: " + str( ljm.eReadAddress(handle, Kernel.T7_AIN0, Kernel.T7_AIN_T))) print("AIN1: " + str( ljm.eReadAddress(handle, Kernel.T7_AIN1, Kernel.T7_AIN_T))) print("FIO0: " + str( ljm.eReadAddress(handle, Kernel.T7_FIO0, Kernel.T7_FIO_T))) print("FIO1: " + str( ljm.eReadAddress(handle, Kernel.T7_FIO1, Kernel.T7_FIO_T))) except ljm.ljm.LJMError: index = Globals.handles.index(handle) info = Globals.information[index] print("Device " + str(info) + " is disconnected") thread = Thread(info, conn_attempt, conn_try_sec) threads.append(thread) Globals.information.remove(Globals.information[index]) Globals.handles.remove(handle) print("Device " + str(info) + " removed from list of devices") print("Trying to reconnect to " + str(info)) thread.start() for thread in threads: if thread.is_alive(): thread.exit(0) return
def sync_monitor(self): try: self.data[0].append( str( ljm.eReadAddress(self.handles[0], Kernel.T7_AIN1, Kernel.T7_AIN_T))) except ljm.ljm.LJMError: print("exiting") sys.exit(-1) self.s.enter(self.time, 1, self.sync_monitor)
https://labjack.com/support/software/api/modbus/modbus-map Hardware Overview(Device Information Registers): https://labjack.com/support/datasheets/t-series/hardware-overview """ from labjack import ljm # Open first found LabJack handle = ljm.openS("ANY", "ANY", "ANY") # Any device, Any connection, Any identifier #handle = ljm.openS("T7", "ANY", "ANY") # T7 device, Any connection, Any identifier #handle = ljm.openS("T4", "ANY", "ANY") # T4 device, Any connection, Any identifier #handle = ljm.open(ljm.constants.dtANY, ljm.constants.ctANY, "ANY") # Any device, Any connection, Any identifier 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 eReadAddress to read a value from the LabJack. address = 60028 # Serial number dataType = ljm.constants.UINT32 result = ljm.eReadAddress(handle, address, dataType) print("\neReadAddress result: ") print(" Address - %i, data type - %i, value : %f" % (address, dataType, result)) # Close handle ljm.close(handle)
def read(self, address, dtype=ljm.constants.FLOAT32): """ To read data directly from a register """ return ljm.eReadAddress(self.handle, address, dtype)
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
counter = 0 #46000 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])) while 1: #for address in [46000, 46002, 46004, 46006]: for address in address_list: # Setup and call eReadAddress to read a value from the LabJack. dataType = ljm.constants.UINT32 try: result = ljm.eReadAddress(handle, address, 3) if address == 60050: result = result - 273.15 print("%i Address - %i, value : %f" % (counter, address, result)) sql = """INSERT INTO freezers.`sensor_readings` ( `device`, `reading`, `recorded`) VALUES ({a}, {r}, NOW() ) ;""".format( a=address, r=result) except: print "Failed to read from LJ or write to db" cursor.execute(sql) print time.sleep(sleep_time) counter += 1
""" Demonstrates how to use the labjack.ljm.eReadAddress (LJM_eReadAddress) function. """ 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 eReadAddress to read a value from the LabJack. address = 60028 # Serial number dataType = ljm.constants.UINT32 result = ljm.eReadAddress(handle, address, dataType) print("\neReadAddress result: ") print(" Address - %i, data type - %i, value : %f" % \ (address, dataType, result)) # Close handle ljm.close(handle)
def read(self,address,dtype=ljm.constants.FLOAT32): """ To read data directly from a register """ return ljm.eReadAddress(self.handle,address,dtype)
def __init__(self, comport_arduino, startsteps, sensitivity, invertx: bool, run_test: str): # Handle input parameters self.port = comport_arduino self.startcount = startsteps self.init_pos = 0 self.sensitivity = float(sensitivity) if self.sensitivity > 1: self.sensitivity = 0.5 logger.info("Invalid sensitivity entered: new value = {}".format( self.sensitivity)) self.invert_x_axis = invertx self.test = run_test # Setup Arduino and Stepper Motors self.board = pyfirmata.Arduino(self.port) time.sleep(1) self.motors = [] self.default_motor_setup() self.dirpull = { 0: [0, 1], 1: [0], 2: [0, 3], 3: [3], 4: [2, 3], 5: [2], 6: [1, 2], 7: [1], } self.dirpush = { 0: [2, 3], 1: [2], 2: [1, 2], 3: [1], 4: [0, 1], 5: [0], 6: [0, 3], 7: [3], } # motor vectors that code for the directions in the x-y space of the needle self.motorvec = { 0: np.array([1, 1]), 1: np.array([-1, 1]), 2: np.array([-1, -1]), 3: np.array([1, -1]), } """ FESTO section !!! to use FESTO: first upload "FESTO_controlv3.lua" to the T7 with Kipling 3 software then close the connection with Kipling 3 software """ # config self.config_object = ConfigParser() self.config_object.read('config.ini') festo = self.config_object["FESTO"] self.init_FESTO_pos = int(festo["initial_pos"]) self.init_FESTO_speed = float(festo["initial_speed"]) self.FESTO_stepsize = int(festo["step_size"]) self.AIN0addr = 0 # position (0-10V) self.DAC0addr = 1000 # speed ref.signal (2.5V) self.DAC1addr = 1002 # speed out signal (-2.5 - 2.5V) self.initialpos_addr = 46000 self.targetpos_addr = 46002 self.speed_addr = 46004 self.enable_addr = 46008 self.f_datatype = ljm.constants.FLOAT32 self.i_datatype = ljm.constants.UINT16 self.offsetV = 2.5 # (offsetV+2.5V on DAC1 = 25 mm/s) self.offV = 0.0299544557929039 # low voltage that T7 can certainly output self.maxpos = 50 # mm self.minpos = 3 # mm self.currentpos = self.init_FESTO_pos try: FESTO_handle = ljm.openS("ANY", "USB", "ANY") except ljm.LJMError as error: FESTO_handle = None logger.error( "No FESTO_handle: thus not able to use the FESTO functions \n Error presented: " + str(error)) if FESTO_handle is not None: self.FESTO_handle = FESTO_handle # Set initial positions (keep target pos at init_FESTO_pos at the start) ljm.eWriteAddress(self.FESTO_handle, self.initialpos_addr, self.f_datatype, self.init_FESTO_pos) ljm.eWriteAddress(self.FESTO_handle, self.targetpos_addr, self.f_datatype, self.init_FESTO_pos) # Set speed ljm.eWriteAddress(self.FESTO_handle, self.speed_addr, self.f_datatype, self.init_FESTO_speed) logger.success( "FESTO connected, handle is available, init is set, current position =" + str( ljm.eReadAddress(self.FESTO_handle, self.AIN0addr, self.f_datatype))) time.sleep(0.3) # Enable init LUA program ljm.eWriteAddress(self.FESTO_handle, self.enable_addr, self.f_datatype, 1) logger.success("FESTO moving to initial position") else: logger.error( "Something went wrong when creating a FESTO Handle. Check if all adresses are correct in needle.py" ) self.FESTO_handle = None
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])) #for address in [0, 2, 4, 6, 8, 46000, 46002, 46004, 46006, 7000, 7001, 7002, 7003, 7004]: #for address in [0, 2, 4, 6, 8, 46000, 46002, 46004, 46006, 7000, 7300, 7600, 7004, 7304, 7604]: #for address in [0, 4, 8, 2000, 2001, 2002, 2005, 2006]: #for item in sorted(reads.keys()): for address in [46000, 46002, 46004, 46006]: #address = reads[item] # Setup and call eReadAddress to read a value from the LabJack. result = ljm.eReadAddress(handle, address, 3) print address, result string = ljm.eReadNameString(handle, "DEVICE_NAME_DEFAULT") print "--------------\n{}\n------------\n".format(string ) #ljm.eWriteNameString(handle, "DEVICE_NAME_DEFAULT", "CTLGH_-80_T7") result = ljm.eReadAddress(handle, 60050, 3) - 273.15 # also read internal temperature and convert from K to C print "--------------\n{:.1f} C\n------------\n".format(result ) time.sleep(10) counter+=1