Debian Codegraph @

pymeasure/instruments/inficon/sqm160.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 from pymeasure.instruments import Channel, Instrument
26
27
28 def calculate_checksum(msg):
29 """calculate a two byte Cyclic Redundancy Check based on 14 bits
30
31 Parameters
32 ----------
33 msg: bytes
34 Message of the device without the sync character
35 """
36 # check if message contains data
37 if not msg:
38 return chr(0) + chr(0)
39 # initialize CRC
40 crc = 0x3fff
41 # loop over characters in message
42 for char in msg:
43 crc ^= char
44 for i in range(8):
45 tmpcrc = crc
46 crc = crc >> 1
47 if tmpcrc & 1 == 1:
48 crc ^= 0x2001
49 crc &= 0x3fff
50 # separate 14 significant bits in two byte checksum
51 return bytes(((crc & 0x7f) + 34, ((crc >> 7) & 0x7f) + 34))
52
53
54 class SensorChannel(Channel):
55 """Sensor channel for individual rate measurements."""
56
57 rate = Channel.measurement(
58 "L{ch}?", """Get the current rate for a sensor in Angstrom per second""",
59 cast=float,
60 )
61
62 thickness = Channel.measurement(
63 "N{ch}", """Get the current thickness for a sensor in Angstrom""",
64 cast=float,
65 )
66
67 frequency = Channel.measurement(
68 "P{ch}", """Get the current frequency for a sensor in Hz""",
69 cast=float,
70 )
71
72 crystal_life = Channel.measurement(
73 "R{ch}", """Get the crystal life value in percent""",
74 cast=float,
75 )
76
77
78 class SQM160(Instrument):
79 """Inficon SQM-160 multi-film rate/thickness monitor.
80
81 Uses a quartz crystal sensor to measure rate and thickness in a thin
82 film deposition process. Connection to the device is commonly made through
83 a serial connection (RS232) or optionally via USB or Ethernet.
84
85 A command packet always consists of the following:
86 - 1 Byte: Sync character ('!' appears only at the start of a message).
87 - 1 Byte: length character obtained from the message length without CRC.
88 A value of 34 is added so that no '!' can occur.
89 - Command message with variable length.
90 - 2 Byte: Cyclic Redundancy Check (CRC) checksum.
91
92 A response packet always consists of:
93 - 1 Byte: Sync character ('!' appears only at the start of a message).
94 - 1 Byte: length character obtained from the message length without CRC.
95 A value of 35 is added.
96 - 1 Byte: Response status character indicating the status of the command.
97 - Response message with variable length.
98 - 2 Byte: Cyclic Redundancy Check (CRC) checksum.
99
100 :param adapter: pyvisa resource name of the instrument or adapter instance
101 :param string name: Name of the instrument.
102 :param string baud_rate: Baud rate used by the serial connection.
103 :param kwargs: Any valid key-word argument for Instrument
104
105 """
106 sensor_1 = Instrument.ChannelCreator(SensorChannel, 1)
107 sensor_2 = Instrument.ChannelCreator(SensorChannel, 2)
108 sensor_3 = Instrument.ChannelCreator(SensorChannel, 3)
109 sensor_4 = Instrument.ChannelCreator(SensorChannel, 4)
110 sensor_5 = Instrument.ChannelCreator(SensorChannel, 5)
111 sensor_6 = Instrument.ChannelCreator(SensorChannel, 6)
112
113 def __init__(self, adapter, name="Inficon SQM-160 thickness monitor",
114 baud_rate=19200, **kwargs):
115 super().__init__(adapter,
116 name,
117 includeSCPI=False,
118 write_termination="",
119 read_termination="",
120 asrl=dict(baud_rate=baud_rate),
121 timeout=3000,
122 **kwargs)
123
124 def read(self):
125 """Reads a response message from the instrument.
126
127 This method also checks for a correct checksum.
128
129 :returns: the response packet
130 :rtype: string
131 :raises ConnectionError: if a checksum error is detected or a wrong
132 response status is detected.
133 """
134 header = self.read_bytes(2)
135 # check valid header
136 if header[0] != 33: # b"!"
137 raise ConnectionError(f"invalid header start byte '{header[0]}' received")
138 length = header[1] - 35
139 if length <= 0:
140 raise ConnectionError(f"invalid message length '{header[1]}' -> length {length}")
141
142 response_status = self.read_bytes(1)
143 if response_status == b"C":
144 raise ConnectionError("invalid command response received")
145 elif response_status == b"D":
146 raise ConnectionError("Problem with data in command")
147 elif response_status != b"A":
148 raise ConnectionError(f"unknown response status character '{response_status}'")
149
150 if length - 1 > 0:
151 data = self.read_bytes(length - 1)
152 else:
153 data = b""
154 chksum = self.read_bytes(2)
155 calculated_checksum = calculate_checksum(
156 header[1].to_bytes(length=1, byteorder='big') + response_status + data)
157 if chksum == calculated_checksum:
158 return data.decode()
159 else:
160 raise ConnectionError(
161 f"checksum error in received message '{header + response_status + data}' "
162 f"with checksum '{calculated_checksum}' but received '{chksum}'")
163
164 def write(self, command):
165 """Write a command to the device."""
166 length = chr(len(command) + 34)
167 message = f"{length}{command}".encode()
168 self.write_bytes(b"!" + message + calculate_checksum(message))
169
170 def check_set_errors(self):
171 """Check the errors after setting a property."""
172 self.read()
173 return [] # no error happened
174
175 firmware_version = Instrument.measurement(
176 "@", """Get the firmware version.""",
177 cast=str,
178 )
179
180 number_of_channels = Instrument.measurement(
181 "J", """Get the number of installed channels""",
182 cast=int,
183 )
184
185 average_rate = Instrument.measurement(
186 "M", """Get the current average rate in Angstrom per second""",
187 cast=float,
188 )
189
190 average_thickness = Instrument.measurement(
191 "O", """Get the current average thickness in Angstrom""",
192 cast=float,
193 )
194
195 all_values = Instrument.measurement(
196 "W", """Get the current rate (Angstrom/s), Thickness (Angstrom), and frequency (Hz)
197 for each sensor""",
198 cast=float,
199 preprocess_reply=lambda msg: msg[5:], # ingore first '00.00'
200 )
201
202 reset_flag = Instrument.measurement(
203 "Y", """Get the power-up reset flag.
204 It is True only when read first after a power cycle.""",
205 cast=int,
206 values={True: 1, False: 0},
207 map_values=True,
208 )
209
210 def reset_system_parameters(self):
211 """Reset all film and system parameters."""
212 self.write("Z")
213 self.read() # read obligatory response message
214
215 def reset_thickness_rate(self):
216 """Reset the average thickness and rate.
217
218 This also sets all active Sensor Rates and Thicknesses to zero
219 """
220 self.write("S")
221 self.read() # read obligatory response message
222
223 def reset_time(self):
224 """Reset the time of the monitor to zero.
225 """
226 self.write("T")
227 self.read() # read obligatory response message