from tek import * import string as s def FindTekInstrument(inst_list) : # inst_list is of the form [instrument name, serial number string] inst = s.strip(inst_list[0]).upper() sn = s.strip(inst_list[1]).upper() devices = FindUSBDevices() error = '' if inst == 'AFG3021B' : #sn = 'C031180' #print 'Look for AFG3021B:' dev = AFG3021B(devices, sn) if dev.visa == '' : error += "Error: AFG3021B-" + sn + " not found.\n" else : dev.Write('*IDN?') try: response = dev.Read() print "Function generator identification: ", response dev.ClearQueue() except : error += 'Function generator has been found, but it is not responsive.\n' elif inst == 'TDS1012B' : # Now the scope #sn = 'C055976' #print 'Look for TDS1012B:' dev = TDS1012B(devices, sn) if dev.visa == '' : error += "Error: TDS1012B-" + sn + " not found.\n" else : dev.Write('*IDN?') if dev.error : print dev.error error = dev.error else : response = dev.Read() print "Scope identification: ", response if dev.error : error += 'Scope has been found, but it is not responsive.\n' else : dev.ClearQueue() else : error += inst + ' is not supported.\n' dev = '' if error : error += 'Available USB devices: \n' for key in devices : error += key + ' ' + str(devices[key]) + '\n' print dev.error return dev, error def InitializeDevices(inst, com, prt) : # inst is the instrument dictionary, com is the list of commands, prt is the print switch (1/0 = yes/no) error = '' # Find the function generator first. #fg, error = FindTekInstrument('AFG3021B', fg_sn) fg, error = FindTekInstrument(inst['fg']) #if error : #print error # Now find the scope. scope, error2 = FindTekInstrument(inst['scope']) if error2 : #print error2 error += error2 # Send the commands if prt : print 'Command sequence:' for c in com : if (c[0] == 'fg') & (fg.visa != '') : # Send commands only if fg exists fg.Write(c[1]) if prt : print 'fg ' + c[1] + ' -> time taken = ' + str(fg.time) if fg.error != '' : print fg.error fg.error = '' elif (c[0] == 'scope') & (scope.visa != '') : # Send commands only if scope exists scope.Write(c[1]) if prt : print 'scope ' + c[1] + ' -> time taken = ' + str(scope.time) if scope.error != '' : print scope.error scope.error = '' elif (c[0] == 'fg-read') & (fg.visa != '') : try: print fg.Read() #print 'fg-read ' except : error += 'fg-read error\n' elif (c[0] == 'scope-read') & (scope.visa != '') : try: print scope.Read() #print 'scope-read ' except : error += 'scope-read error\n' elif c[0] == 'delay' : if prt : print 'delay ' + c[1] time.sleep(float(c[1])) elif c[0] == 'print' : if prt : print c[1] if error : error = '\nInitialization errors:\n' + error return error def ReadInitializationFile(file_name, validation) : # Validation is the name of this program, 'initialize_fg_scope.py' error = '' warning = '' p = [] instruments = {} fin = open(file_name, 'r') file_lines = fin.readlines() fin.close() for aline in file_lines : al = s.strip(s.split(aline, '#')[0]) # Ignore comment fields. if al : # Ignore comment lines. # Expectations: one command line is assumed after fg = or scope =. a = s.split(al, '=') a[0] = s.strip(a[0]).lower() if len(a) > 1 : a[1] = s.strip(a[1]) if a[0] == 'validation' : if a[1].lower() != validation.lower() : error = 'The validation argument in ' + file_name + ' does not match the name of this program, ' + validation return p, instruments, error, warning elif a[0].startswith('*') : instruments[s.strip(s.split(a[0], '*')[1])] = s.split(a[1], ',') else : p.append(a) # Note that lines without a command string are ignored. if a[1] == '' : warning += 'Malformed line: ' + al + '\n' else : warning += 'Malformed line: ' + al + '\n' if warning : warning = 'Warnings for the following lines in ' + validation + ':\n' + warning return p, instruments, error, warning def Test() : com, instruments, error, warning = ReadInitializationFile('initialize_fg_scope_commands.txt', 'initialize_fg_scope.py') if error : print error if warning: print warning for c in com : print c print instruments error = InitializeDevices(instruments, com, 1) if error : print error else : print 'Instruments successfully initialized.' def Main() : com, instruments, error, warning = ReadInitializationFile('initialize_fg_scope_commands.txt', 'initialize_fg_scope.py') if error : print error if warning: print warning error = InitializeDevices(instruments, com, 1) if error : print error else : print 'Instruments successfully initialized.' # ------------------------------------------------------------------------------- if __name__ == '__main__' : #Test() Main()