def AvailableDevices(self, callback=None, force=False): if self.available_devices is None or force: if callback is not None: # Create a thread to do the work and call back when done thread = Thread(target=self._scan_thread, kwargs={ 'callback': callback, 'force': force }) thread.start() # Signal we are waiting for a callback available = None else: # No callback so we have to do it here. with self.lock: # Look for any labjacks anywhere results = ljm.listAllS("ANY", "ANY") # Becomes a list of found sn's with model and connections self.available_devices = {} for index in range(results[0]): sn = str(results[3][index]) model = results[1][index] if model in LABJACK_MODELS: model = LABJACK_MODELS[model] connection = results[2][index] if connection in LABJACK_CONNECTIONS: connection = LABJACK_CONNECTIONS[connection] # Remove model and connections seen for each serial number if sn not in self.available_devices: self.available_devices[sn] = { 'model': model, 'connections': [connection] } else: if connection not in self.available_devices[ sn]['connections']: self.available_devices[sn][ 'connections'].append(connection) available = self.available_devices else: available = self.available_devices return available
def ld_connect(dt, ct): """ function used to detect LabJack device(s) using specific protocol(s) dt: device type DEVICE and ANY allowed ct: connection type CT values and ANY allowed return: list of connection to devices and list of connection information in case of loosing connection for trying to restore the connection to de device >>> dev = ld_connect(ANY, ANY) #it will connect to any labjack devices using any protocols >>> dev = ld_connect(DEVICE_T7, CT[3]) #it will connect to labjack t7 devices using tcp protocols >>> dev = ld_connect(DEVICE_T7, ANY) #it will connect to labjack t7 devices using any protocols >>> dev = ld_connect(DEVICE_T7, CT[2]) #it will connect to labjack t7 devices using only wifi >>> dev = ld_connect(DEVICE_T7, CT[1]) #it will connect to labjack t7 devices using only ethernet >>> dev = ld_connect(DEVICE_T7, CT[0]) #it will connect to labjack t7 devices using only usb """ connection_info = [] handle = [] labjack_devices = ljm.listAllS(dt, ct) for index in range(labjack_devices[CD_INDEX]): try: handle.append( ljm.open( labjack_devices[DT_INDEX][index], labjack_devices[CT_INDEX][index], labjack_devices[SN_INDEX][index] ) ) connection_info.append( [ labjack_devices[DT_INDEX][index], labjack_devices[CT_INDEX][index], labjack_devices[SN_INDEX][index] ] ) except ljm.ljm.LJMError as LjmError: print( LjmError._errorString + ": Unable to connect to device " + str( [ labjack_devices[DT_INDEX][index], labjack_devices[CT_INDEX][index], labjack_devices[SN_INDEX][index] ] ) ) return handle, connection_info
listAllS. Args: functionName: The name of the function used info: tuple returned by listAll or listAllS """ print("\n%s found %i LabJacks:\n" % (functionName, info[0])) fmt = ''.join(["{%i:<18}" % i for i in range(0, 4)]) print( fmt.format("Device Type", "Connection Type", "Serial Number", "IP Address")) for i in range(info[0]): print( fmt.format(DEVICE_TYPES.setdefault(info[1][i], str(info[1][i])), CONN_TYPES.setdefault(info[2][i], str(info[2][i])), str(info[3][i]), ljm.numberToIP(info[4][i]))) # listAll and listAllS returns the tuple (numFound, aDeviceTypes, # aConnectionTypes, aSerialNumbers, aIPAddresses) # Find and display LabJack devices with listAllS. info = ljm.listAllS("ANY", "ANY") displayDeviceInfo("listAllS", info) """ # Find and display LabJack devices with listAll. info = ljm.listAll(ljm.constants.ctANY, ljm.constants.ctANY) displayDeviceInfo("listAll", info) """
CONN_TYPES = {ljm.constants.ctUSB: "USB", ljm.constants.ctTCP: "TCP", \ ljm.constants.ctETHERNET: "Ethernet", ljm.constants.ctWIFI: "WiFi"} #Displays the LabJack devices information from listAll or listAllS. #functionName: The name of the function used #info: tuple returned by listAll or listAllS def displayDeviceInfo(functionName, info): print("\n%s found %i LabJacks:\n" % (functionName, info[0])) fmt = ''.join(["{%i:<18}" % i for i in range(0, 4)]) print(fmt.format("Device Type", "Connection Type", "Serial Number", \ "IP Address")) for i in range(info[0]): print(fmt.format(DEVICE_TYPES.setdefault(info[1][i], str(info[1][i])), \ CONN_TYPES.setdefault(info[2][i], str(info[2][i])), \ str(info[3][i]), \ ljm.numberToIP(info[4][i]))) #listAll and listAllS returns the tuple (numFound, aDeviceTypes, #aConnectionTypes, aSerialNumbers, aIPAddresses) #Find and display LabJack devices with listAllS. info = ljm.listAllS("ANY", "ANY") displayDeviceInfo("listAllS", info) ''' #Find and display LabJack devices with listAll. info = ljm.listAll(ljm.constants.ctANY, ljm.constants.ctANY) displayDeviceInfo("listAll", info) '''