Debian Codegraph @

pymeasure/instruments/thyracont/smartline_v1.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.validators import strict_discrete_set
26 from pymeasure.instruments import Instrument
27
28
29 def calculate_checksum(msg):
30 """Calculate a 1 bytes checksum mapped to a printable ASCII character.
31
32 The checksum is calculated by the sum of the decimal values of each message
33 character modulo 64 + 64.
34
35 :param string msg: message content
36 :returns: calculated checksum
37 :rtype: string
38 """
39 chksum = sum(map(ord, msg)) % 64 + 64
40 return chr(chksum)
41
42
43 class SmartlineV1(Instrument):
44 """Thyracont Vacuum Instruments Smartline gauges with Communication Protocol V1.
45
46 Devices using Protocol V1 were manufactured until 2017.
47
48 Connection to the device is made through an RS485 serial connection.
49 The default communication settings are baudrate 9600, 8 data bits, 1 stop bit,
50 no parity, no handshake.
51
52 A communication packages is structured as follows:
53
54 Characters 0-2: Address for communication
55 Character 3: Command character, uppercase letter for reading and lowercase for writing
56 Characters 4-n: Data for the command, can be empty.
57 Character n+1: Checksum calculated by: (sum of the decimal value of bytes 0-n) mod 64 + 64
58 Character n+2: Carriage return
59
60 :param adapter: pyvisa resource name of the instrument or adapter instance
61 :param string name: Name of the instrument.
62 :param int address: RS485 adddress of the instrument 1-15.
63 :param int baud_rate: baudrate used for the communication with the device.
64 :param kwargs: Any valid key-word argument for Instrument
65
66 """
67 # API is described in detail in this link:
68 # https://wiki.kern.phys.au.dk/Interface_protokoll_Thyracont_eng5.pdf
69
70 def __init__(self, adapter, name="Thyracont Vacuum Gauge V1", address=1,
71 baud_rate=9600, **kwargs):
72 super().__init__(adapter,
73 name,
74 includeSCPI=False,
75 write_termination="\r",
76 read_termination="\r",
77 asrl=dict(baud_rate=baud_rate),
78 **kwargs)
79 self.address = address
80
81 def read(self):
82 """Reads a response message from the instrument.
83
84 This method also checks for a correct checksum.
85
86 :returns: the data fields
87 :rtype: string
88 :raises ValueError: if a checksum error is detected
89 """
90 msg = super().read()
91 chksum = calculate_checksum(msg[:-1])
92 if msg[-1] == chksum:
93 return msg[:-1]
94 else:
95 raise ConnectionError(
96 f"checksum error in received message {msg} "
97 f"with checksum {chksum} but received {msg[-1]}")
98
99 def write(self, command):
100 """Writes a command to the instrument.
101
102 This method adds the required address and checksum.
103
104 :param str command: command to be sent to the instrument
105 """
106 fullcmd = f"{self.address:03d}" + command
107 super().write(fullcmd + calculate_checksum(fullcmd))
108
109 def check_set_errors(self):
110 reply = self.read()
111 if len(reply) < 4:
112 raise ConnectionError(f"Reply of instrument ('{reply}') too short.")
113 if reply[3] in ["N", "X"]:
114 raise ConnectionError(f"Reply from Instrument indicates an error '{reply}'")
115 return []
116
117 device_type = Instrument.measurement(
118 "T",
119 """Get the device type.""",
120 cast=str,
121 preprocess_reply=lambda s: s[4:],
122 )
123
124 pressure = Instrument.measurement(
125 "M",
126 """Get the pressure measurement in mbar.""",
127 cast=str,
128 preprocess_reply=lambda s: s[4:],
129 get_process=lambda s: float(s[:4])/1000*10**(int(s[4:])-20),
130 )
131
132 display_unit = Instrument.control(
133 "U", "u%06d",
134 """Control the display's pressure unit.""",
135 cast=int,
136 preprocess_reply=lambda s: s[4:],
137 values={"mbar": 0, "Torr": 1, "hPa": 2},
138 map_values=True,
139 validator=strict_discrete_set,
140 )
141
142 cathode_enabled = Instrument.control(
143 "I", "i%d",
144 """Control the hot/cold cathode state of the pressure gauge.""",
145 cast=int,
146 preprocess_reply=lambda s: s[4:],
147 values={True: 1, False: 0},
148 map_values=True,
149 validator=strict_discrete_set,
150 check_set_errors=True,
151 )