# PyVisa simulator import numpy as np 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 self.status = {} self.out_buffer = '' self.output_form = 'short' # 'long' is the other choice. if visa_id == "USB0::0x0699::0x0366::C040717" : self.function = 'scope' self.idn = 'Fake TDS1012B' # Set some typical operational values self.status['wfmpre:xunit'] = 's' self.status['wfmpre:xincr'] = '1.0e-6' self.status['wfmpre:yunit'] = 'V' self.status['wfmpre:ymult'] = '1.0e-3' self.status['wfmpre:yoff'] = '0' self.status['wfmpre:yzero'] = '0' k = 2.0 * np.pi/1250.0 self.ascii_data = '' for i in range(2500) : self.ascii_data += str(int(127.50 * (np.sin(k * i) + 1))) + ',' self.ascii_data = self.ascii_data.rstrip(',') #print self.curve elif visa_id == "USB0::0x0699::0x0346::C031285" : self.function = 'wg' self.idn = 'Fake AFG3021B' else : self.function = '?' """ 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 if com.lower() == 'curve' : if self.status['data:encdg'] == 'ascii' : response = self.ascii_data else : try: response = self.status[com] # This is the short form response. Long would be com + ' ' + status[com]. except : response = 'Error, ' + com + ' not specified.' 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()