Commit c3442a2f authored by alisec's avatar alisec
Browse files

PressureUnits to NameEnum, init self.sensors = [], check fsr values in...

PressureUnits to NameEnum, init self.sensors = [], check fsr values in get/set_full_scale_range_mbar/unitless
parent 213f8621
Pipeline #52510 canceled with stages
"""
Device classe for Pfeiffer TPG controllers.
Device class for Pfeiffer TPG controllers.
The Pfeiffer TPG control units are used to control Pfeiffer Compact Gauges.
Models: TPG 251 A, TPG 252 A, TPG 256A, TPG 261, TPG 262, TPG 361, TPG 362 and TPG 366.
......@@ -141,7 +141,12 @@ class PfeifferTPG(SingleCommDevice):
Pfeiffer TPG control unit device class
"""
class PressureUnits(IntEnum):
class PressureUnits(NameEnum):
"""
Enum of available pressure units for the digital display. "0" corresponds either to bar or to mbar depending on
the TPG model. In case of doubt, the unit is visible on the digital display.
"""
mbar = 0
bar = 0
Torr = 1
......@@ -192,11 +197,7 @@ class PfeifferTPG(SingleCommDevice):
super().__init__(com, dev_config)
# list of sensors connected to the TPG
self.sensors = None
# last command sent to the sensors, can be followed
# by any number of calls to enquire() method
self._last_command = None
self.sensors = []
def __repr__(self):
return f"Pfeiffer TPG with {self.number_of_sensors} sensors: {self.sensors}"
......@@ -205,10 +206,6 @@ class PfeifferTPG(SingleCommDevice):
def number_of_sensors(self):
return len(self.sensors)
@property
def last_command(self):
return self._last_command
@property
def unit(self):
"""
......@@ -279,13 +276,8 @@ class PfeifferTPG(SingleCommDevice):
"""
if not isinstance(unit, self.PressureUnits):
try:
unit = self.PressureUnits[unit]
except KeyError:
message = "{} is not part of accepted units: {}".format(
unit, [key.name for key in self.PressureUnits])
logging.error(message)
raise KeyError(message)
unit = self.PressureUnits(unit)
try:
self.com.send_command(f"UNI,{unit.value}")
logging.info(f"Setting display unit to {unit.name}")
......@@ -360,9 +352,12 @@ class PfeifferTPG(SingleCommDevice):
:raises SerialCommunicationIOError: when communication port is not opened
"""
if len(fsr) != self.number_of_sensors or any([not isinstance(f, int)
for f in fsr]):
raise ValueError("Input should be a list with one int per sensor.")
wrong_values = [v for v in fsr if v not in self.config.model.full_scale_ranges_reversed]
if len(fsr) != self.number_of_sensors:
raise ValueError(f"Argument fsr should be of length {self.number_of_sensors}. Received length {len(fsr)}.")
elif wrong_values:
raise ValueError(f"Argument fsr contains invalid values: {wrong_values}. "
f"Accepted values are {list(self.config.model.full_scale_ranges_reversed.keys())}")
str_fsr = ','.join([str(f) for f in fsr])
try:
......@@ -387,6 +382,14 @@ class PfeifferTPG(SingleCommDevice):
except PfeifferTPGError:
logging.error("Query full scale range of all sensors failed.")
raise
wrong_values = [v for v in answer.split(',')
if not v.isdigit() or int(v) not in self.config.model.full_scale_ranges_reversed]
if wrong_values:
raise PfeifferTPGError(f"The controller returned the full scale range values: {answer}. The values "
f"{wrong_values} are invalid. Accepted values are "
f"{list(self.config.model.full_scale_ranges_reversed.keys())}.")
fsr = [int(i) for i in answer.split(',')]
logging.info(f"Obtained full scale range of all sensors as {fsr}.")
return fsr
......@@ -399,12 +402,14 @@ class PfeifferTPG(SingleCommDevice):
:raises SerialCommunicationIOError: when communication port is not opened
"""
if len(fsr) != self.number_of_sensors or any([not isinstance(f, (int, float))
for f in fsr]):
raise ValueError("Input should be a list with one pressure per sensor.")
wrong_values = [v for v in fsr if v not in self.config.model.full_scale_ranges]
if len(fsr) != self.number_of_sensors:
raise ValueError(f"Argument fsr should be of length {self.number_of_sensors}. Received length {len(fsr)}.")
elif wrong_values:
raise ValueError(f"Argument fsr contains invalid values: {wrong_values}. "
f"Accepted values are {list(self.config.model.full_scale_ranges.keys())}")
str_fsr = ','.join([
str(self.config.model.full_scale_ranges[f]) for f in fsr])
str_fsr = ','.join([str(self.config.model.full_scale_ranges[f]) for f in fsr])
try:
self.com.send_command(f"FSR,{str_fsr}")
......@@ -420,12 +425,20 @@ class PfeifferTPG(SingleCommDevice):
:return: full scale range values in mbar, like [0.01, 1, 0.1, 1000, 50000, 10]
:raises SerialCommunicationIOError: when communication port is not opened
"""
try:
answer = self.com.query("FSR")
except PfeifferTPGError:
logging.info("Query full scale range of all sensors failed.")
raise
wrong_values = [v for v in answer.split(',')
if not v.isdigit() or int(v) not in self.config.model.full_scale_ranges_reversed]
if wrong_values:
raise PfeifferTPGError(f"The controller returned the full scale range values: {answer}. The values "
f"{wrong_values} are invalid. Accepted values are "
f"{list(self.config.model.full_scale_ranges_reversed.keys())}.")
fsr = [self.config.model.full_scale_ranges_reversed[int(i)]
for i in answer.split(',')]
logging.info(f"Obtained full scale range of sensors as {fsr} mbar.")
......
......@@ -141,7 +141,7 @@ def test_set_display_unit(started_pfeiffer_tpg):
com.put_text(chr(6))
pg.set_display_unit(pg.PressureUnits.Pascal)
# invalid str argument
with pytest.raises(KeyError):
with pytest.raises(ValueError):
pg.set_display_unit("Not an accepted unit")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment