### Add a search path to the system python path list so that required modules in other directories can be imported. #path.append('../find_usb_supported_devices') #print path import visa_devices as vd def ParseLine(a) : b = a.split('#')[0].strip() if len(b) == 0 : return {} c = b.split('=') if len(c) != 2 : print('Error in line: ' + a + '\n') return {} parameter = c[0].strip() parameter_value = c[1].strip() return {parameter : parameter_value} def CreateDeviceList(usb_instruments_file) : # Reads file of supported instruments and creates a list. # devices = [{1'vendor':'agilent', 'connection':'usb', 'vid':123, 'pid':454, 'type':['osc', 'wave'], 'channels':2, 'delay': 0.1}] infile = open(usb_instruments_file, 'r') stuff = infile.readlines() infile.close() devices = [] dev = {} for thing in stuff : parsel = ParseLine(thing) #print parsel if len(parsel) > 0 : for key in parsel : tag = key.upper() if tag.find('VEN') == 0 : if 'vendor' in dev : devices.append(dev) dev = {} vendor = parsel[key] dev['vendor'] = vendor elif tag.find('VID') == 0 : vid = int(parsel[key], 16) dev['vid'] = vid elif tag.find('PID') == 0 : pid = int(parsel[key], 16) dev['pid'] = pid elif tag.find('PROD') == 0 : prod = parsel[key] dev['prod'] = prod elif tag.find('TYPE') == 0 : p = parsel[key].split(',') prod_type = [] for ps in p : if ps.find('OSC') == 0 : prod_type.append('osc') elif ps.find('WAVE') == 0 : prod_type.append('wave') else : #print('Unsupported type of product: ' + parsel[key] + '\n') prod_type.append('unsupported-' + p) dev['type'] = prod_type elif tag.find('CHAN') == 0 : channels = int(parsel[key]) dev['channels'] = channels elif tag.find('CONN') == 0 : dev['connection'] = parsel[key] elif tag.find('DELAY') == 0 : dev['delay'] = float(parsel[key]) elif tag.find('POINTS') == 0 : dev['points'] = int(parsel[key]) elif tag.find('PRO') == 0 : dev['protocol'] = parsel[key] else : print('Unrecognized parameter: ' + tag + '\n') return [] devices.append(dev) print('\nSupported Instruments ( a list of dictionaries) :') for thing in devices : print('\t{' + ', '.join(['"%s" : %s' % (key, value) for (key, value) in thing.items()]) + '}') return devices def FindSupportedDevices(devices_connected, supported_devices) : # devices_connected is a dictionary of visa-usb instruments currently connected. # suported_devices is a list of supported devices. # recognized_devices is a dictionary of connected and supported instruments, identified by a model-sn string. recognized_devices = {} for d in devices_connected : for sd in supported_devices : if (int(devices_connected[d]['vid'], 16) == sd['vid']) and (int(devices_connected[d]['pid'], 16) == sd['pid'] ) : sn = devices_connected[d]['sn'] # This is the operating system handle for the pipeline to the instrument. handle = vd.v.instrument(d) # The instrument function is in the visa module imported in the visa_devices module. a = dict(sd.items() + {'handle' : handle, 'sn' : sn}.items()) recognized_devices[a['prod'] + '-' + a['sn']] = a print('\nRecognized Instruments (a dictionary)') for e in recognized_devices : print('\t"' + e + '" : {' + ', '.join(['"%s" : %s' % (key, value) for (key, value) in recognized_devices[e].items()]) + '}') recognized_devices[e]['handle'].write('*IDN?') # Query instrument for its id. print('\t\tID verification: ' + recognized_devices[e]['handle'].read()) print return recognized_devices def RecognizedDevices(): devices_connected = vd.FindUSBDevices(print_messages=True, write_log=False) supported_device_list = CreateDeviceList('supported_instruments.txt') devices = FindSupportedDevices(devices_connected, supported_device_list) return devices def Test(): devices_connected = vd.FindUSBDevices(print_messages=True, write_log=True) supported_device_list = CreateDeviceList('supported_instruments.txt') devices = FindSupportedDevices(devices_connected, supported_device_list) return #------------------------------------------------------------------------------- if __name__ == '__main__' : Test()