""" Module providing the ability to parse a command file and configure active instruments. The following modules must in the working directory or the Python path: visa.py or visa_fake.py visa_devices.py supported_instruments.py instrument_classes.py The following text files must be in the current working directory (cwd). supported_instruments.txt configure_instruments.txt """ import string as s import time try: import instrument_classes as ic except: print('\nsupported_instruments.py not found.') exit(0) def ReadInitializationFile(file_name, validation) : # Validation is the name of this program or the base name in a set of programs: 'configure_instruments.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 SendCommands(scope, wg, commands, print_messages=False) : error = '' prt = print_messages if prt : print('\nCommand sequence:') for c in commands : if (c[0] == 'wg') & (wg.pipe != '') : # Send commands only if fg exists wg.Write(c[1]) if prt : print 'wg ' + c[1] + ' -> time taken = ' + str(wg.time) if wg.error != '' : print wg.error wg.error = '' elif (c[0] == 'scope') & (scope.pipe != '') : # 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] == 'wg-read') & (wg.pipe != '') : try: print wg.Read() #print 'wg-read ' except : error += 'wg-read error\n' elif (c[0] == 'scope-read') & (scope.pipe != '') : 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 InitializeInstruments(target_instruments, com, print_messages=False) : error = '' instruments_dict = ic.CreateInstrumentObjects(print_messages=True) wg_tag = s.replace('-'.join(target_instruments['wg']), ' ', '') osc_tag = s.replace('-'.join(target_instruments['scope']), ' ', '') #print wg_tag, osc_tag scope, wg = ic.SelectOneInstrumentOfEachType(instruments_dict, osc_tag, wg_tag, print_messages=False) if print_messages : print('\nInstruments to be initialized:') print('\t' + scope.name) print('\t' + wg.name) error = SendCommands(scope, wg, com, print_messages=True) if print_messages : # Query some parameters. print('\nQuery some scope parameters:') scope.write('*IDN?') idn = scope.read() print('\t*IDN? : ' + idn) scope.write('ch1:probe?') print('\tch1:probe? : ' + scope.read()) scope.write('ch1:scale?') print('\tch1:scale? : ' + scope.read()) if idn.lower().find('fake') > -1 : print('\tStatus :') print(scope.pipe.status) # Valid statement only when using visa_fake.py print('\nQuery some wg parameters:') wg.write('*IDN?') idn = wg.read() print('\t*IDN? : ' + idn) if idn.lower().find('fake') > -1 : print('\tStatus :') print(wg.pipe.status) # Valid statement only when using visa_fake.py return error def Test() : com, target_instruments, error, warning = ReadInitializationFile('configure_instruments.txt', 'configure_instruments.py') if error : print error if warning: print warning print #for c in com : print c print target_instruments error = InitializeInstruments(target_instruments, com, print_messages=True) if error : print error else : print('\nInstruments successfully initialized.') #--------------------------------------------------------------------------------------------------- if __name__ == '__main__' : Test()