Debian Codegraph @

pymeasure/instruments/andeenhagerling/ah2500a.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 import math
26 import re
27 import logging
28
29 from pymeasure.instruments import Instrument
30 from pymeasure.instruments.validators import strict_range
31
32 log = logging.getLogger(__name__)
33
34
35 class AH2500A(Instrument):
36 """ Andeen Hagerling 2500A Precision Capacitance Bridge implementation
37 """
38 # regular expression to extract measurement values
39 _reclv = re.compile(
40 r"[FHZ0-9.=\s]*C=\s*(-?[0-9.]+)\s*PF L=\s*(-?[0-9.]+)\s*NS V=\s*(-?[0-9.]+)\s*V")
41 _renumeric = re.compile(r'[-+]?(\d*\.?\d+)')
42
43 def __init__(self, adapter, name=None, timeout=3000,
44 write_termination="\n", read_termination="\n",
45 **kwargs):
46 super().__init__(
47 adapter,
48 name or "Andeen Hagerling 2500A Precision Capacitance Bridge",
49 write_termination=write_termination,
50 read_termination=read_termination,
51 timeout=timeout,
52 includeSCPI=False,
53 **kwargs
54 )
55 self._triggered = False
56
57 config = Instrument.measurement(
58 "SHOW",
59 """Get the configuration.""",
60 )
61
62 caplossvolt = Instrument.measurement(
63 "Q",
64 """Get the result of a single capacitance, loss measurement and return the
65 values in units of pF and nS. The used measurement voltage is returned
66 as third value.""",
67 # lambda function is needed here since AH2500A is otherwise undefined
68 get_process=lambda v: AH2500A._parse_reply(v),
69 )
70
71 vhighest = Instrument.control(
72 "SH V", "V %.4f",
73 """Control maximum RMS value of the used measurement voltage. Values of up to
74 15 V are allowed. The device will select the best suiting range below
75 the given value.""",
76 validator=strict_range,
77 values=[0, 15],
78 # typical replies: "VOLTAGE HIGHEST= 15.0 V" or
79 # "VOLTAGE HIGHEST 1.00 V"
80 get_process=lambda v: float(AH2500A._renumeric.search(v).group(0)),
81 )
82
83 @classmethod
84 def _parse_reply(cls, string):
85 """
86 parse reply string from Andeen Hagerling capacitance bridges.
87
88 :param string: reply string from the instrument. This commonly is:
89 2500A:
90 "C= 1.234567 PF L= 0.000014 NS V= 0.750 V"
91 2700A:
92 "F= 1000.00 HZ C= 4.20188 PF L=-0.0260 NS V= 15.0 V"
93 :returns: tuple with C, L, V values
94 """
95 m = cls._reclv.match(string)
96 if m is not None:
97 values = tuple(map(float, m.groups()))
98 return values
99 # if an invalid string is returned ('EXCESS NOISE')
100 if string.strip() == "EXCESS NOISE":
101 log.warning("Excess noise, check your experiment setup")
102 return (math.nan, math.nan, math.nan)
103 else: # some unknown return string (e.g. misconfigured units)
104 raise Exception(f'Returned string "{string}" could not be parsed')
105
106 def trigger(self):
107 """
108 Triggers a new measurement without blocking and waiting for the return
109 value.
110 """
111 self.write("TRG")
112 self._triggered = True
113
114 def triggered_caplossvolt(self):
115 """
116 reads the measurement value after the device was triggered by the
117 trigger function.
118 """
119 if not self._triggered:
120 log.warning(
121 "Device not triggered, trigger manually for better timing")
122 self.trigger()
123 self._triggered = False
124 return AH2500A._parse_reply(self.read())