def _set_instrument_parameter(self, command): """ Set a parameter on the instrument. We will attempt up to MAX_SET_RETRIES to set the value correctly, according to the following sequence: 1. send set command 2. verify error byte, if returned (per Responses) 3. send query command 4. verify returned value equals the set value (within tolerance) @throws InstrumentParameterException """ response_type = getattr(Responses, command) parameter = getattr(Parameter, command) # store the configured setting old_value = self._param_dict.format(parameter) if old_value is None: raise exceptions.InstrumentParameterException( 'Missing required instrument parameter: %s' % parameter) log.debug('response_type: %s parameter: %s command: %s', response_type, getattr(Parameter, command), command) # attempt to set the value up to MAX_SET_RETRIES times for x in xrange(MAX_RETRIES): if response_type == STATUS: resp = self._do_cmd_resp(command, old_value) self._check_error_byte(resp) else: self._do_cmd_no_resp(command, old_value) # query the value from the instrument to load the parameter dictionary self._do_cmd_resp(command) # if values match, we were successful, return. difference = abs(self._param_dict.format(parameter) - old_value) if difference < CLOSE_ENOUGH: return log.error( 'Set attempt failed. Parameter: %s Set value: %s Returned value: %s difference: %.2f', parameter, old_value, self._param_dict.get(parameter), difference) # configuring the RGA failed, restore the setting from our configuration and raise an exception self._param_dict.set_value(parameter, old_value) raise exceptions.InstrumentParameterException( 'Unable to set instrument parameter: %s, attempted %d times' % (parameter, MAX_RETRIES))
def _set_params(self, *args, **kwargs): """ Set parameters, raise a CONFIG_CHANGE event if necessary. @throws InstrumentParameterException """ self._verify_not_readonly(*args, **kwargs) params_to_set = args[0] old_config = self._param_dict.get_all() # check if in range constraints = ParameterConstraints.dict() parameters = Parameter.reverse_dict() # step through the list of parameters for key, val in params_to_set.iteritems(): # if constraint exists, verify we have not violated it constraint_key = parameters.get(key) if constraint_key in constraints: var_type, minimum, maximum = constraints[constraint_key] try: value = var_type(val) except ValueError: raise exceptions.InstrumentParameterException( 'Unable to verify type - parameter: %s value: %s' % (key, val)) if val < minimum or val > maximum: raise exceptions.InstrumentParameterException( 'Value out of range - parameter: %s value: %s min: %s max: %s' % (key, val, minimum, maximum)) # all constraints met or no constraints exist, set the values for key, val in params_to_set.iteritems(): if key in old_config: self._param_dict.set_value(key, val) else: raise exceptions.InstrumentParameterException( 'Attempted to set unknown parameter: %s value: %s' % (key, val)) new_config = self._param_dict.get_all() # If we changed anything, raise a CONFIG_CHANGE event if old_config != new_config: self._driver_event(DriverAsyncEvent.CONFIG_CHANGE)