Debian Codegraph @

pymeasure/instruments/hcp/tc038.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 logging
26
27 from pymeasure.instruments import Instrument
28 from pyvisa.constants import Parity
29
30
31 log = logging.getLogger(__name__)
32 log.addHandler(logging.NullHandler())
33
34
35 def _data_to_temp(data):
36 """Convert the returned hex value "data" to a temperature in °C."""
37 return int(data[7:11], 16) / 10.
38 # get the hex number, convert to int and shift the decimal sign
39
40
41 registers = {'temperature': "D0002",
42 'setpoint': "D0120",
43 }
44
45
46 def _check_errors(response):
47 errors = {"02": "Command does not exist or is not executable.",
48 "03": "Register number does not exist.",
49 "04": "Out of setpoint range.",
50 "05": "Out of data number range.",
51 "06": "Executed monitor without specifying what to monitor.",
52 "08": "Illegal parameter is set.",
53 "42": "Sum does not match the expected value.",
54 "43": "Data value greater than specified received.",
55 "44": "End of data or end of text character is not received.",
56 }
57 if response[5:7] == "OK":
58 return []
59 else: # got[5:7] == "ER"
60 """If communication is completed abnormally, TC038 returns a
61 character string “ER” and error code (EC1 and EC2)"""
62 EC1 = response[7:9]
63 if EC1 in ("03", "04", "05", "08"):
64 EC2 = response[9:11]
65 return [errors[EC1] + f" Wrong parameter has number {EC2}."]
66 return [errors[EC1]]
67
68
69 class TC038(Instrument):
70 """
71 Communication with the HCP TC038 oven.
72
73 This is the older version with an AC power supply and AC heater.
74
75 It has parity or framing errors from time to time. Handle them in your
76 application.
77
78 The oven always responds with an "OK" to all valid requests or commands.
79
80 :param str adapter: Name of the COM-Port.
81 :param int address: Address of the device. Should be between 1 and 99.
82 :param int timeout: Timeout in ms.
83 """
84
85 def __init__(self, adapter, name="TC038", address=1, timeout=1000,
86 **kwargs):
87 super().__init__(
88 adapter,
89 name,
90 timeout=timeout,
91 write_termination="\r",
92 read_termination="\r",
93 parity=Parity.even,
94 includeSCPI=False,
95 **kwargs,
96 )
97 self.address = address
98
99 self.set_monitored_quantity() # start to monitor the temperature
100
101 def write(self, command):
102 """Send a `command` in its own protocol."""
103 # 010 is CPU (01) and time to wait (0), which are fix
104 super().write(chr(2) + f"{self.address:02}" + "010" + command + chr(3))
105
106 def read(self):
107 """Do error checking on reading."""
108 # Response is chr(2) + address:02 + "01" + response + chr(3)
109 got = super().read()
110 errors = _check_errors(got)
111 if errors:
112 raise ConnectionError(errors[0])
113 return got
114
115 def check_set_errors(self):
116 """Check for errors after having set a property.
117
118 Called if :code:`check_set_errors=True` is set for that property.
119 """
120 try:
121 self.read()
122 except ConnectionError as exc:
123 log.exception("Setting a property failed.", exc_info=exc)
124 raise
125 else:
126 return []
127
128 def set_monitored_quantity(self, quantity='temperature'):
129 """
130 Configure the oven to monitor a certain `quantity`.
131
132 `quantity` may be any key of `registers`. Default is the current
133 temperature in °C.
134 """
135 # WRS in order to setup to monitor a word
136 # monitor 1 word
137 # monitor the word in register D0002
138 self.ask(command="WRS" + "01" + registers[quantity])
139
140 setpoint = Instrument.control(
141 "WRD" + registers['setpoint'] + ",01",
142 "WWR" + registers['setpoint'] + ",01,%s",
143 """Control the setpoint of the temperature controller in °C.""",
144 get_process=_data_to_temp,
145 set_process=lambda temp: f"{int(round(temp * 10)):04X}",
146 check_set_errors=True,
147 )
148
149 temperature = Instrument.measurement(
150 "WRD" + registers['temperature'] + ",01",
151 """Measure the current temperature in °C.""",
152 get_process=_data_to_temp
153 )
154
155 monitored_value = Instrument.measurement(
156 "WRM",
157 """Measure the currently monitored value. For default it is the current
158 temperature in °C.""",
159 get_process=_data_to_temp
160 )
161
162 information = Instrument.measurement(
163 "INF6",
164 """Get the information about the device and its capabilities.""",
165 get_process=lambda got: got[7:-1],
166 )