# PyVisa simulator def get_instruments_list(): return ["USB0::0x0699::0x0346::C031285", "USB0::0x0699::0x0366::C040717", "COM1", "COM3", "LPT1"] class instrument : def __init__(self, visa_id) : # If visa_id = "USB0::0x0699::0x0346::C031285", then this instrument is a Tek AFG3021B awg. # If visa_id = "USB0::0x0699::0x0366::C040717", then this instrument is a Tek TDS102B scope. self.visa_id = visa_id self.identity = 'Pseudo instrument ' + visa_id if visa_id == "USB0::0x0699::0x0366::C040717" : self.function = 'scope' self.idn = 'Fake TDS1012B' elif visa_id == "USB0::0x0699::0x0346::C031285" : self.function = 'wg' self.idn = 'Fake AFG3021B' else : self.function = '?' self.status = {} self.out_buffer = '' self.output_form = 'short' # 'long' is the other choice. """ if self.function == 'scope' : self.status['ch1:coupling'] = 'ac' self.status['ch1:bandwidth'] = 'off' if self.function == 'wg' : self.status['function'] = 'sin' self.status['function:frequency'] = '1.0e3' self.status['voltage:amplitude'] = '1.000' """ def write(self, stuff) : # Is this a query or an action? stuff.strip() query = stuff.find('?') if query > -1 : # Strip leading ':' or '::'. com = stuff[0:query].strip(':').strip() # Only one query is assumed. if com.lower() == '*idn' : response = self.idn else : response = self.status[com] # This is the short form response. Long would be com + ' ' + status[com]. if self.output_form == 'long' : response = com + ' ' + response self.output_buffer = response else : # Strip leading ':' or '::'. stuff.strip(':') # Then split by the continuation character ';' coms = stuff.split(';') lc = len(coms) # If length of coms is 1, then the single command and argument are simple to add to the dictionary. # If coms has more than one entry, then subsequent entries must be analyzed to determine if they # are continuations of the first entry. if lc == 1 : # Split on space to get the argument splits = coms[0].split(' ') # Warning: there might be more than one consecutive space in the command. command = splits[0].strip() argument = splits[-1].strip() # Use the last element to handle the possibility of consecutive spaces. self.status[command] = argument else : # For the first command, split on space to get the argument splits = coms[0].split(' ') # Warning: there might be more than one consecutive space in the command. command = splits[0].strip() argument = splits[-1].strip() # Use the last element to handle the possibility of consecutive spaces. self.status[command] = argument base_command = command.split(':')[0].strip() + ':' for c in coms[1:] : # From com[1] to the last element. splits = c.split(' ') # Warning: there might be more than one consecutive space in the command. command = splits[0].strip() argument = splits[-1].strip() # Use the last element to handle the possibility of consecutive spaces. self.status[base_command + command] = argument def read(self) : return self.output_buffer def Test() : inst = get_instruments_list() print inst scope = instrument(inst[1]) scope.write('*IDN?') print scope.read() print scope.status scope.write('ch1:probe 1;scale 0.5;position -2.0') print scope.status scope.write('ch1:probe ?') print scope.read() wg = instrument(inst[0]) print wg.status wg.write('voltage:offset 0') wg.write('voltage:phase:adjust 0') print wg.status #-------------------------------------------------------------------------------- if __name__ == '__main__' : Test()