def get_atten(self, idx=0): """This function returns the current attenuation from an attenuator at a given index in the instrument. Args: idx: This zero-based index is the identifier for a particular attenuator in an instrument. Raises: Error is raised if the underlying telnet connection to the instrument is not open. Returns: A float that is the current attenuation value. """ if not self.is_open: raise attenuator.Error( "Connection to attenuator at %s is not open!" % self._telnet_client.host) if idx + 1 > self.path_count or idx < 0: raise IndexError("Attenuator index out of range!", self.path_count, idx) atten_val_str = self._telnet_client.cmd("CHAN:%s:ATT?" % (idx + 1)) atten_val = float(atten_val_str) return atten_val
def set_atten(self, idx, value): """Sets the attenuation value for a particular signal path. Args: idx: Zero-based index int which is the identifier for a particular signal path in an instrument. For instruments that only has one channel, this is ignored by the device. value: A float that is the attenuation value to set. Raises: Error is raised if the underlying telnet connection to the instrument is not open. IndexError is raised if the index of the attenuator is greater than the maximum index of the underlying instrument. ValueError is raised if the requested set value is greater than the maximum attenuation value. """ if not self.is_open: raise attenuator.Error( "Connection to attenuator at %s is not open!" % self._telnet_client.host) if idx + 1 > self.path_count: raise IndexError("Attenuator index out of range!", self.path_count, idx) if value > self.max_atten: raise ValueError("Attenuator value out of range!", self.max_atten, value) # The actual device uses one-based index for channel numbers. self._telnet_client.cmd("CHAN:%s:SETATT:%s" % (idx + 1, value))
def cmd(self, cmd_str, wait_ret=True): if not isinstance(cmd_str, str): raise TypeError("Invalid command string", cmd_str) if not self.is_open: raise attenuator.Error("Telnet connection not open for commands") cmd_str.strip(self.tx_cmd_separator) self._tn.read_until(_ascii_string(self.prompt), 2) self._tn.write(_ascii_string(cmd_str + self.tx_cmd_separator)) if wait_ret is False: return None match_idx, match_val, ret_text = self._tn.expect( [_ascii_string("\S+" + self.rx_cmd_separator)], 1) if match_idx == -1: raise attenuator.Error( "Telnet command failed to return valid data") ret_text = ret_text.decode() ret_text = ret_text.strip(self.tx_cmd_separator + self.rx_cmd_separator + self.prompt) return ret_text