# Module to find all current VISA-compatible devices and VISA-USB devices. try: import visa as v except : print '\nWarning: PyVisa is not available.' try: import visa_fake as v print '\nA pseudo PyVisa, visa_fake, was imported.' except: print '\nWarning: Fake PyVisa is not available.' exit(0) import string as s ## Use PyVisa to find all visa-compatible devices on usb, com, lpt, gpib connections def FindVisaDevices(print_messages=False, write_log=False) : logfile = "log_visa.txt" # a is list of strings, one per instrument. Each string contains the connection, vendor id, model number and serial number. a = v.get_instruments_list() # Print the list of instruments to the terminal. if print_messages : print('\nVISA devices found:') print(a) #print('\t' + '\n\t'.join(a)) # Writing result of actions to a log file can be useful for trouble-shooting. if write_log : f = open(logfile, "w") #Open a text file to record the output. The 'w' means "write". f.write('Result of search for VISA devices:') f.write('\n\t' + '\n\t'.join(a)) # '\t' is a tab and '\n' is a new line or linefeed (lf). f.close() # Always close whatever you have opened. print('\n' + logfile + ' written.') return a ## Find just the USB-connected devices def FindUSBDevices(print_messages=False, write_log=False) : logfile = "log_visa_usb.txt" visa_devs = FindVisaDevices(write_log) # Use FindVisaDevices function defined in find_visa_devices.py usb_devs = {} # Create an empty dictionary for dev in visa_devs : if dev.find('USB') > -1 : protocol, vid, pid, sn = s.split(dev, '::') usb_devs[dev] = {'protocol' : protocol, 'pid' : pid, 'sn' : sn,'vid' : vid} # Each device is specified by visa identifier string dev. if print_messages : print('\nVISA USB devices found ( {visa id string : {protocol, product id, serial number, vendor id}):') for thing in usb_devs : print('\t"' + thing + '" : {' + ', '.join(['"%s" : %s' % (key, value) for (key, value) in sorted(usb_devs[thing].items())]) + '}') if write_log : f = open(logfile, 'w') f.write('\nVISA USB devices found ( {visa id string : {protocol, product id, serial number, vendor id}):') for thing in usb_devs : f.write('\n\t{"' + thing + '" : {' + ', '.join(['"%s" : %s' % (key, value) for (key, value) in sorted(usb_devs[thing].items())]) + '}}') f.close() print('\n' + logfile + ' written.') return usb_devs # Return a dictionary of visa-usb devices #------------------------------------------------------------------------------- if __name__ == '__main__' : #FindVisaDevices(print_messages=True, write_log=True) usb = FindUSBDevices(print_messages=True, write_log=True)