Debian Codegraph @

pymeasure/instruments/agilent/agilent4294A.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 Instrument, SCPIMixin
26 from pymeasure.instruments.validators import strict_range, strict_discrete_set
27 import pandas as pd
28 import numpy as np
29 import os
30
31 # Set of valid arguments for the MEAS? command
32 MEASUREMENT_TYPES = [
33 "IMPH",
34 "IRIM",
35 "LSR",
36 "LSQ",
37 "CSR",
38 "CSQ",
39 "CSD",
40 "AMPH",
41 "ARIM",
42 "LPG",
43 "LPQ",
44 "CPG",
45 "CPQ",
46 "CPD",
47 "COMP",
48 "IMLS",
49 "IMCS",
50 "IMLP",
51 "IMCP",
52 "IMRS",
53 "IMQ",
54 "IMD",
55 "LPR",
56 "CPR",
57 ]
58
59
60 class Agilent4294A(SCPIMixin, Instrument):
61 """ Represents the Agilent 4294A Precision Impedance Analyzer """
62
63 def __init__(self, adapter, name="Agilent 4294A Precision Impedance Analyzer",
64 read_termination="\n",
65 write_termination="\n",
66 timeout=5000,
67 **kwargs):
68
69 super().__init__(
70 adapter,
71 name,
72 read_termination=read_termination,
73 write_termination=write_termination,
74 timeout=timeout,
75 **kwargs
76 )
77
78 start_frequency = Instrument.control(
79 "STAR?", "STAR %d HZ", "Control the start frequency in Hz",
80 validator=strict_range, values=[40, 140E6]
81 )
82
83 stop_frequency = Instrument.control(
84 "STOP?", "STOP %d HZ", "Control the stop frequency in Hz",
85 validator=strict_range, values=[40, 140E6]
86 )
87
88 num_points = Instrument.control(
89 "POIN?", "POIN %d", "Control the number of points measured at each sweep",
90 validator=strict_discrete_set, values=range(2, 802),
91 cast=int,
92 )
93
94 measurement_type = Instrument.control(
95 "MEAS?", "MEAS %d", "Control the measurement type. See MEASUREMENT_TYPES",
96 validator=strict_discrete_set, values=MEASUREMENT_TYPES,
97 )
98
99 active_trace = Instrument.control(
100 "TRAC?", "TRAC %s", "Control the active trace",
101 validator=strict_discrete_set, values=["A", "B"]
102 )
103
104 title = Instrument.control(
105 "TITL?", 'TITL "%s"', "Control the title of the active trace"
106 )
107
108 def save_graphics(self, path=""):
109 """ Save graphics on the screen to a file on the local computer.
110 Adapted from:
111 https://www.keysight.com/se/en/lib/software-detail/programming-examples/4294a-data-transfer-program-excel-vba-1645196.html
112 """
113
114 self.write("STOD MEMO") # store to internal memory
115 self.write("PRIC VARI") # save a color image
116
117 root, ext = os.path.splitext(path)
118 if ext != ".tiff":
119 ext = ".tiff"
120 if not root:
121 root = "graphics"
122
123 path = root + ext
124
125 REMOTE_FILE = "agt4294a.tiff" # Filename of the in-memory file on the device
126 self.write(f'SAVDTIF "{REMOTE_FILE}"')
127
128 vErr = self.ask("OUTPERRO?").split(",")
129 if not int(vErr[0]) == 0:
130 self.write(f'PURG "{REMOTE_FILE}"')
131 self.write(f'SAVDTIF "{REMOTE_FILE}"')
132 vErr = self.ask("OUTPERRO?").split(",")
133
134 self.write(f'ROPEN "{REMOTE_FILE}"')
135 lngFileSize = int(self.ask(f'FSIZE? "{REMOTE_FILE}"'))
136 MAX_BUFF_SIZE = 16384
137 iBufCnt = lngFileSize // MAX_BUFF_SIZE
138 if lngFileSize % MAX_BUFF_SIZE > 0:
139 iBufCnt += 1
140
141 with open(path, 'wb') as file:
142 for _ in range(iBufCnt):
143 data = self.adapter.connection.query_binary_values("READ?", datatype='B',
144 container=bytes)
145 file.write(data)
146 self.write(f'PURG "{REMOTE_FILE}"')
147
148 return path
149
150 def get_data(self, path=None):
151 """
152 Get the measurement data from the instrument after completion.
153
154 :param path: Path for optional data export to CSV.
155 :returns: Pandas Dataframe
156 """
157 prev_active_trace = self.active_trace
158
159 num_points = self.num_points
160 freqs = np.array(self.ask("OUTPSWPRM?").split(","), dtype=float)
161 self.active_trace = "A"
162 adata = np.array(self.ask("OUTPDTRC?").split(","), dtype=float).reshape(num_points, 2)
163
164 self.active_trace = "B"
165 bdata = np.array(self.ask("OUTPDTRC?").split(","), dtype=float).reshape(num_points, 2)
166
167 # restore the previous state
168 self.active_trace = prev_active_trace
169
170 df = pd.DataFrame(
171 np.hstack((freqs.reshape(-1, 1), adata, bdata)),
172 columns=["Frequency", "A Real", "A Imag", "B Real", "B Imag"]
173 )
174
175 if path is not None:
176 _, ext = os.path.splitext(path)
177 if ext != ".csv":
178 path = path + ".csv"
179 df.to_csv(path, index=False)
180
181 return df